How to call controller function from template in Django? - django

How to call controller function from template in Django instead of hardcoding URIs?

I guess You mean "link to it".
This can be done via {% url %} tag, see docs
If Your intent is to call view function directly, this is not possible by design - templates should not know about views, it's a separate layer.

How to call controller function from template
Not sure what you mean here. If you mean to provide an an HTML link to it see the second part of my answer below.
instead of hardcoding URIs?
In case you haven't already, use the {% url %} tag. Use this in conjunction with named URLs in your url config.

If you mean adding URL to the resulting HTML, then either use {% url %} tag as others, or get used to writing and using model_instance.get_absolute_url(). Personally I'm using the latter whenever possible, adding also custom "URL functions" like get_delete_url() etc.

Related

I don't know when to use reverse() in django.core.urlresolvers module

I finished reading the documentation for the reverse() method of the Django URL dispatcher.
When is it useful?
Thank you!
The function supports the dry principle - ensuring that you don't hard code urls throughout your app. A url should be defined in one place, and only one place - your url conf. After that you're really just referencing that info.
Use reverse() to give you the url of a page, given either the path to the view, or the page_name parameter from your url conf. You would use it in cases where it doesn't make sense to do it in the template with {% url 'my-page' %}.
There are lots of possible places you might use this functionality. One place I've found I use it is when redirecting users in a view (often after the successful processing of a form)-
return HttpResponseRedirect(reverse('thanks-we-got-your-form-page'))
You might also use it when writing template tags.
Another time I used reverse() was with model inheritance. I had a ListView on a parent model, but wanted to get from any one of those parent objects to the DetailView of it's associated child object. I attached a get__child_url() function to the parent which identified the existence of a child and returned the url of it's DetailView using reverse().
The reverse() function is used in django to achieve DRY compliant urls in your views.
Find a clearer explanation here
It's used when you want to resolve a view by name along with arguments to a URL in code. It's the backend for the {% url %} template tag.

Subrequests in Django templates

I'm working on my first Django project and have my templates setup with a base that all the others extend. In that base I want to have some user-specific navigation which means loading some values from the database to build the contents of a drop down menu. However I don't want to have to do this inside each view. Coming from Symfony2/Twig I would normally do this using a sub-request where I tell the template to render a view and that will use it's own template. Using syntax like:
{% render 'Bundle:Controller:action' with {} %}
How would I accomplish this same thing with Django? I've read over the docs a couple of times but can't find any way to do this.
You have two approaches:
(better)
- add the code to base.html (the one you're always extending) and only override it when you need to.
or
(worse)
- in every template use {% include %} to include your menus.html template.
Update: re-reading your question: you could modify the request in context-processor so your base.html would then have this information.
Custom template tags are what you want.

Reusable templates for common display elements (tables, etc.) in django

I'm building a django application that has similar-looking display elements on several different pages.
For example, the projects.html page has a table that lists projects and some related information; the documents.html page has a similar-looking table
It seems like there ought to be a way to define a "my_kind_of_table" template and then insert it into different pages as necessary:
{% proj_list | create_my_kind_of_table:name,description,last_update %}
and then...
{% doc_list | create_my_kind_of_table:name,header,owner %}
I suspect that django can already do this, but I don't know what to search for. Any suggestions?
How about {% include ... %}?
To add context variables that all tables needs, then you have three solutions to pick: One is what I would call the "old fashioned" way, and is to have special function that is called by all views that needs to add context function. The second is to create a function decorator, that is used on the function that returns the response. The third way can be used if you use the new 1.3 class-based views, then you can create a mixin-class that your view-class inherits, and that adds these things in its own get method.
Custom template tags and filters will accomplish this: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

How to structure a website in Django?

I have a simple (I hope so) question about the structure of a typical Django website. I have to restructure my site using some "Django-powered" pages mixed to other typical static pages.
I'd like to have a few templates and a lot of pages using these templates.
My question is:
What is the best way to provide different content for each page?
What I'm thinking right now is having different pages in my URL conf using the same templates and modifying the content within the View. But it's so unnatural to "hard-code" my content within the View, I suppose.
Is there a way to define content areas in my template and, for example, edit them within the Admin panel?
There must be better ways, I think.
Thank you!
Edit
Maybe Flatpages could help in my task. But does exist a way to add "content areas" tied to specific parts of the templates? I can fine only the main content area.
How are your "content areas" different than django's template blocks? You can define something like this in your layout file:
base.html
{% block overridable %}Default content{% endblock overridable %}
And in your templates, you extend it like this:
view.html
{% extends "base.html" %}
{% block overridable %} Overrided by view {% endblock overridable %}
You can use generic views, and I would heartily encourage you to. They're great. Check out the django docs for generic views. But as for different pages requiring a different template, yeah, that's how websites work.
Django is not "file-light" - it works by combining lots of small files. You're quite right to be averse to hard-coding HTML in your view controller - that's generally bad practice. You're going to have a template for each view, but really, how many views do you have? Even in a large application, the number of views that can't be handled by either a built-in or custom-made generic view is going to be pretty low.

django query db on every request

i have a navigation element that is determined by values in a database.
no matter what view it is, i need to get these navigation objects out of the database.
where in the code can i tell it to set a template variable containing all the navigation objeccts without setting it in every view?
It sounds like a good use for a context processor.
Right way to do this is to use templatetag. Then you don't have to include it in every view, just in your templates like {% load navigation %} {% navigation %}
How to write one:
django docs on template tags (read overview and inclusion tags)
anoher resource