Is something like this possible in a template?
{% if request.path == url 'posts:post_detail' pk=instance.id %}
If not, how can I achieve this result being True? I want to this to be true when user is looking at a specific post such as post/1.
Yes, you can using the as var syntax:
{% url 'posts:post_detail' pk=instance.id as the_url %}
...
{% if request.path == the_url %}
...do something
{% endif %}
Related
I have this simple tag:
myapp/templatetags/my_filters.py
#register.simple_tag
def get_bookmark_object(content_type, object_id):
return Bookmark.objects.get(content_type=content_type, object_id=object_id)
In my template, I want to be able to do this:
{% load my_filters %}
{% with object as bookmark %}
{% with bookmark_object=get_bookmark_object bookmark.content_type bookmark.object_id %}
{% if bookmark.content_type.model == 'post' %}
{% include 'content/post/object.html' with object=bookmark_object user_bookmark=bookmark %}
{% elif bookmark.content_type.model == 'note' %}
{% include 'content/note/object.html' with object=bookmark_object user_bookmark=bookmark %}
{% endif %}
{% endwith %}
{% endwith %}
I get the error:
TemplateSyntaxError at /my-page/
'with' received an invalid token: 'bookmark.content_type'
My question is:
How do I use my custom get_bookmark_object template tag in a with statement? An example with code would help me clarify a lot.
Reference:
Django's with built-in
What you defined is a template tag, you can assign the value produced by the template tag with an {% … as … %} tag:
{% get_bookmark_object bookmark.content_type bookmark.object_id as bookmark_object %}
…
for example, I can reverse a url in this way:
{% url 'home:index' %}
but if I need to compare a url in a if sentence, like this:
{% if request.path == url %}
and I want to replace the url with a reverse one
but I can't do this:
{% if request.path == {% url 'home:index' %} %}
So is there another way can solve this?
Thanks a lot !
The url tag takes an argument as <var> that saves the result of the reverse to a variable. You can then use this variable in your comparison
{% url 'home:index' as home_url %}
{% if request.path == home_url %}
First, what I'd like to work:
{% if method == POST %}
{% include "app/template_x.html"%}
{% else %}
{% include "app/template_y.html"%}
{% endif %}
I'm using class based views, inheriting from django.views.generic.edit.UpdateView
I tried a few things, among them passing an HttpRespons.method object via extra context in the urls.py asview() function.
Anyone have an idea how I should approach this problem?
Edit: Using request.method always returns "GET" upon template rendering. I want the user to be able to submit a form and have the template render a different {% include "template" %} on the same page following confirmation of success.
{% if request.method == "POST" %}
{% include "app/template_x.html"%}
{% else %}
{% include "app/template_y.html"%}
{% endif %}
I want to write a template that renders something only one time.
My idea is to create a flag variable to check it is the first time.
My code
{% with "true" as data %}
{% if data == "true" %}
//do something
** set data to "false" **
{% else %}
//do something
{% endif %}
{% endwith %}
I don't know How to change a variable in django template. Is it possible? Or is there a better way to do this?
This can be done with a Django custom filter
django custom filter
def update_variable(value):
data = value
return data
register.filter('update_variable', update_variable)
{% with "true" as data %}
{% if data == "true" %}
//do somethings
{{update_variable|value_that_you_want}}
{% else %}
//do somethings
{% endif %}
{% endwith %}
NIKHIL RANE's answer doesn't work for me. Custom simple_tag() can be used to do the job:
#register.simple_tag
def update_variable(value):
"""Allows to update existing variable in template"""
return value
and then use it like this:
{% with True as flag %}
{% if flag %}
//do somethings
{% update_variable False as flag %}
{% else %}
//do somethings
{% endif %}
{% endwith %}
So I have this in a Django template.
{% if request.get_full_path = "/blog/" %}
do something
{% endif %}
Which does exactly what I want when I go to http://somesite.com/blog/
What I want to do is also include all subdirectories.
So something like
{% if request.get_full_path = "/blog/*" %}
do something
{% endif %}
Unfortunately as far as I can tell Django doesn't do wildcards in templates. So how do I do this?
Try with this:
{% if request.get_full_path|slice:'0:6' == '/blog/' %}