Slice url in django template - django

I'm using the following url in my template: {% url 'index' %}.
Assume that it resolves to /index.
How can I slice it? To make it /ind?
{% url 'index'|slice:"4" %} doesn't work, it slices index.

Use as statement, then apply slice or truncatechars filters to it
{% url 'index' as the_url %}

Try use this:
{% with 'index'|slice:":4" as path %}
{% url path %}
{% endwith %}

Related

How to pass variable into Django block in url?

Context
I am trying to loop through a list of objects, then call on the name and id attribute and pass it into the Url block as regex to be captured in the urls.py later.
Problem
{%if people_found%}
{%for the_people in people_found%}
{{the_people.firstname}}
{%endfor%}
{%endif%}
Urls.py
app_name = 'Search4Bday'
urlpatterns= [path("search/profile/<str:firstname>/<int:id>", views.profile, name = "profile")]
Question
I'm not sure how I can pass the variables into the urls.py file if I can't call on the variable the_people with double curly braces. I'm also not sure if this is the correct way to pass in the variables into the regular expression capture portion of the urls.py. Any ideas?
Use add tag of Django Template Lang:
{% if people_found %}
{% for the_people in people_found %}
{{the_people.firstname}}
{% endfor %}
{% endif %}
edit: I didn't pay attention to urls.py and this will not work as wanted. Instead of adding slashes between variables put them directly. So this should work.
{% if people_found %}
{% for the_people in people_found %}
{{the_people.firstname}}
{% endfor %}
{% endif %}
I tried another method, I could have just done this:
{% if people_found %}
{% for the_people in people_found %}
{{the_people.firstname}}
{% endfor %}
{% endif %}
https://docs.djangoproject.com/en/4.0/ref/templates/language/

django template url - append variable to string

Im trying to add a variable to the end of a string and am getting issues with reverse match, from what I gather the below should work
{% for type in type_links %}
<li>{{ type.subnet_type }}</li>
{% endfor %}
I should have a url that has "all_1" for example in it.
however I am getting a reverse url error
Reverse for 'site_list' with arguments '('',)' not found. 1 pattern(s) tried: ['sites/site_list/(?P<display_filter>[\\w\\-]+)$']
is this correct way to add a variable to end the of the string?
EDIT:
url pattern, the pattern works as I have tested it manually before trying to create the URLs dyanmically
url(r'^site_list/(?P<display_filter>[\w\-]+)$', views.site_list, name='site_list'),
Thanks
You can do
{% for type in type_links %}
<li>
{% with type.id|stringformat:"s" as str_obj_id %}
{% with 'all_'|add:str_obj_id as extra_param %}
{{ type.subnet_type }}
{% endwith %}
{% endwith %}
</li>
{% endfor %}

How do I add urls to {% blocktrans %} without failing silently

For adding an url to a blocktrans, I use:
{% url "home" as home %}
{% blocktrans with url=home %}
...
{% endblocktrans %}
However, this is not equivalent to use {% url "home" %} without the blocktrans because it fails silently if url "home" doesn't exist.
Is there any way to add an url to blocktrans as if I was using {% url "home" %} inside the {% blocktrans %}?
Putting in other words, how do I make a sentence with an url translatable and having the url not failing silently in Django?

URL output in template is empty

URL pattern declaration from views.py
url(r'^intentions/(\d+)/$', 'intentions.views.show'),
When I am writing address directly, like intentions/1 works ok, but when I trying to display URL show for object like:
{% for i in intentions %}
{% url 'intentions.views.show' i.id as iUrl %}
<li>{{ i }}</li>
{% endfor %}
I am ending with empty href. Could someone give me any advice?
If you are using Django 1.3 or 1.4, make sure you are loading the new url tag. Add the following line to the top of your template:
{% load url from future %}
If you are using Django 1.4 or earlier, and you haven't loaded the new url tag as above, then you need to remove the single quotes from the url pattern:
{% url intentions.views.show i.id as iUrl %}
As an aside, it's recommended to name your url pattern:
url(r'^intentions/(\d+)/$', 'intentions.views.show', name='show_intention'),
Then change your template to:
{% for i in intentions %}
{% url 'show_intention' i.id as iUrl %}
<li>{{ i }}</li>
{% endfor %}
you could try
{% for i in intentions %}
<li><{{ i }}</li>
{% endfor %}
don't use {% url 'intentions.views.show' i.id as iUrl %}, it's maybe confused.
Also,you could try modifiy the url as follow
url(r'^intentions/(\d+)/$', 'intentions.views.show', name="intention_view"),
then,
{% for i in intentions %}
{% url "intention_view" i.id as iUrl %}
<li>{{ i }}</li>
{% endfor %}
If there are also ending empty href,try the first method.
'intentions.views.show'
Also give a name='' to your URL and use it in your template code.

Django trans and url tags

I want to translate a paragraph containing a URL in a Django 1.3 application.
<p>
First edit your profile, please.
</p>
Depending on the language, the text surrounded by <a> tags will surely change. How can I allow translators to decide on the link placement? Wrapping the entire thing in a {% trans %} causes an error:
<p>{% trans "First <a href='{% url edit-profile username=user.username %}'>edit your profile</a>, please." %}</p>
The error thrown is TemplateSyntaxError: Searching for value. Unexpected end of string in column 64: trans "First <a href='{% url edit-profile username=user.username.
How should I go about doing this? Do I have to determine the URL in the view, then pass that URL as a string to the template? That seems like a really convoluted solution for what I would think is a very common problem.
Use {% blocktrans %}. The Django translation docs include this example:
{% url path.to.view arg arg2 as the_url %}
{% blocktrans %}
This is a URL: {{ the_url }}
{% endblocktrans %}
This works for me:
{% url "app-name:name-of-view" as the_url %}
{% blocktrans %}
This is a URL: {{ the_url }}
{% endblocktrans %}