Pyinstaller编译但Django静态javascript文件出现404错误

时间:2018-06-22 19:31:02

标签: django pyinstaller django-staticfiles

我设法使我的应用程序得以编译,但是只要它引用了静态文件,我的javascript文件都会收到404错误。我确认它们确实存在于dist应用程序中。

我的应用程序结构设置如下:

enter image description here

我已将我的静态文件添加到.spec文件的数据中,如下所示:

# -*- 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')

我还将静态设置添加到我的mysite>搜索> urls.py中,如下所示:

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)

我的控制台输出显示:

[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

我已按照此处显示的说明进行操作:

Location of static files when creating a Django exe using pyinstaller

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

我在设置中的static_url和staticfiles_dirs定义为:

STATIC_URL = '/static/'

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

单独运行manage.py时,我的应用程序运行良好。要让我的静态文件在pyinstaller可执行文件中被识别,我缺少什么?

1 个答案:

答案 0 :(得分:0)

尝试一下。为我工作。 在您的基本URL中添加它。

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

在settings.py

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

在.spec(验证标识)中

# -*- 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(....)