Location of static files when creating a Django exe using pyinstaller

前端 未结 3 1130
北荒
北荒 2021-01-14 03:26

I have a Django project with the following structure: root videos static templates

and the STATIC_URL setting in settings.py is STATIC_URL = \'/static/\'

相关标签:
3条回答
  • 2021-01-14 03:56

    please see https://docs.djangoproject.com/en/1.10/howto/static-files/

    in your urls.py file, you should add something like blew

    from django.conf import settings
    
    from django.conf.urls.static import static
    
    urlpatterns = [
    url(r'^login$', login, name='login'),
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    and in your app.spec file, add datas

    datas=[('xxx/templates','xxx/templates'),
           ('xxx/static','xxx/static')],
    
    0 讨论(0)
  • 2021-01-14 03:57

    First make sure that django.contrib.staticfiles is included in your INSTALLED_APPS in settings.py. Then have to insert in your settings.py for example this one:

    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "static"),
    ]
    

    I hope definition of BASE_DIR you have in the top of settings.py. BASE_DIR points to a folder, where your manage.py file exists. So if you would have static files in the same dir, then you leave above setting as it is. If not, let's say in the same dir, next to manage.py you will have folder named app, then your settings should look like:

    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, "app/static"),
    ]
    

    And you can even run:

    python manage.py collectstatic
    

    To get admin static files into your static directory.

    Hope that helps.

    0 讨论(0)
  • 2021-01-14 04:02

    This looks like it might be an issue with where the PyInstaller packed python script looks for static files. They are placed in a temporary folder and need to be accessed by an absolute path. Check out this issue: Bundling data files with PyInstaller (--onefile)

    0 讨论(0)
提交回复
热议问题