3

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 ?

Curtwagner1984
  • 1,908
  • 4
  • 30
  • 48

4 Answers4

4

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')],
yixing yan
  • 173
  • 1
  • 7
2

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!

0

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.

turkus
  • 4,637
  • 2
  • 24
  • 28
  • Thanks I did as you said, I even printed out the staticfile_dirs when the server starts to make sure it got the correct dir. But after making an .exe with Pyinstaller it still can't find the static files. EX: static files should be in 'root/videos/static' but when I put them there it can't find it... Even though the printout says it should look exactly there. – Curtwagner1984 Aug 29 '16 at 13:24
  • I also tried replacing STATICFILES_DIRS with STATIC_ROOT same results... It appears that after an .exe is created it looks for the static files somewhere else... – Curtwagner1984 Aug 29 '16 at 14:35
  • Did you try to run ``python manage.py collectstatic`` when you switched to ``root/video/static``? – turkus Aug 29 '16 at 15:11
  • I did, and it copied the static files from admin to this folder. BUT it still can't find any of the static files inside this folder (I'm not sure where it is looking for them) I think it's worth noting that without Pyinstaller everything works fine. When running from pycharm Django has no trouble whatsoever to find the static files in `root/video/static` – Curtwagner1984 Aug 29 '16 at 16:40
  • And if fire up collectstatic, does it display path, where they are located? – turkus Aug 29 '16 at 16:43
  • It displays the path to where it will copy the admin static files and it's `root/video/static` but for some reason, it doesn't search there when the site tries to load. All I get is 404s. – Curtwagner1984 Aug 29 '16 at 18:16
  • Try to put in: ``STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), 'here/your/full/path']`` full path to static folder as in this example. – turkus Aug 29 '16 at 18:36
  • When you say full path do you mean absolute path? Like `c:\django\mysite\video\static`? – Curtwagner1984 Aug 30 '16 at 11:09
  • @Curtwagner1984 yes. – turkus Aug 30 '16 at 14:09
  • But in the printout it looks for it in the correct path. Also if I use a hardcoded path it will only work on a specific dir. – Curtwagner1984 Aug 31 '16 at 11:21
  • Yes, because it has to. Because you have only one static file dir, where you could have subdirectories and files. – turkus Aug 31 '16 at 12:06
  • Yes, I understand that, but that path should be relative to the root. And not absolute... Or am I missing something ? Also in the printout it gets the right folder path, but still for some reason isn't looking in it. Have you successfully used Pyinstaller on Django and get it to see the static files in the way you suggested ? – Curtwagner1984 Aug 31 '16 at 15:20
  • Show me how you understand relative to the root please. – turkus Aug 31 '16 at 15:23
  • Like this `os.path.join(BASE_DIR, "static")` so no matter where you have this app it will look for static in 'root/static/. If you put it in 'h:\django\project\my_app' it will look for static files in 'h:\django\project\my_app\static'. But this isn't the problem because as I said, it prints out the **absolute** path and still doesn't find the static files there. It says it looks in `c:\django\my_app\static` and the files are there but it still doesn't see them for some reason. Did you manage to use Pyinstaller successfully and get it to see the static files ? – Curtwagner1984 Aug 31 '16 at 15:46
  • I didn't have problems within. About paths, in the last one there is ``project`` missing between ``django`` and ``my_app``, maybe that's the problem. – turkus Aug 31 '16 at 15:49
  • No.. Those are just examples from the top of my head. When I run it so it print's out the static and media dirs like so: `Static files dir is: E:\Django-github\YAPO\videos\static ` `Media files dir is: E:\Django-github\YAPO\videos\media' I later ran `python manage.py collectstatic` and it copied the admin files into the static folder mentioned above. I navigated to this path and made sure the files are there (not admin, my files) and they were. Still, it doesn't see them after Pyistaller. You say you didn't have problems with Pyinstaller it saw the static files right away ? – Curtwagner1984 Aug 31 '16 at 16:03
  • Unfortunately I cannot help you more. Hope you will find answer somewhere. – turkus Aug 31 '16 at 17:48
  • Thank you for your time. – Curtwagner1984 Aug 31 '16 at 18:36
  • Did you find a solution to getting the static files to work with pyinstaller? – user1470034 Jun 22 '18 at 15:37
0

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)