Django - 使用静态文件的奇怪行为 [英] Django - Strange behavior using static files
问题描述
问题的简历如下...当我使用第三方应用程序(Haystack)的静态视图我无法使用静态文件。
我的项目有这个目录结构:
1001empbr(项目文件夹的名称)
|
| ------ 1001emp(django项目的名称)
|
| ------ 1001empbr(我的应用程序的名称)
| ------ site_media(带有静态文件的文件夹CSS / JPG / GIF)
| ---- - 模板(带模板的文件夹)
当我使用这样的urlpatterns(urls.py)很棒:
import os.path#Para poder suportar静态文件
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导入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 < *)$','django.views.static.serve',{'document_root':settings.MEDIA_ROOT}),#支持静态文件
url(r'^ $',main_page),
(r'^ iframe / $',i_frame),
)
是服务器活动:
发现错误
Django版本1.3.1,使用设置'emp1001.settings'
开发服务器正在运行http://127.0.0.1:8000/
使用CONTROL-C退出服务器。
[17 / Nov / 2011 10:15:30]GET / HTTP / 1.1200 2816
[17 / Nov / 2011 10:15:30]GET /site_media/estilos.css HTTP /1.1200 6894
[17 / Nov / 2011 10:15:30]GET /site_media/pgiframe.css HTTP / 1.1200 1345
[17 / Nov / 2011 10:15:31 ]GET /site_media/logo1.gif HTTP / 1.1200 4358
[17 / Nov / 2011 10:15:31]GET /site_media/fundo1.jpg HTTP / 1.1304 0
[ 17 / Nov / 2011 10:15:31]GET /site_media/form.gif HTTP / 1.1304 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.1200 124
[17 / Nov / 2011 10:16:20]GET /site_media/banner.jpg HTTP / 1.1200 12538
[17 / Nov / 2011 10:16:20]GET /site_media/logo2.gif HTTP / 1.1200 3418
[17/11 / 2011 10:16:20]GET /site_media/imgs/fundo1.jpg HTTP / 1.1404 1753
[17 / Nov / 2011 11:20:06]GET / HTTP / 1.1200 2816
当我使用干草堆的静态视图时:
import os.path#Para poder suportar static files
from django.conf.urls.defaults import pattern,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}),#支持静态文件
url(r' $',main_page),
(r'^ iframe / $',i_frame),
)
有些时间浏览我得到这个:
发现错误
Django版本1.3.1 ,使用设置'emp1001。设置'
开发服务器正在运行http://127.0.0.1:8000/
使用CONTROL-C退出服务器。
[17 / Nov / 2011 11:50:25]GET / resultados /?q = m%C3%A9dico HTTP / 1.1200 10634
[17 / Nov / 2011 11:50:25 ]GET /site_media/estilos.css HTTP / 1.1500 85526
[17 / Nov / 2011 11:50:26]GET /site_media/pgiframe.css HTTP / 1.1500 85548
[ 17 / Nov / 2011 11:50:26]GET /site_media/logo2.gif HTTP / 1.1500 85067
[17 / Nov / 2011 11:50:27]GET /site_media/banner.jpg HTTP /1.1500 85315
由< bound方法启动的线程中的未处理的异常< django.contrib.staticfiles.management.commands.runserver.Command对象的命令的.ntr_run在0x1225ed0>>
sys.excepthook中的错误:
追溯(最近的最后一次调用):
文件/usr/lib/python2.6/site-packages/abrt_exception_handler.py,第147行, <拉姆达>
sys.excepthook = lambda etype,value,tb:handleMyException((etype,value,tb))
TypeError:'NoneType'对象不可调用
原始异常是:
追溯(最近的最后一次呼叫):
文件/home/andre/python_virtualenv/lib/python2.6/site-packages/django/core/management/commands/runserver.py,第107行,in inner_run
run(self.addr,int(self.port),handler,ipv6 = self.use_ipv6)
文件/home/andre/python_virtualenv/lib/python2.6/site-packages /django/core/servers/basehttp.py,第696行,运行
httpd.serve_forever()
文件/usr/lib64/python2.6/SocketServer.py,第224行, serve_forever
r,w,e = select.select([self],[],[],poll_interval)
AttributeError:'NoneType'对象没有属性'select'
任何关于应该导致这种情况的线索?
最好的问候, p>
我不知道这是否会修复问题,但你的url模式看起来有点困惑。每个 urlpatterns
对象应该只有一个 前缀
字符串作为其第一个参数。您有'haystack.views'
,然后稍后''
。
您是在您的网址格式中传递可调用对象而不是字符串,因此使用空字符串''
作为您的前缀是正确的。
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}),#支持静态文件
url(r'^ $',main_page),
(r'^ iframe / $',i_frame),
)
I'm new to Django. I'm getting insane trying to understand what is going on with static files(css's and images).
The resume of the problem is the following... when I use static views from a 3rd party App(Haystack) I can't use static files.
My project have this directory structure:
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)
When I use urlpatterns(urls.py) like this works great:
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),
)
Here is the server activity:
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
When I use with the static views from 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),
)
Some after time browsing I get this:
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'
Any clue about what should be causing this?
Best Regards,
I don't know whether this will fix the issue, but your url patterns look a little confused. Each urlpatterns
object should only have one prefix
string as its first argument. You have 'haystack.views'
and then later ''
.
You are passing callable objects instead of strings in your url patterns, so using the empty string ''
for your prefix is fine.
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),
)
这篇关于Django - 使用静态文件的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!