I was following Django tutorial, and got stuck where it asked me to replace the default template for administrative part of the site with my own. The problem was a typo in the template's name. I suspected there must be a problem like that, but to troubleshoot the problem it'd be very helpful to see some kind of report from Django on what template it used to render a particular page. Is there any way to do this?
First if you have set DEBUG = True django automatically gives you information about where django was looking for templates (in general and especially in case it didn't find one)
You should see something like this:
second you can add the popular django plugin django-debug-toolbar. It can show you for each request what templates were used and what their context was.
see: https://django-debug-toolbar.readthedocs.io/en/stable/panels.html#template
Still, not exactly the answer, but something to get me closer. One could start Django shell, and then try this:
>>> from django.template.loader import get_template
>>> get_template('template/name').origin.name
to find out what template was actually used. This is still not enough to see though which templates were considered while resolving the template.
Related
This is a more-general question – just hoping to find someone who already knows.
("To save my forehead," y'know.) ;-)
I want to use CKEditor in conjunction with Machina forums, and I specifically want to be able to "drag and drop" images. I've found the right CKEditor feature to do that, but I'm getting "Incorrect Server Response" messages from CKEditor when I try to accomplish the drop. (This also happens on my development box.)
(Note that my concern is quite-particular to Django ("django-ckeditor"), and to the Machina forum software ("django-machina"). I need answers that are very tightly focused upon this use-case.)
So is there anyone out there who might say – "oh yeah, that happened to me, too, and the way to fix it is ...?"
Well, what do you know? I figured it out.
The problem was – and, as the django-ckeditor documentation clearly states, the default urlpattern entries (in the include file) specify a "staff-only" decorator for uploads. So, ckeditor was getting an error-message response and of course it didn't know what to do.
To solve the problem:
First, of course, be sure that ckeditor_uploader (as well as ckeditor) is installed on your system and is in your INSTALLED_APPS list in settings.py.
Now, in your urls.py, first add this line near the top:
from ckeditor_uploader import views as uploader_views
Next, insert the urlpattern entries that you find in the package's urls.py file, but referencing the uploader_viewsalias, viz:
url(r'^ckeditor/upload/',
uploader_views.upload, name='ckeditor_upload'),
url(r'^ckeditor/browse/',
never_cache(uploader_views.browse), name='ckeditor_browse'),
If you erroneously attempt to specify ckeditor_uploader.views. in the url() entry, you will be rewarded with:
NameError: name 'ckeditor_uploader' is not defined
Now you know! :-)
Also, don't forget what the Machina documentation told you to remember: ;-)
MACHINA_MARKUP_WIDGET = 'ckeditor_uploader.widgets.CKEditorUploadingWidget'
If you're doing "drag and drop," you're necessarily doing "file uploads," so you must use the provided field-types or (equivalently ...) the provided widgets from the ckeditor_uploader app.
Usually I have the opposite problem, Django can't find the templates.
Lastly, I change a template of mine called custmber_view.html, but Django didn't notice the chagnes.
I tried flushing all caches, without success. In my dispair, I completely removed the template
hoping to see the familiar TemplateDoesNotExist exception.
To my surprise, Django keeps rendering the template! Although it is not found on the hard-drive.
Can someone suggest a solution to this mystery?
ARGH, legacy projects, I am doomed to fix them for ever. Somewise guy put the following in settings.py:
TEMPLATE_DIRS = (
os.path.join(project_dir, u'templates'),
u'/var/www/venv2.7/lib/python2.7/site-packages/backoffice/templates',
)
I feel like this now ...
So, conclusion, If you are working on a project which was not made as a Django App, always make sure you read the variable TEMPLATE_DIRS.
But for your colleagues future (and for better moral) always follow How to write reusable Django app
I want to check the useragent using Django inside my template. I know this is possible using JavaScript, but I wanted a server side solution.
I know I can use HttpRequest.META in some middleware class, which I am not currently looking for. I want to determine this using some code in the template itself, without any dependency on other files / classes.
Can anybody help?
You need to use context processors, more specifically django.core.context_processors.request.
This SO answer covers it quiet well:
How can I pass data to any template from any view in Django?
Especially this blog post, that is referenced in the SO answer:
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/
I am building a Django application that is a pretty basic blog, so far it has been wonderful. I got comments, tags etc up. But one thing is bugging me: I cant get the sidebar i want to work. I use the django.views.generic.date_based generic view and this is my urls.py for the blog:
urlpatterns = patterns('django.views.generic.date_based',
(r'(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail',dict(info_dict, slug_field='slug',template_name='blog/detail.html')),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', dict(info_dict, template_name='blog/list.html')),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>w{1,2})/$','archive_day',dict(info_dict,template_name='blog/list.html')),
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$','archive_month', dict(info_dict, template_name='blog/list.html')),
(r'^(?P<year>\d{4})/$','archive_year', dict(info_dict, template_name='blog/list.html')),
(r'^$','archive_index', dict(info_dict, template_name='blog/list.html')),
)
When i use the URL with 'archive_index' passed i can easily print the latest entries for my sidebar, but when i enter a post i will use one of the top ones where only "object_detail" is availabe. This makes my sidebar entries dissapear. What is the best solution to this problem? Is there a way to make some objects available globally? Through views or otherwise.
People do things like that with template tags. The documentation for custom template tags might be helpful, and there's also a great little tutorial here.
Alternatively, you can use context processors - but that adds an overhead to every single request, which may not be necessary.
I've produced a few Django sites but up until now I have been mapping individual views and URLs in urls.py.
Now I've tried to create a small custom CMS but I'm having trouble with the URLs. I have a database table (SQLite3) which contains code for the pages like a column for header, one for right menu, one for content.... so on, so on. I also have a column for the URL. How do I get Django to call the information in the database table from the URL stored in the column rather than having to code a view and the URL for every page (which obviously defeats the purpose of a CMS)?
If someone can just point me at the right part of the docs or a site which explains this it would help a lot.
Thanks all.
You dont have to to it in the flatpage-way
For models, that should be addressable, I do this:
In urls.py I have a url-mapping like
url(r'(?P<slug>[a-z1-3_]{1,})/$','cms.views.category_view', name="category-view")
in this case the regular expression (?P<slug>[a-z1-3_]{1,}) will return a variable called slug and send it to my view cms.views.category_view. In that view I query like this:
#render_to('category.html')
def category_view(request, slug):
return {'cat':Category.objects.get(slug=slug)}
(Note: I am using the annoying-decorator render_to – it is the same as render_to_response, just shorter)
Edit This should be covered by the tutorial. Here you find the url-configuration and dispatching in every detail. The djangobook also covers it. And check pythons regex module.
Of course you can use this code.
Your question is a little bit twisted, but I think what you're asking for is something similar to how django.contrib.flatpages handles this. Basically it uses middleware to catch the 404 error and then looks to see if any of the flatpages have a URL field that matches.
We did this on one site where all of the URLs were made "search engine friendly". We overrode the save() method, munged the title into this_is_the_title.html (or whatever) and then stored that in a separate table that had a URL => object class/id mapping.ng (this means it is listed before flatpages in the middleware list).