Django - How to convert URL template syntax to Django 1.5? - django

I was able to convert most {% url %} template syntaxes to Django 1.5.
But I'm not able to convert this kind of old url's to Django 1.5:
{% url monthly_archive date|date:'Y' date|date:'m' %}
This does not work:
{% url "monthly_archive date|date:'Y'" date|date:'m' %}
Any ideas?
Best Regards,

Why would you put the close quote there, after the first parameter? That makes no sense. It should go after the URL name, so that the thing quoted is just "monthly_archive".

Related

django url NoReverseMatch works in 3rd party app but not my project

I am integrating django-zinnia 0.12.3 into my django 1.4 project. I have it installed and working. I want to override its zinnia/base.html template so that all the content appears using my site's template. The zinnia templates extend 'zinnia/base.html'.
When I copy the file zinnia/templates/zinnia/base.html to myproject/templates/zinnia/base.html all the zinnia {% url %} stopped working and gave NoReverseMatch. Even if I made zero changes to the file. For example:
{% url 'zinnia_entry_archive_index' %} --> returns:
NoReverseMatch Reverse for ''zinnia_entry_archive_index'' ... not found
{% url 'admin:app_list' 'zinnia' %}" title="{% trans "Dashboard" %} --> returns:
NoReverseMatch u"'admin" is not a registered namespace
I was solved the problem by removing the quotes around the url names, for example:
{% url zinnia_entry_archive_index %}
However, if I remove the copy of base.html that I put in myproject/templates/zinnia (in other words it uses the original one in the zinnia project), the urls work again.
My question is why does it work with the quotes inside the original zinnia folder, but not from within my project folder?
The reason is that in Django <= 3 the url tag takes url name without quotes. But in Django 1.4+ it is deprecated and url name without quotes is gone in Django 1.5:
So if you are using Django <= 1.4 do not remove the quotes (unless you are passing context variable) around url names. Instead do this for compatibility reason if you ever wanted to upgrade your django version:
{% load url from future %}
{% url 'zinnia_entry_archive_index' %}
Documentation
Don’t forget to put quotes around the function path or pattern name!
Changed in Django 1.5: The first parameter used not to be quoted, which was inconsistent > with other template tags. Since Django 1.5, it is evaluated according to the usual rules: > it can be a quoted string or a variable that will be looked up in the context.

Django template error - only option for 'trans' is 'noop'

This is regarding Django tutorial - Part 2
http://docs.djangoproject.com/en/dev/intro/tutorial02/
In the section to change the template for admin page, I tried to change this section in the base_site.html page.
{% trans 'Django's administration' %}
When I add the apostrophe and s, I ge the error that -
TemplateSyntaxError at /admin/
only option for 'trans' is 'noop'
Why is it so? I thought I should be able to change the site's name. I tried using double quotes and escape sequence also, but it did not work.
I tried using double quotes and escape
sequence also, but it did not work.
That is definitely the problem. That is the only problem that the error message specifies!
Are you positive there are no other places where you've done that?
It should be:
{% trans "Django's administration" %}
That error messages only exists for the tag "trans" and appears if there is any other argument in the tag that is not noop.
The problem is the second single-quote:
{% trans 'Django's administration' %}
Django is treating everything after it as an argument. As the only argument it accepts is noop, this is causing an error.
One way to get around it is to do as Yuji 'Tomita' Tomita suggested, and enclose your translation string in double-quotes.
Another way is to use the blocktrans tag:
{% blocktrans %}
Django's administration
{% endblocktrans %}
As you're not using quotes to denote text which needs to be translated, this won't run into the same issue as the trans tag.

Django url templatetag (but not reverse() ) error: Caught NoReverseMatch while rendering

I'm trying to use the url template tag as such:
{% url all-labs-map %}
but when i view the page, i get this error:
Caught NoReverseMatch while rendering: Reverse for 'all-labs-map' with arguments '()' and keyword arguments '{}' not found.
When I use the template tag like this:
{% url gmaps.views.all_labs %}
It works just fine.
Here's the URL conf:
urlpatterns = patterns('gmaps.views',
url(r'^lab_list/$', 'all_labs', name="all-labs-map" ),
)
I tried using the django shell to see if there was a problem with the named URL, but using
reverse('all-labs-map')
returns the correct URL.
Any ideas on what's going on?
Thanks!
Majd
EDIT:
I am using django 1.2 on ubuntu with nginx server and gunicorn and virtualenv. I'm having another trouble with a custom tag where the library loads, but the tag itself is not recognized even though i'm using the correct tag registration syntax. Any ideas would be very greatly appreciated!
This is still high in Google results, but no one has answered it correctly yet. The key is this:
{% load url from future %}
Prior to Django 1.3, this was the syntax for the url templatetag:
{% url view_name arg1 %}
In Django 1.5, this will be the syntax:
{% url "view_name" arg1 %}
Starting in Django 1.3, the old version works but gives you a deprecation warning, telling you to {% load url from future %} and switch to the new version of that templatetag, in preparation for Django 1.5
Have you tried enclosing the name of the url in quotes like so:
{% url "all-labs-map" %}
or
{% url 'all-labs-map' %}
I've had some problems with URLs once and this seemed to help.
Also regarding #user608133 comment - you need to restart gunicorn rather than nginx, as nginx is just a proxy...
This error might occur if you have another url with the same name overriding this one that requires multiple parameters. Is there any duplicates found when you do a search in your entire project for 'all-labs-map'?

Using date in the Django url templatetag

I am trying to build a date-based URL with Django's url template tag. I have a datetime object that I can display like so:
{{block|date:"F j Y"}}
However, when I use nearly the same syntax with the url templatetag, like so:
{% url meeting block|date:"Y" %}
I get an error -- it appears that the only thing passed to url is an empty string:
... Reverse for 'meeting' with arguments '(u'',)' and arguments ...
What might I be doing wrong?
The url tag is a bit strange, and is very picky about its arguments. In particular, I don't think it evaluates any filters in its arguments.
You could try this:
{% with block|date:"Y" as blockyear %}{% url meeting blockyear %}{% endwith %}

Using string literals as parameters to template tags in Django templates

One of the things I find myself doing often is passing string literals as parameters to template tags or functions; for instance:
{% url my-url 'my_param' %}
Unfortunately, the django template engine doesn't let you do this. So I find myself doing this a lot in my view code:
my_context_dict['MY_PARAM'] = 'my_param'
and then in my view code:
{% url my-url MY_PARAM %}
Or creating a series of URL mappings, which I personally try to avoid.
Is it possible to use a string literal in Django templates? Or possibly a more elegant solution? I haven't seen anything on here or in the documentation.
This feels wrong but is right.
text
The nested ""'s don't seem like they should work. They do. The Django {% %} material is simply pulled out of the HTML without regard for surrounding context. So the "duplicated" "'s aren't really duplicated at all.
Use double quotes instead of single quotes:
{% url my_view "my_param" %}
Very wierd - I have a django project that uses single quotes to pass a string value and it functions just fine.
<a href="{% url categories 'vendor' %}"</a>
<a href="{% url categories 'crew' %}"</a>
On further investigation it turns out this has changed in django 1.5. It now requires the quotes even around the url pattern name.