0

I've managed to get my applications to compile, but anytime it references a static file I receive a 404 error on my javascript files. I verified they do exist in the dist application.

My application structure is setup as the following:

enter image description here

I've added my static files to the datas of the .spec file as shown below:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['manage.py'],
             pathex=['C:\\python\\dsssecurity'],
             binaries=[],
             datas=[('mysite/static','static_root'),('mysite/search/templates','mysite/templates')],
             hiddenimports=['django_select2.apps','django.contrib.admin.apps','django.contrib.auth.apps','django.contrib.contenttypes.apps','django.contrib.sessions.apps','django.contrib.messages.apps','django.contrib.staticfiles.apps','django_filters.apps','widget_tweaks.apps','django.contrib.messages.middleware.apps','django.template.loaders.apps'],
             hookspath=['C:/Python/dsssecurity'],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='dsssecurity',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='dsssecurity')

I've also added the static settings to my mysite > search > urls.py as shown below:

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


urlpatterns = [
    url(r'^results/$', views.results, name='results'),
    url(r'^submitted/$', views.submitted, name='submitted'),
    url(r'^update/$', views.update, name='update'),
    url(r'^submittedupdate/$', views.submittedupdate, name='submittedupdate'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

my output of the console shows:

[22/Jun/2018 14:24:02] "GET /search/?employeentname=&employeelastname=&qv_statusid=1&employee_status_id=1&title=&coid= HTTP/1.1" 200 841390
[22/Jun/2018 14:24:10] "POST /search/results/ HTTP/1.1" 200 36667
Not Found: /static/search/selectallrep.js
Not Found: /static/search/selectall.js
Not Found: /static/search/rolebased.js
[22/Jun/2018 14:24:10] "GET /static/search/selectall.js HTTP/1.1" 404 2354
Not Found: /static/search/buttonsubmit.js
[22/Jun/2018 14:24:10] "GET /static/search/selectallrep.js HTTP/1.1" 404 2363
[22/Jun/2018 14:24:10] "GET /static/search/rolebased.js HTTP/1.1" 404 2354
[22/Jun/2018 14:24:10] "GET /static/search/buttonsubmit.js HTTP/1.1" 404 2363
Not Found: /static/search/buttonsubmit.js
[22/Jun/2018 14:24:10] "GET /static/search/buttonsubmit.js HTTP/1.1" 404 2363
Not Found: /static/search/selectall.js
[22/Jun/2018 14:24:10] "GET /static/search/selectall.js HTTP/1.1" 404 2354
Not Found: /static/search/selectallrep.js
[22/Jun/2018 14:24:10] "GET /static/search/selectallrep.js HTTP/1.1" 404 2363
Not Found: /static/search/rolebased.js
[22/Jun/2018 14:24:10] "GET /static/search/rolebased.js HTTP/1.1" 404 2354

I've followed instructions shown here:

Location of static files when creating a Django exe using pyinstaller

and

https://github.com/pyinstaller/pyinstaller/issues/2348

My static_url and staticfiles_dirs in settings is defined as:

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'mysite/static'),
)

My application works fine when running manage.py stand alone. What am I missing to get my static files to be recognized in my pyinstaller executable?

user1470034
  • 671
  • 2
  • 8
  • 23

1 Answers1

1

Try this.. Worked for me. In Your base URL add this.

urls.py

 from django.contrib.staticfiles.urls import staticfiles_urlpatterns
 urlpatterns = patterns('',
 # ...
 )+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns() # Added this

In settings.py

   #..... 
   STATIC_URL = '/static/'
   STATICFILES_DIRS = [
   os.path.join(BASE_DIR, "myapp/static") # my static files are located in myapp/static
   ]
   # ....

In .spec (VERIFY IDENTATION)

# -*- mode: python -*-

block_cipher = None

a = Analysis(......)

##### include mydir in distribution #######
def extra_datas(mydir):
    def rec_glob(p, files):
        import os
        import glob
        for d in glob.glob(p):
            if os.path.isfile(d):
                files.append(d)
             rec_glob("%s/*" % d, files)
    files = []
    rec_glob("%s/*" % mydir, files)
    extra_datas = []
    for f in files:
    extra_datas.append((f, f, 'DATA'))
return extra_datas
a.datas += extra_datas('myapp')
a.datas += extra_datas('db.sqlite3')
###########################################

pyz = PYZ(.....)
exe = EXE(.....)
coll = COLLECT(....)
black_coder
  • 76
  • 3
  • 8