Combining multiple Django Views/Templates - django

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.

Related

What is the right way to organize a Django Project?

I'm new to Django and I'm trying to wrap my head around how these apps are supposed to be organized. I have the following questions:
Are apps like divs that are generated separately?
Can we have apps within apps?
Can we have apps that when clicked on, change other apps with javascript?
Right now I just have one views.py file and it loads all of its content through different function calls.
So right now I'm faced with if I should break up my views.py into smaller apps.
Am I going about Django the correct way?
Are apps defined like the they are in picture below, or are apps supposed to act more like a page?
What if I want a header, breadcrumbs, and footer for all my pages? I'm super confused #.#
Apps have nothing whatsoever to do with divs. Django is not a CMS (although it can be used to create CMSs) and doesn't dictate the layout of your templates.
The usual way to handle different blocks on the page that need different logic to populate them is via custom template tags. James Bennett has a good writeup on this, although the syntax is rather out of date so refer to the first link for that.

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 - Static content display based on URL

I'm working on a Django site with a basic three column design. Left column navigation, center column content and right column URL specific content blocks.
My question is about the best method of controlling the URL specific content blocks in the right column.
I am thinking of something along the lines of the Flatpages app that will make the content available to the template context if the URL matches a pre-determined pattern (perhaps regex?).
Does anyone know if such an app already exists?
If not, I am looking for some advice about the best way to implement it. Particularly in relation to the matching of patterns to the current URL. Is there any good way to re-use parts of the Django URL dispatcher for this use?
Django CMS is a good suggestion, it depends on how deep you want to go. If this is just the beginning of different sorts of dynamic content you want then you should go that way for sure.
A simple one-off solution would be something like this:
You would just need to write a view and add some variables on the end of the URL that would define what showed up there. Depending on how fancy you need to get, you could just create a simple models, and just map the view to the model key
www.example.com/content/sidecontent/jokes/
so if "jokes" was your block of variable sidecontent (one of many in your sides model instances) the urls.py entry for that would be
(r'^content/sidecontent/(?P<side>)/$,sides.views.showsides),
and then in your sides app you have a view with a
def showsides(request, side):
Sides.objects.get(pk=side)
etc...
For something like this I personally would use Django CMS. It's like flatpages on steroids.
Django CMS has a concept of Pages, Templates, and Plugins. Each page has an associated template. Templates have placeholders where you can insert different plugins. Plugins are like mini-applications that can have dynamic model-based content.
Although Django-CMS is an interesting suggestion, there are quite a few projects that do specifically what you've requested - render blocks of content based on a URL. The main one that I know about is django-flatblocks.

Multiple views loaded into a template

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.

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.