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

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?

Related

NoReverseMatch at /user_details/reset-password

I know it's well asked, but somehow none of the other answers seemed to solve my problem.
I get an error with the ReverseMatch, here is the relevant urls.py section:
# for password reset
url(r'^reset-password$','django.contrib.auth.views.password_reset', {'post_reset_redirect' : 'user_details/reset_password.html', 'template_name': 'user_details/reset_password.html'}, name="password_reset"),
url(r'^reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', 'django.contrib.auth.views.password_reset_confirm', {'post_reset_redirect' : 'user_details/reset_password.html'}, name="password_reset_confirm"),
I think the name is adequately defined?
The problem is in the template /usr/lib/python2.7/dist-packages/django/contrib/admin/templates/registration/password_reset_email.html, error at line 6:
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
where the {% url ...} is highlighted in red.
Why?
I assume that Django would then use the default template for that view?
Thanks!
Chris
Right, I had to resolve to overwriting the Django default email, because the reverse URL lookup doesn't want to behave properly.
So I created a copy of the original Django email file (/usr/lib/python2.7/dist-packages/django/contrib/admin/templates/registration/password_reset_email.html):
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
{% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}
and replaced line 6 with the following:
{{ protocol }}://{{ domain }}/user_details/reset/{{uid}}/{{token}}
which matches my urls.py.
This does indeed work now...
You have defined a URL which accepts a "uidb36" parameter, but the reverse call is passing "uidb64".

Slice url in django template

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

Customize Installed Apps on Django

in my server I have a lot of apps installed like, facebook_connect, userena, guardian and so on...
For example, I realized that if I customize the:
django-userena / userena / templates / userena / emails / activation_email_message.txt
{% load i18n %}{% autoescape off %}{% load url from future %}
{% if not without_usernames %}{% blocktrans with user.username as username %}Dear {{ username }},{% endblocktrans %}
{% endif %}
{% blocktrans with site.name as site %}Thank you for signing up at {{ site }}.{% endblocktrans %}
{% trans "To activate your account you should click on the link below:" %}
{{ protocol }}://{{ site.domain }}{% url 'userena_activate' activation_key %}
{% trans "Thanks for using our site!" %}
{% trans "Sincerely" %},
{{ site.name }}
{% endautoescape %}
For specified website, and I have more than 4 in the same server, I will make a complete mess in my django_site.
My question is:
How to customize the templates or models in some installed apps without completely change the original django_site?
Thanks in advance,
You cannot change the models, but you can override templates.
In the same directory as manage.py, you would have a directory called templates, there, you can create the following folder hierarchy, and put your custom template.
templates/userena/emails/activation_email_message.txt

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