views:

1558

answers:

4

So, here is one of my urls.py

urlpatterns = patterns('items.views',
    url(r'^(?P<item_id>[\d+])/$', 'view_listing', name="item_view"),
)

And in my template, I can do this: <a href="{% url item_view 1 %}">here</a> and I'll get a link to the right page. Everything works great!

But, here is another one

urlpatterns = patterns('django.views.generic.list_detail',
    (r'^(?P<slug>[\w-]+)/$', 'object_detail', dict(page_info, slug_field='slug'), "page_view"),
)

But in my template if I try this: <a href="{% url page_view slug='TermsAndConditions' %}">Terms and Conditions</a> or this <a href="{% url page_view 'TermsAndConditions' %}">Terms and Conditions</a> it errors out with this error:

TemplateSyntaxError at /

Could not parse the remainder: ''TermsAndConditions '' from ''TermsAndConditions ''

Does anyone know if it's possible to use named urls with generic views and the url template tag like this? Or the right way to get it to work with generic views?

Thanks.

A: 

Don't put quotation marks around the string "TermsAndConditions":

{% url page_view slug=TermsAndConditions %}
James Bennett
This gives me a different error now.TemplateSyntaxError at /Caught an exception while rendering: Reverse for 'listitstolen.page_view' with arguments '()' and keyword arguments '{'slug': ''}' not found.
TehOne
Sorry, do it with double quotes. I thought it was a variable you were trying to use.
James Bennett
I had tried this before as well, and it also does not work. I get this error:TemplateSyntaxError at /Caught an exception while rendering: Reverse for 'listitstolen.page_view' with arguments '()' and keyword arguments '{'slug': u'TermsAndConditions'}' not found.
TehOne
+3  A: 

The template system in Django only supports double quotes, which explains the syntax error you get when using single quotes. You will need to do

{% url page_view slug="TermsAndConditions" %}

If you omit the quotes, Django things you're referring to a variable named TermsAndConditions.

Guðmundur H
I had tried this before as well, and it also does not work. I get this error:TemplateSyntaxError at /Caught an exception while rendering: Reverse for 'listitstolen.page_view' with arguments '()' and keyword arguments '{'slug': u'TermsAndConditions'}' not found.
TehOne
Strange. This works for me—I put the URLconf for page_view in an empty project and tried the template code in a shell—the reverse worked. Try using url() in the URLconf (although I guess it make no difference...) and try also the positional version: {% url page_view "TermsAndConditions" %}
Guðmundur H
Still didn't work. Not sure how it worked for you and not for me, but this wouldn't be the first time in programming that this is the case. I wonder what is different between out implementations. U were able get it to work using the generic detail view? Here is my code http://dpaste.com/hold/15428/
TehOne
A: 

Is there a solution?

I didn't ever find one.
TehOne
+1  A: 

The solution is

<a href='{% url page_view slug="TermsAndConditions" %}'>Terms and Conditions</a>
Stan
Who would have thought such a simple solution as changing the types of quotes would be the fix. I'm not even working on the site anymore, but in any case, I did a quick test, and it worked. Thanks.
TehOne