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).
Related
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.
So, I've recently inherited a large code base that is fairly obfuscated. When I navigate a page on my local machine is there any way to determine which template/view is actually being called to create the view that I'm seeing at that moment?
I would like to get a better idea of where certain parts of the page are actually coming from, but the project is so large and disorganized that going through present templates is simply not feasible.
Is there any nice way to get around this? Worth mentioning that the defined urls all seem to be poorly written, obfuscated regex, (not to mention incredibly long) so direct examination of the urls file is not extremely feasible.
When I try to run resolve on the url of the page I'm trying to view I get a 404, and I'm not really sure where to progress from there, since the page clearly works.
Any help would be greatly appreciated.
Personnaly I use this : https://github.com/django-debug-toolbar/django-debug-toolbar
The Django Debug Toolbar is a configurable set of panels that display
various debug information about the current request/response and when
clicked, display more details about the panel's content.
Currently, the following panels have been written and are working:
Django version
Request timer
A list of settings in settings.py
Common HTTP headers
GET/POST/cookie/session variable display
Templates and context used, and their template paths
SQL queries including time to execute and links to EXPLAIN each query
List of signals, their args and receivers
Logging output via Python's built-in logging, or via the logbook module
There is also one Django management command currently:
debugsqlshell: Outputs the SQL that gets executed as you work in the Python interactive shell. (See example below)
If you have ideas for other panels please let us know.
Note: The Debug Toolbar only works on Django 1.3 and newer.
0 code to add, only a few minor changes to settings.py
You will get what you want and even more.
I have a new job and a huge django project (15 apps, more than 30 loc). It's pretty hard to understand it's architecture from scratch. Are there any techniques to simplify my work in the beginning? sometimes it's even hard to understand where to find a form or a view that I need... thnx in advance.
When I come to this kind of problem I open up a notebook and answer the following:
1. Infrastructure
Server configuration, OS etc
Check out the database type (mysql, postgres, nosql)
External APIS (e.g Facebook Connect)
2. Backend
Write a simple description
Write its input/output from user (try to be thorough; which fields are required and which aren't)
Write its FK and its relation to any other apps (and why)
List down each plugin the app is using. And for what purpose. For example in rails I'd write: 'gem will_paginate - To display guestbook app results on several pages'
3. Frontend
Check out the JS framework
Check the main stylesheet files (for the template)
The main html/haml (etc) files for creating a new template based page.
When you are done doing that. I think you are much more prepared and able go deeper developing/debugging the app. Good luck.
Use this http://packages.python.org/django-extensions/graph_models.html
to generate the Relationship diagrams from the models so that you can visually see how the models are related to each other. This will give you nice idea about the app
1) Try to install the site from scratch. You will find what external apps are needed for the site to run.
2) Reverse engineer. Browse through the site and try to find out what you have to do to change something to that page. Start with the url, look up in urls.py, read the view, check the model. Are there any hints to other processes?
3) Try to write down everything you don't understand, and document the answers for future reference.
I would clone the project so you can mess up endlessly.
Then I would start to reduce the code. "What happens if if just remove this function here?
Also get django debug toolbar:
https://github.com/django-debug-toolbar/django-debug-toolbar
A good terminal debugger is also golden, there are many out there, here is an example:
https://github.com/tomchristie/django-pdb
This allow you to halt the code and even inject and mutate parameters in runtime. Just like GDB in C.
If you use FireFox you can install FireBug on it and when you for example submit ajax form you can see at which url send you request after what you can easily find controller which work with this form data. At chrome this utility embedded by default and call by F12 key.
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.
I'm supposed to build some Django apps, that allow you to administer multiple sites through one backend. The contrib.sites framework is quite perfect for my purposes. I can run multiple instances of manage.py with different settings for each site; but how should django's admin deal with different settings for different sites, eg. if they have different sets of languages, a different (default) language? So there are some problem s to face if you have to work on objects coming from different sites in one admin...
I think settings.ADMIN_FOR is supposed to be quite helpful for cases like this, but theres hardly any documentation about it and I think it's not really used in the actual Django version (?).
So any ideas/solutions are welcome and much appreciated!
Thanks a lot...
There is an old blog post by James Bennet which might be helpful:
Create a new Site object in your admin for each domain, and put the id of that Site into its settings file as SITE_ID so Django knows which site in the database corresponds to this settings file.
In the settings file for your original site (the one with id 1), add the other sites’ settings files to the ADMIN_FOR setting, to let Django know that this one instance of the admin application will handle all of the sites.
As documented ADMIN_FOR (for which i can not post link) should be a tuple of settings modules much like INSTALED_APPS is a tuple of django app modules.
Note that blog post is from 2006 so it uses a bit outdated API.