Multiple views loaded into a template - django

Hay guys, in PHP when i want to load a differnet page (render, with all variables outputting) i would use
include 'mypage.php'
This would load the page up and render the output.
How do i do something like this in Django? My problem is that i have a "quick list" which lists a bunch of popular items. I want this list to appear on every page, even if i don't call the object.
any ideas?
EDIT: The view is called shop.app.popular_items

You can use Inclusion tags.

Use template context_processors

To answer your question to becomingGuru on when to use context processors and when to use inclusion tags, remember that context processors are run on every template render. So they should only be used when you know you will always need the value.

Edit:
After Daniel's answer and as you said you want to have it on every page,
becomingGuru's answer is probably the best solution.
This does not belong into the view (if you not want to display this as a standalone page).
This should go into a custom template tag.

Related

Determining parent page of template

Inside a Grails GSP template (e.g., _form.gsp), how can I determine what page is calling the template (e.g., create.gsp or edit.gsp) so that different fields can be displayed?
There is no general way, no. If you just know that in your application that the foo action always renders the foo view and there aren't exceptions to that, then of course you can just look at the action name. You could also do something simple like put something in the model when rendering the template, like <g:render template="foo" model="[parentPage:'edit']"/>, or set a variable in pageScope or similar. There are ways to accomplish what you want, but no general way in a template to answer the question "What GSP rendered this template?". I hope that helps.
So I googled it without any mention of templates (gsp determine current page) and found the answer on this Nabble thread. I will use params.action to determine what action I'm currently doing (e.g., create or edit), and use that to display different fields.
Cheers!

Combining multiple Django Views/Templates

I am working on a website. On the Homepage, I want to show the posts in the center and show famous tags on the right side of the page. Now, posts and tags are two different apps and their views/tempaltes are calculated/generated in two separate functions.
How can I show what I want on my Homepage keeping my mind that its two different views being called. As far as I understand, I can only call a single view to show my homepage.
This must be possible but I am unable to understand this. Any help will be much appreciated.
I think you need to create a custom template tag that in this case called: Inclusion tags
You can look into writing a custom template tag for the tags. That way the tag logic can stay in the right application.
It depends how these different apps render the templates. If they just render the template without extending from a base template you could simply use AJAX to load all the content into your homepage.
If not, then there is no other way than writing a custom solution for this. This could be an extension to your views, a custom template tag as already mentioned or something else according to what exactly you need.

Django : render a view in other views

I'd like to "include" a view in all my other views.
Look at my website : www.urlc.be
The "Classement" on the right is displayed on every page but I don't know how to include it the "django style". Right now I'm using AJAX call to display the "Classement" on each page.
Can you help me find a better solution ?
Thanks.
If you need the table on every page i would use an inclusion tag and include it in the base template that all relevant pages inherit from.
There are several approaches you can take to that. One is to write some middleware to add some more data to each response as it leaves the view. The other would be to write your own template tag to render the section in question. Look here for middleware and here for template tags.

Django -- Break up a request.path into each word?

I want to take the URL from request.path in a template and do a test on just the first word. If my URL is this:
/estimates/commercial
I want to somehow remove the commercial from the request.path. I was hoping this is possible within the template, too, since I need to do this check at every page.
Is this possible within my Django template?
EDIT - for clarification
The purpose of this is to highlight the navigation link based on what page is currently being viewed. I have a main nav and a sub nav and I want to highlight as follows:
main nav --> [Systems][Estimates]
"Systems" sub nav ---> [New][Details][Invoives]
If I am in the Details section of the Systems section, I want the words System and Details to be a different colour, or underlined, or something.
Your options are:
Write a ContextProcessor
Write a custom template filter. I'd reccomend just writing a split template filter and then using the join and slice filters to get the desired affect. However, it might be easier to just write a filter that does the whole thing.
Truthfully I'd consider #1 to be the better option. You are using context processors already right? If not, now is the time =)
def somename(request):
return { 'some_context_var': request.path.split('/')[:-1] }
Edit:
If your path was /something/foo/bar/estimates/commercial/
def somename(request):
primary, secondary = request.path.split('/')[-2:]
return { 'primary_name': primary, 'secondary_name': secondary }
would give you 2 context vars with 'estimates' and 'commercial' as their values. This idea is pretty easy to expand, or even make more abstract and allow an arbitrary number of context variables being added.
You could use django-treemenus, which can be extended in many ways. In the manual, there is also an example how to highlight a navigation item if the user is within a certain path.
Without writing your own custom template filter, it'd be easier, I think, to do this in the view code and pass the relevant portion into the template.

How can I pass a standard, static variable from all views in Django?

I'm working on a blog application, and I want to have a sidebar that includes a list of all the months the blog has been in existence, to provide links to archives pages. Moreover, I'd like to make this automatically update when the month changes, rather than hardcoding it in the template. Of course, as far as I can tell, this means that I'll have to calculate the list of months in every view, and pass it into every template from every view.
I'd like to avoid this, if possible. Is there a way to calculate the list once and automatically apply it to every template, without having to explicitly pass it into the template from every view?
There are a few possible solutions to your problem.
If you really want to have this on every page on your site a context processor is probably your best choice. Context processors are basic way to inject data into all template contexts. Be aware however that the context processor will be called on every request.
An alternative solution would be to create a custom template tag and use it on a shared base template for all of the pages you wish to have your sidebar. Template tags are a bit more complex to create but they are more flexible.
With either solution you should also look at Django's cache framework. The cache framework makes it pretty easy to temporarily store your calculated values for a while to save some work on each request.
You want a template context processor
Django - having middleware communicate with views/templates
http://docs.djangoproject.com/en/dev/ref/templates/api/?from=olddocs#id1
Django's template inheritance should cover this. You could create a base template that handles your sidebar functionality. Your other views extend this template.
Template Inheritance:
http://www.djangobook.com/en/1.0/chapter04/#s-template-inheritance
A combination of custom template tags as mentioned previously and template fragment caching should do the trick.