Javascript with Django?

前端 未结 8 1264
日久生厌
日久生厌 2021-02-06 19:20

I know this has been asked before, but I\'m having a hard time setting up JS on my Django web app, even though I\'m reading the documentation.

I\'m running the Django de

相关标签:
8条回答
  • 2021-02-06 19:21

    The error 404 occurs because MEDIA_ROOT requires absolute path, not relative. The server is trying to access /media in your filesystem, which is obviously not what you want.

    Try this instead:

    import os
    PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
    MEDIA_ROOT = os.path.join(PROJECT_PATH, 'site_media')
    MEDIA_URL = '/media/'
    
    0 讨论(0)
  • 2021-02-06 19:24

    You may want to use absolute path for 'document_root' in urls.py if you want to use the development server to serve static files. MEDIA_ROOT and MEDIA_URL don't play any role here.

    Here are my settings for your reference. I put all static media files under site_media/

    mysite/
        site_media/
            css/
            js/
            images/
        ...
    

    in settings.py:

    ROOT_PATH = os.path.normpath(os.path.dirname(__file__))
    

    in urls.py:

    url(r'^media/(?P<path>.*)$', "django.views.static.serve", {'document_root':
                                          os.path.join(settings.ROOT_PATH, 'site_media')})
    

    You can move static files else where, just need to point 'document_root' to the correct path. Make sure comment out this url line for production deployment.

    0 讨论(0)
  • 2021-02-06 19:27

    I serve javascript via static. So I have something in my urls.py like

    (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': os.getenv('STATIC_DIR')})
    

    So JS urls look like /static/js/blah.js, CSS urls look like /static/css/blah.css, etc. I have Apache handle the static directory when running in production to avoid any issues with Django's static serving mechanism.

    0 讨论(0)
  • 2021-02-06 19:30

    Actually, you can put your Javascript files (and all your static content) anywhere you want. I mean, Django does not impose a standard on where to place them, after all they won't be handled by Django, they'll be served by the webserver.

    Said that, It's a good idea to keep them somewhere close to the project's files. I'd recommend to keep them in a sibling folder to your Django code. Same with MEDIA_ROOT.

    It is a good idea to decouple your static files from python files because now you can put them in totally separate folders in a production environment and easily give different access to static files and python code (say FTP access, or permissions).

    Something to keep in mind is that the settings' MEDIA_ROOT is the place where user's media files (that is uploaded content) will be placed, these are not your static project files, these are whatever files your Django app uploads (avatars, attachments, etc).

    Proposed folder structure:

    mysite.com/
      media/       - User media, this goes in settings.MEDIA_ROOT
      static/      - This is your static content folder
        css/
        js/
        images/
      templates/
      project/     - This is your Django project folder
        __init__.py
        manage.py
        settings.py
        myapp/
          __init__.py
          ...files..py
    

    See the other responses recommendation on using Django's serve() function for development enviroment. Just make sure you add that url() to your urlpatterns under a settings.DEBUG is True conditional.

    As for your templates, it's a good idea to use a context processor to send your static file's path to all your templates.

    0 讨论(0)
  • 2021-02-06 19:30

    This is a structure I use in a project separated into multiple apps. It's a good practice to adapt this structure right at the start -- you don't want global rearrangement when you need to add another app to your project.

    /media
      favicon.ico
      robots.txt
      /upload # user uploaded files
      /js  # global js files like jQuery
      /main_app
        /js
        /css
        /img
      /other_app
        /js
        /css
        /img
    

    Then I use excellent StaticMiddleware from django-annoying to serve files. settings.py:

    import os
    import annoying
    
    MEDIA_ROOT = os.path.join(os.path.dirname(__file__), "media")
    MIDDLEWARE_CLASSES = (
        'annoying.middlewares.StaticServe',
        #...
    )
    
    0 讨论(0)
  • 2021-02-06 19:39

    I just remove the '^', "r'static/" instead of "r'^static/". It's works for me ,good luck.

    url(r'static/(?P<path>.*)$', 'django.views.static.serve',
         { 'document_root': the_path }),
    
    0 讨论(0)
提交回复
热议问题