Can I debug django to display which template it is rendering? - python

I use django for a large project that is new for me and has many templates. Now I was asked to edit a template that I can display in the browser but I don't know where the template is. Is there a way to make django write out which template it is rendering (and possibly which python classes are used)? Similar to what happens at an error where there is lots of information about what is used.

yes definitely, you can use django debug toolbar for this !

Related

Django is rendering template from the database efficient

This is not a HOWTO question, as it, as such, has been answered before here
I am trying to integrate Django with modern frontend framework, and I found that it is possible to store and render Django templates from models. Since it is not a standard, I am wondering what are the advantages (or disadvantages if that's the case) of file based templates.
Reading though the documentation, I have seen that it is recommended to actually cache templates and models as much as possible for best performance, so why would it not be recommended to store templates in the database? It seems very convenient to me that in doing so pages can be edited from the admin panel (where you can add a code editor), which, along with the rest framework and a front end framework synergize very well.
From my research, the template tags and template language seem to work and the context can be passed in a view as well. the only thing I cannot figure out is the {include .. } tag, as it does not seem to point to a view. (although a custom tag cam be made to have this function)
Can such a setup be used in production? or are there security/performance/other concerns?
It's not recommended because templates are the developer's responsibility, not the site admin's.
Otherwise, there's not much performance difference between reading files directly from filesystem and from database.
One drawback of this approach is you don't get revision history (using git, mercurial etc). Sure, you can implement something similar to save the revisions in the database, but then why not use the better tools which already exist?
There's a Django flatpages app which lets you save HTML content in database. But the purpose of the app is to allow site admins to edit HTML content, such as information for an "About Us" page, because writing this info is not a developer's responsibility.

Using custom forms in Django instead of modifying the source files

Let me preface this by saying I'm VERY new to Django and am also having a hard time with some of the documentation. I know that this question has surely been asked and answered a thousand times, but I can't seem to phrase my query properly.
I'm making a project that uses django-registration-redux, and I wanted to customize the template and the forms to accept additional user information. First, I noticed that my changes to the template files weren't having any effect , then I realized that it was using the template files from my Python install location instead of my actual project. I fixed this by setting the templates folder setting, but I also need to modify the registration-redux forms, and can't figure out how to override the default forms with local forms in my application.
You need not change the template settings for your existing project, but you have to make sure you have included 'registration' in the list of your INSTALLED_APPS. In the documentation its mentioned that
You can extend and customize the included templates as needed
Though its not very clear here, django registration redux is built on top of the in built django registration module. What you need to do is build your own custom registration form which is already explained in this answer.
In your case the template that you need to modify/extend is registration/registration_form.html.
Other useful resources that can help you:
http://www.tangowithdjango.com/book17/chapters/login_redux.html
https://www.youtube.com/watch?v=qkFWkOw-ByU

Django templates, how do I know which template gets called

I'm using an external app in my django project which makes use of generic views, and I'm not able to figure out which template gets called for the response.
Do you know how to do that without having to dig into the source code of the app?
May be debugging the stack trace when the template gets loaded?
The Django debug toolbar will tell you.
Use debug-toolbar when you view a page it will show you the templates used.

customizing Django look and feel in Python

I am learning Django and got it to work with wsgi. I'm following the tutorial here:
http://docs.djangoproject.com/en/1.1/intro/tutorial01/
My question is: how can I customize the look and feel of Django? Is there a repository of templates that "look good", kind of like there are for Wordpress, that I can start from? I find the tutorial counterintuitive in that it goes immediately toward customizing the admin page of Django, rather than the main pages visible to users of the site. Is there an example of a "typical" Django site, with a decent template, that I can look at and built on/modify? The polls application is again not very representative since it's so specialized.
any references on this would be greatly appreciated. thanks.
Search for generic CSS/HTML templates, and add in the Django template language where you need it. Because unless you are trying to skin a particular app (such as the admin system), there is nothing Django-specific about any of your HTML.
The fact that you're thinking in terms of Wordpress templates, and that you think the tutorial's poll application is highly specialised, are hints that you haven't really grasped what Django is. It isn't a content management system or a blog engine, although it can be used to build those things.
There's no such thing as a typical Django site, and it simply doesn't make sense to have pre-packaged templates, because the front end could be absolutely anything at all - like a poll.
You write the template like you would write any standalone HTML+CSS page, perhaps with placeholders for the content, then turn those placeholders into actual Django template tags. If you know how to do write HTML, then you know how to make a Django template.
Actually Django does not have a "look and feel". You are probably referring to the built in Django Admin application. That app comes with its own templates.
There are third party applications that can change the Admin interface, Django Grapelli is a great example.
For any other application you want to build yourself, or download. Most likely you'll have to do the templates yourself. In order to come up with something pretty you need to learn about CSS/HTML/JS and design principles as the Django Templates will quite likely be out of your way.
I always recommend HTML Dog for learning the basics of HTML, CSS and JS.

turbogears request/user object in templates and request context

I am currently making the switch from Django to Turbogears 2.1 and am running into some problems that I could not find the answers to in the Turbogears docs. If tg developers read this, let me tell you that one of the best features Django has over TG is its documentation!
1) How do I access the request (user?) object within a mako template in order to check if the user is authenticated? For instance
if (request.user.is_authenticated)
'logout link'
else
'login link'
2) A related quesiton (as the user object is exposed in Django to templates via context processors). Is there a way to add data to the request context? For instance, in my Django app I add a cached dictionary of notifications for the user if the user is logged in via a definition in a context_processors.py file and then include that def in the TEMPLATE_CONTEXT_PROCESSORS tuple in the settings file.
3) This may warrant its own question, but I thought I'd throw it in in case anyone has a quick suggestion. I'm using Netbeans as my IDE and it offers no code coloring or tools for mako files. However, trying to rename the templates with a .html extension throws a mako error. Is there any way around this or am I stuck with plain text and the .mak extension?
Thanks very much
I've moved from Turbogears 1.0 to Django. Might not be able to answer all of these, but I believe in general TG2 tries to keep things fairly similar to TG1. Hopefully pointing out how it works in TG 1, might help...
1) In Turbogears 1.0 you would use tg.identity.anonymous to see if the user was logged in or not. A quick look at the docs, shows it's most likely still the same.
2) Turbogears called this sort of thing stdvars - see here for details for TG 1 http://docs.turbogears.org/1.0/stdvars
3) Sorry don't know, but I presume you can probably just tell Netbeans to consider .mak as another extension for html files (so it uses html syntax highlighting).

Categories