how to reverse url in django template syntax - django

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 %}

Related

Django if logic in template

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 %}

How to set href value in html using python

I have been trying to solve this problem: i want to set url to html a tag using loop. I tried this way. But it gives me error which is "Reverse for 'i.menuResolve' not found. 'i.menuResolve' is not a valid view function or pattern name".
In case, "i.menuResolve" returns url which is '/sales/profile' etc.
{% for i in userMenus %}
<li>
<a href="{% url 'i.menuResolve' %}" ></a>
</li>
{% endfor %}
Please help if anybody knows this error?
If you have a method or property that returns a URL, you don't need to use Django's {% url %} template tag. That template tag passes the arguments to Django's reverse() function, but you don't need to do that if you've already got the URL.
Give this a try:
{% for i in userMenus %}
<li>
<a href="{{ i.menuResolve }}" ></a>
</li>
{% endfor %}

url resolvers inside if statements Django

Just wondering what the correct syntax is for checking if the current path is equal to some url:
{% if request.path == url "app_namespace:route_name" %}
Above doesn't work - but hoping someone knows a way or method for doing this lookup...
You can use this syntax to save the url path in a template variable:
{% url 'app_namespace:route_name' as url_path %}
which you can later use within your if condition
{% if request.path == url_path %}...{% endif %}
Note that you may also find this syntax useful when you need to use the output of a url function within a blocktrans block:
{% blocktrans %}
text to translate
{% endblocktrans %}

how to give {% url '' %} to a variable?

I have this requirement in django template:
{% if user.is_admin %}
<a href='{% url 'url_to_admin' %}'>admin url</a>
{% else %}
<a href='{% url 'url_to_other %}'>other url </a>
{% endif %}
and now, I want let it more simple, I want let url to a variable `xx', like this:
if user.is_admin:
myurl = {% url 'url_to_admin' %}
else:
myurl = {% url 'url_to_other' %}
<a href={{myurl}} > url </a>
But I don't know how to write in django's template?
The url tag can be assigned to a variable through using as.
{% url 'my_url' as foobar %}
{{ foobar }}
But that doesn't make sense in the current place where you're using it. Just assign the anchor tags inside of the if/else statements
{% if user.is_admin %}
admin
{% else %}
other
{% endif %}
Not the answer, but you may try this:
<a href='{% if user.is_admin %}{% url 'url_to_admin' %}{% else %}{% url 'url_to_other' %}'>other url </a>
why don't you handle it in the views before you render the template, try something like in your view.py
if User.is_admin:
url = 'url_to_admin'
else:
url = 'url_to_other'
render(request, "your_template.html", {'url' : url})
then you can go ahead and call your url in your template like
<a href= "{{ url }}>url</a>"
views.py
from django.core.urlresolvers import reverse
def my_view(request):
if request.user.is_admin:
my_url = reverse('my_admin_view')
else:
my_url = reverse('my_other_view')
return render(request, 'my_template.html', {'my_url': my_url })
Using django.core.urlresolvers.reverse
my_template.html
This is a link

URL template optional param

Let's consider this template part
<form class="form-horizontal" action="{% if client_id %}
{% url client_edit client_id=client_id %}{% else %}
{% url client_edit %}
{% endif %}" method="post">{% csrf_token %}
{{ client_form }}
</form>
As you can see the parameter client_id is optional.
Is there a way to avoid this repetition (url client_edit) ?
Url pattern:
url('^client/edit$', client_edit, name='client_edit'),
url('^client/edit/(?P<client_id>\d+)$', client_edit, name='client_edit'),
It's not repetition of using
{% url client_edit %}
since you actualy define two urls. If you really want to make it shorter (not necessary simplier) you could create some filter like
{{client_id|make_url}}
and inside filter you can resolve to proper url
urls don't have optional parameters. You can have multiple patterns point to the same view (as you have done), and then check for the defaults in the view. In your template, {% url client_edit client_id=client_id|default_if_none:-1 %}, then depending on what you want to happen on the view end filter appropriately:
def client_edit(request, client_id = None):
if client_id:
client = get_object_or_404(Client, pk=client_id)
else:
# Default value for client
client = Client.objects.filter(active=True) # for example
# your normal logic here