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),
)
来源:https://stackoverflow.com/questions/8171955/django-strange-behavior-using-static-files