Accessing view that rendered the template from the template in django - django

I was wondering if it is possible to determine which view rendered a specific template in Django (or get a boolean result). For example:
{% if this_view %}
some code
{% else %}
some other code
{% endif %}
I'm probably way off but I was wondering how we could create similar conditional statement within the template. Any help would be much appreciated!

You can use the django's request.get_full_path to get the current url in the template and from the url you should be able to figure out the view.
You can also use request.resolver_match.view_name in your template to get the view name

Related

How to use "for" statement in Django template

Here is Django template code.
I tried to put out images to template.
amount of image put out to template is depending on {{object.image_amount}}
However since {{object.image_amount}} is an integer I can not iterate like this
{% for i in object.image_amount %}
So I tried this but does not work as well.
{% for i in range object.image_amount %}
What should I do?
if you already have images that you need to show in your template
something like object.images or object.images.all if its related model
you can do
{% for image in object.images.all %}

what is difference between {{}} and {% %} in django templates

I am very new to django and working on it.. I visited a html file and dont know the difference between {{}} and {% %} in html files used
as here
{% load static %}
Thanks a lot
You can use
{% %} For sentences such as if and for or to call tags such as load, static, etc.
{{ }} To render variables in the template.
Read More about it at Django Docs
{% %} is for displaying code and {{}} is for displaying variables
There are three things in the template in Django
First is template variable and the second thing is template tag and third and last is template filter
so we write a template variable is {{}}
and write a template tag is {% %}
third and last is template filter {{variable |filter:arg}}
I'm new too for Django, so if i'm wrong, please someone correct me.
The difference between they are:
{{variable}} is used to use a variables. When the template encounters a variable, it evaluates that variable and replaces it with the result.
You also can use filters {{variable|filter}} like this:
{{name|length}} in this case you will use a variable "name" and return the length of that variable.
{%tag%} could use for loops or logic, or load external information into the template to be used by later variables. You can create block tags to help extend other html files parts. Also you can create custom tags.
A good place to see how to do it:
https://www.codementor.io/hiteshgarg14/creating-custom-template-tags-in-django-application-58wvmqm5f
Tags like loops and block, need to be closed.
{% %} for IF ELSE CONDITIONS and FOR LOOP etc
{{ }} for veriables that rendered from view function also used in FOR LOOP veriables like
`enter code here`
{% for obj in qs%}
{{ obj.veriable_name }}
{% endfor %}

Set button variable based on loaded view/url using Djangos Template Language? Python

In my base.html template I have a link to a website that I would like displayed at the top of each page that extends it:
base.html
Results
Except for results.html, when I load that page I would like the link loaded as:
results.html
Home
I'm under the impression that the template language could solve my problem with an if statement:
if currentTemplate/urlRoute != results.html:
button = Results
else:
button = Home
Please help point me in the right direction to implement this if possible :)
Thanks :)
You can get current URL in template by using request.path. The request variable automatically gets passed into each template context if you use Django's RequestContext as recommended (you probably do, since it's the default way).
Then you can just do {% if "reports" in request.path %}......{% endif %}.
That said, a cleaner approach would be to put the link in your base.html in a {% block %} template tag, like this:
{% block top_link %}Results{% endblock %}
Then this URL will be the same in all the pages, and you will be able to override it in your reports page by just specifying another content for the block.

Django Pass Multiple Parameters to Custom Template Filter Inside If Statement

I have an issue. I've written a custom template tag with a function signature like this-
def has_paid_for_article(article, request):
Now, in my template tag I have a conditional statement to determine whether a user can download an article or not (this is determined by if the article is older than two years or the logged in user has paid for the article). Here's the snippet-
{% if article|is_older_than_two_years %}
<span class="amp">& </span>{% get_article_download_link article %}
{% else %}
download
{% endif %}
The aforementioned snippet works fine, however I need to call the has_paid_for_article() function inside of a conditional statement. I've tried the following ways to make this happen-
{% if article|is_older_than_two_years or article|request|has_paid_for_article %}
,
{% if article|is_older_than_two_years or [article, request]|has_paid_for_article %}
This one works outside of the conditional statement-
{% if article|is_older_than_two_years or has_paid_for_article article request %}
What would be the correct syntax here? Also, I've read other posts on the topic, I CANNOT put this logic in the view. I won't go into detail, but with the way it works, that is not an option. Thank you!
Try
{% if article|is_older_than_two_years or article|has_paid_for_article:request %}
See Writing custom template filters

Django Custom Template Tag with Context Variable Argument

I have a custom template tag which shows a calendar. I want to populate certain items on the calendar based on a dynamic value.
Here's the tag:
#register.inclusion_tag("website/_calendar.html")
def calendar_table(post):
post=int(post)
imp=IMP.objects.filter(post__pk=post)
if imp:
...do stuff
In my template, it works fine when I pass a hard coded value, such as
{% load inclusion_tags %}
{% calendar_table "6" %}
However when I try something like {% calendar_table "{{post.id}}" %} , it raises a error a ValueError for the int() attempt. How can I get around this?
You want {% calendar_table post.id %}; the extra {{ and }} are what are causing you the heartburn.
Note that, in your custom tag, you need to take the string ("post.id") that gets passed and resolve it against the context using Variable.resolve. There's more information on that in the Django docs; in particular, look here: http://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#passing-template-variables-to-the-tag