When I set DEBUG=False in my settings file in django 1.5, I no longer have access to the STATIC_URL or any of the other variables that should be loaded by my TEMPLATE_CONTEXT_PROCESSORS in my django templates. Oddly, everything works when DEBUG=True. For what its worth, I definitely have 'django.core.context_processors.static' in my TEMPLATE_CONTEXT_PROCESSORS so that is not the issue. I have also checked a few other variables in my template context and none of the other nothing seems to be there. MEDIA_URL? nope. request? nope. See this example on github (which has been updated with solution), but these are the important pieces that correctly work when DEBUG=True and throw a 500 error when DEBUG=False:
# settings.py
from django.conf.global_settings import *
# ...
TEMPLATE_CONTEXT_PROCESSORS += (
'django.core.context_processors.request',
)
# believe it or not, 'django.core.context_processors.static' is in there
print TEMPLATE_CONTEXT_PROCESSORS
# views.py
from django.template import RequestContext
from django.shortcuts import render_to_response
def wtf(request):
return render_to_response(
"wtf.html", {},
context_instance=RequestContext(request)
)
Does something special happen in django 1.5 when you turn off debug mode? Any suggestions for fixing and/or debugging the problem would be greatly appreciated!
Looks like there was a change between Django 1.2 and 1.3.
You now have to include django.core.context_processors.static in your TEMPLATE_CONTEXT_PROCESSORS if you want the STATIC_URL available to your template outside of debug mode.
You also need to ensure you're using a RequestContext instance when rendering the template.
This can be fixed by editing the ALLOWED_HOSTS variable in your settings.py. See this answer for more details.
To get this to work on localhost, for example, set ALLOWED_HOSTS = ['localhost'].
Related
In my Django project.
I have a requirement of settings.py:
in my settings.py, there is a CORS_ORIGIN_WHITELIST variable:
CORS_ORIGIN_WHITELIST = (
'http://10.10.10.102',
'http://10.10.10.102:8000',
'http://10.10.10.102:8080',
'http://10.10.10.102:8081',
'http://10.10.10.102:8888',
'http://10.10.10.103',
'http://10.10.10.103:8000',
'http://10.10.10.103:8080',
'http://10.10.10.103:8081',
'http://10.10.10.103:8888',
.....
I want to set it flexibly, I mean I can in my user interface for add/update/delete the items.
but it is settings up in settings.py, how can I implements my requirement?
Out-of-the-box django does not support changing settings dynamically, but you can achieve it by using django constance plugin.
I have tried using https://docs.djangoproject.com/en/dev/topics/http/views/ tutorial, but still I am getting the standard 404 html page. I want to switch in to my custom view
handler404 = 'myview.views.custom_page_not_found' ,
I did debug it (using eclipse), then the value of handler404(old value -'django.config.default.views.page_not_found) is changed to the new value I have given ('myview.views.custom_page_not_found'). But it's still showing the older 404 page. And I have changed settings.py DEBUG into False then it shows the custom page. But it got some disadvantages (it won't load static files and all, DEBUG = false is not the right way) so I had to reset to True.
Do I have to make some other modification for implementing this?
I think you can not change the 404 page in DEBUG = True mode without difficulty.
There is a hint in the documentation (https://docs.djangoproject.com/en/dev/topics/http/views/#the-404-page-not-found-view):
If DEBUG is set to True (in your settings module), then your 404 view will never be used, and your URLconf will be displayed instead, with some debug information.
Try adding this to the bottom of your main urls.py:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^404/$', TemplateResponse, {'template': '404.html'}))
Swap out the 404.html to the appropriate template you use, I believe 404.html is the default though. Then with debug=True you can test out your 404 page.
If you want to Test it out with Debug=True then you need this at the bottom of your main urls.py instead:
#Enable static for runserver with debug false
from django.conf import settings
if settings.DEBUG is False: #if DEBUG is True it will be served automatically
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
When running with DEBUG = False, don't forget to collect static:
python manage.py collectstatic
Hope this helps, Cheers!
In django how to run /admin interface as well as customized admin index page. My template dirs is followed below.
TEMPLATE_DIRS = (
PROJECT_PATH + '/templates/',
)
And...
ADMIN_MEDIA_PREFIX = '//admin/'
If i will comment this line my other functions would not work, if i put it uncommented then ma admin interface shows my specified file.
What should i do to run both simultaneously. Thanks in advance
Leave TEMPLATE_DIRS alone, that affects more than just the admin, and that's not your problem anyways.
The way to override any admin page is to include the associated template from the default Django admin templates in your own 'yourproject/templates/admin' directory, and make the necessary modifications.
See the documentation: https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#overriding-admin-templates
I have a mostly entirely plain django project, with no adding of my own media or customization of the admin interface in any way. Running the server with python manage.py runserver results in a nicely-formatted admin interface. Running the server with gunicorn_django does not. Why is this the case, and how can I fix it?
It's definitely an issue of not finding the css files, but where are they stored? I never configured this, and the MEDIA_ROOT setting is ''.
EDIT: I just want to know how django-admin serves the non-existent admin files... and how can I get gunicorn_django to do the same?
If you use contrib.static, you have to execute a collectstatic command to get all the app-specific static files (including admin's own) into the public directory that is served by gunicorn.
I've run into this problem too (because I do some development against gunicorn), and here's how to remove the admin-media magic and serve admin media like any other media through urls.py:
import os
import django
...
admin_media_url = settings.ADMIN_MEDIA_PREFIX.lstrip('/') + '(?P<path>.*)$'
admin_media_path = os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^' + admin_media_url , 'django.views.static.serve', {
'document_root': admin_media_path,
}, name='admin-media'),
...
)
Also: http://djangosnippets.org/snippets/2547/
And, of course, #include <production_disclaimer.h>.
I think the easiest way is to add alias to nginx (are you using one?!) configuration file:
location /static/admin/ {
alias /<path_to_your_admin_static_files>/;
}
it worked immediately for me
Ok, got it. Just had to add this line to settings.py:
MEDIA_ROOT = '/home/claudiu/server/.virtualenv/lib/python2.5/site-packages/django/contrib/admin/media/'
David Wolever's answer was close, for my installation, but I think some paths may have changed in newer django. In particular I set
admin_media_path = os.path.join(django.__path__[0], 'contrib', 'admin', 'static', 'admin')
and in urlpatterns added:
url(r'^static/admin/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': admin_media_path,
}),
based on info found here: https://docs.djangoproject.com/en/dev/howto/static-files/
works for me, but more "magical" than I like.
like exposed here, one can set a MEDIA_URL in settings.py (for example i'm pointing to Amazon S3) and serve the files in the view via {{ MEDIA_URL }}. Since MEDIA_URL is not automatically in the context, one have to manually add it to the context, so, for example, the following works:
#views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
def test(request):
return render_to_response('test.html', {}, context_instance=RequestContext(request))
This means that in each view.py file i have to add from django.template import RequestContext and in each response i have to explicitly specify context_instance=RequestContext(request).
Is there a way to automatically (DRY) add MEDIA_URL to the default context? Thanks in advance.
There is a generic view for this use :
direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs)
It is not well documented (in my opinion : it doesn't tell that it uses a RequestContext), so I advise you to check out the implementation :
http://code.djangoproject.com/browser/django/trunk/django/views/generic/simple.py
I think it is what you are looking for ...
Add "django.core.context_processors.media" to your TEMPLATE_CONTEXT_PROCESSORS in the settings file.