Django - 使用静态文件的奇怪行为

时间:2011-11-17 17:56:05

标签: django django-haystack url-pattern

我是Django的新手。我疯狂地试图了解静态文件(css&s和图像)发生了什么。

问题的恢复如下......当我使用来自第三方应用程序(Haystack)的静态视图时,我无法使用静态文件。

我的项目有这样的目录结构:

1001empbr (name of the folder for the project)
   |
   |------ 1001emp (name of the django project)
              |
              |------ 1001empbr (name of my App)
              |------ site_media (folder with static files CSS/JPG/GIF)
              |------ templates (folder with the templates)

当我使用这样的urlpatterns(urls.py)时效果很好:

import os.path # Para poder suportar static files
from django.conf.urls.defaults import patterns, include, url
from emp1001br.views import *
from emp1001 import settings
from haystack.forms import FacetedSearchForm
from haystack.query import SearchQuerySet
from haystack.views import FacetedSearchView
import datetime

urlpatterns = patterns(
    #'haystack.views',                   
    #url(r'^resultados/$', FacetedSearchView(template='emp1001br/pgresultados.html', searchqueryset=sqs, form_class=FacetedSearchForm), name='haystack_search'),                 
    '',
    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), # To support static files
    url(r'^$', main_page),
    (r'^iframe/$', i_frame),
)

以下是服务器活动:

0 errors found
Django version 1.3.1, using settings 'emp1001.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[17/Nov/2011 10:15:30] "GET / HTTP/1.1" 200 2816
[17/Nov/2011 10:15:30] "GET /site_media/estilos.css HTTP/1.1" 200 6894
[17/Nov/2011 10:15:30] "GET /site_media/pgiframe.css HTTP/1.1" 200 1345
[17/Nov/2011 10:15:31] "GET /site_media/logo1.gif HTTP/1.1" 200 4358
[17/Nov/2011 10:15:31] "GET /site_media/fundo1.jpg HTTP/1.1" 304 0
[17/Nov/2011 10:15:31] "GET /site_media/form.gif HTTP/1.1" 304 0
[17/Nov/2011 10:16:20] "GET /iframe/ HTTP/1.1" 200 1874
[17/Nov/2011 10:16:20] "GET /site_media/close.gif HTTP/1.1" 200 124
[17/Nov/2011 10:16:20] "GET /site_media/banner.jpg HTTP/1.1" 200 12538
[17/Nov/2011 10:16:20] "GET /site_media/logo2.gif HTTP/1.1" 200 3418
[17/Nov/2011 10:16:20] "GET /site_media/imgs/fundo1.jpg HTTP/1.1" 404 1753
[17/Nov/2011 11:20:06] "GET / HTTP/1.1" 200 2816

当我使用Haystack的静态视图时:

import os.path # Para poder suportar static files
from django.conf.urls.defaults import patterns, include, url
from emp1001br.views import *
from emp1001 import settings
from haystack.forms import FacetedSearchForm
from haystack.query import SearchQuerySet
from haystack.views import FacetedSearchView
import datetime

urlpatterns = patterns(
    'haystack.views',                   
    url(r'^resultados/$', FacetedSearchView(template='emp1001br/pgresultados.html', searchqueryset=sqs, form_class=FacetedSearchForm), name='haystack_search'),                 
    '',
    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), # To support static files
    url(r'^$', main_page),
    (r'^iframe/$', i_frame),
)

一些浏览后我得到了这个:

0 errors found
Django version 1.3.1, using settings 'emp1001.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[17/Nov/2011 11:50:25] "GET /resultados/?q=m%C3%A9dico HTTP/1.1" 200 10634
[17/Nov/2011 11:50:25] "GET /site_media/estilos.css HTTP/1.1" 500 85526
[17/Nov/2011 11:50:26] "GET /site_media/pgiframe.css HTTP/1.1" 500 85548
[17/Nov/2011 11:50:26] "GET /site_media/logo2.gif HTTP/1.1" 500 85067
[17/Nov/2011 11:50:27] "GET /site_media/banner.jpg HTTP/1.1" 500 85315
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x1225ed0>>
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/abrt_exception_handler.py", line 147, in <lambda>
    sys.excepthook = lambda etype, value, tb: handleMyException((etype, value, tb))
TypeError: 'NoneType' object is not callable

Original exception was:
Traceback (most recent call last):
  File "/home/andre/python_virtualenv/lib/python2.6/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
    run(self.addr, int(self.port), handler, ipv6=self.use_ipv6)
  File "/home/andre/python_virtualenv/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 696, in run
    httpd.serve_forever()
  File "/usr/lib64/python2.6/SocketServer.py", line 224, in serve_forever
    r, w, e = select.select([self], [], [], poll_interval)
AttributeError: 'NoneType' object has no attribute 'select'

有关应该导致此问题的任何线索?

最诚挚的问候,

1 个答案:

答案 0 :(得分:2)

我不知道这是否能解决问题,但你的网址模式看起来有点混乱。每个urlpatterns对象应该只有一个prefix字符串作为其第一个参数。您有'haystack.views',然后是''

您的网址格式为passing callable objects instead of strings,因此使用空字符串''作为前缀是可以的。

urlpatterns = patterns(
    '',                   
    url(r'^resultados/$', FacetedSearchView(template='emp1001br/pgresultados.html', searchqueryset=sqs, form_class=FacetedSearchForm), name='haystack_search'),                 
    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), # To support static files
    url(r'^$', main_page),
    (r'^iframe/$', i_frame),
)