How do I create if statement on django template? - django

enter image description here
If there are not projects created, projects is the name of my viewclass for my model, then I need it to create a message, but I do not know how to create the right if statement.

Standard way to implement any if statement in Django's template:
{% if my_projects %}
Do what you want if condition is ok
{% else %}
Do what you want if condition is not satisfied
{% endif %}
I assume that my_projects variable contains a list/queryset of projects if any and is passed to the view's context.

Related

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 how to store an object in session to use in multiple templates

Im working with django and i would like to store a object (in session?) so i can use this in multiple templates. So similar to the "user" that is always accessible, id like to add one of my own. So i dont have to add it every time in render(request,
What i try so far:
def login_character(request, character_name):
request.session['character'] = Character.objects.get(name=character_name)
return HttpResponseRedirect(reverse('index'))
Template:
{% if 'character' in request.session %}
<p>Jeej there is some character</p>
{{ request.session.character.name }}
{% else %}
<p>Nope, nothing here</p>
{% endif %}
But that doesn't seem to work,
Can someone help me out or point in the right direction?
With kind regards,
Hans
I think you meant
{% if 'character' in request.session %}
instead of
{% if 'character' in request.session['character'] %}
Also, you wrote that you need sessions, because you want an object to be always accessible, without a need to explicitly add it in every view. In this case I think a template context processor would probably be a better choice.
Update: You also need to make sure that django.core.context_processors.request is among template context processors in your settings file. See also this answer.

update value of a variable inside a template - django

I have seen enough number of examples that allow me to declare a new variable inside a template and set its value. But what I want to do is to update the value of a particular variable inside the template.
For example I have a datetime field for an object and I want to add timezone according to the request.user from the template. So I will create a template filter like {% add_timezone object.created %} and what it will do is that it will add timezone to object.created and after that whenever I access {{object.created}} it will give me the updated value.
Can anybody tell me how this can be done. I know I need to update the context variable from the template filter. But don't know how.
You cannot modify a value in a template, but you can define 'scope' variables using the {% with %} tag:
{% with created=object.created|add_timezone %}
Date created with fixed timezone: {{ created }}
{% endwith %}
where add_timezone is a simple filter:
def add_timezone(value):
adjusted_tz = ...
return adjusted_tz
register.filter('add_timezone', add_timezone)

django tags and html buttons

In my base.html I have:
blabla
{% ifequal alterprofile no %}
{% include 'registration/submittedprofile.html' %}
{% else %}
{% include 'registration/submittedprofile2.html' %}
{% endifnotequal %}
blabla
In views.py I have alterprofile = "no".
How do i change alterprofile to "yes". This is my submittedprofile:
<form action="" method="get">
blablabla
<input type="submit" value="Make Changes">
</form>
And this is my views.py:
def userprofile(request):
alterprofile = "no"
username = request.user
return render_to_response('registration/userprofile.html', {'user': username, 'alterprofile' = alterprofile})
Is there anyone who can code the answers for me. I've tried playing round with the previous answers but to no affect.
Django variables are rendered from the server side, so you can not change the variable after it was passed to your template. What you want to achieve is done via frontend scripting.
In this case you would pass both variables to the django template, save them in your Javascript and then switch them once you clicked the button you mentioned (via onClick event handling).
You can use url arguments like:
/myurl/
/myurl/?show2
then, in your views.py you can use request.POST['show2'] to check if exists and then send a variable again to the view to be checked with your {if}s
As an aside note, either you don't understand basic request flow with web applications or you are not explaining properly what you mean with "html button", so you are not fluent with html language. Sorry if my intuition is harsh or wrong.
its not that clear what you are asking but here is how to make the logic work as I think you want it based on yoru submitted code:
in your template change it to
ifequal alterprofile "no"
to include registration/submittedprofile.html.
When you change the view to alterprofile = "yes" the registration/submittedprofile2.html will be included instead if you keep your current template logic.
This is because in your view, alterprofile is assigned a string therefore its always a string. When you tried to test against no instead of "no" django was looking for a variable called no which doesn't exist.
This means that everytime you run it would have always included registration/submittedprofile2.html

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