Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location of static files when creating a Django exe using pyinstaller

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/'

I managed to use pyinstaller to create a windows executable from manage.py. I can start the Django server but I can't figure out where to put the static files.

When I first started the server it could not find the templates as well, it searched for them in : 'root\django\contrib\admin\templates\videos\' I copied the templates to this folder and it worked. But I can't figure out where to put the static files. I tried putting them in 'root\django\contrib\admin\static' to recreate the original structure. But it seems it doesn't search for them there...

Anyone knows where the static files should go? Or how to define the place where a bundled pyinstaller exe will look for them ?

like image 473
Curtwagner1984 Avatar asked Nov 24 '25 18:11

Curtwagner1984


2 Answers

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')],
like image 159
yixing yan Avatar answered Nov 27 '25 09:11

yixing yan


I think I figured this one out. Like you, I was having the same issue where I could package and build my Django project with pyinstaller, but the static files could not be found when running the project from the built executable. Everything was working fine when I would run the project with manage.py, but this was a head scratcher.

The solution that worked for me was running the collectstatic function and then serving my static files from that directory instead of the app/static/app directory.

If you aren't familiar with collectstatic it essentially searches your project directory for all of the static files in your apps and puts them in a folder on your top level directory that you specify in your settings file.

Here's how to run it.

Here's a link to its documentation.

Now my top-level directory looked like this.

site
    app
        admin.py
        apps.py
        models.py
        views.py
    site
        asgi.py
        settings.py
        urls.py
        wsgi.py
    media
        media files
    staticfiles
        static files I want to serve (css, js, etc)

In my settings file I specified my static file settings like this...

STATIC_URL = '/staticfiles/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'app\\static\\app')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Then in my urls.py file I included the staticfiles directory like this... (Note this is the urls.py file in the main site directory not in the app directory if you made one in there)

from django.contrib import admin
from django.conf import settings
from django.urls import include, path
from django.conf.urls.static import static

urlpatterns = [
    path('', include('app.urls')),
    path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

In my spec file I was able to add the folder like this...

datas=[('..\\site\\staticfiles\\', '.\\staticfiles\\')]

And then in my html file I was able to use the static files like this...

<script src="{% static 'app/static.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'app/static.css' %}">

Hopefully this helps some people if they've run into the same problem. Also as a reminder be sure to run collectstatic before you build especially if you've modified or changed any of your static files.

python manage.py collectstatic

Cheers!

like image 38
planetexpressdeliveryboy Avatar answered Nov 27 '25 08:11

planetexpressdeliveryboy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!