Re: Generic views and url issues

40 views
Skip to first unread message

Sergiy Khohlov

unread,
Jan 24, 2013, 1:23:17 AM1/24/13
to django...@googlegroups.com
try to check testaddcall/1/ please
Many thanks,

Serge


+380 636150445
skype: skhohlov


2013/1/24 <amy.c...@cbsinteractive.com>:
> I've been trying to understand how to use generic views. I've followed some
> tutorials, and read through Django docs, but I can't get the url function to
> work in my templates.
>
> I get the error
> NoReverseMatch at /testadcall/
>
> Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not
> found.
>
>
> in my urls.py
>
> queryset = {'queryset': Adcall.objects.order_by('name')}
>
> urlpatterns = patterns('django.views.generic.list_detail',
> url(r'^$','object_list', queryset, name="adcalls"),
> url(r'(?P<object_id>\d+)/detail/$', 'object_detail', queryset,
> name="detail"),
> )
>
> in my template for the list view:
>
> {% load url from future %}
> {% if object_list %}
> <ul>
> {% for adcall in object_list %}
> <li><a href="{% url 'detail' adcall.id %}/">{{ adcall.name
> }}</a></li>
> {% endfor %}
> </ul>
> {% endif %}
>
> I've tried no quotes, single quotes, and double quotes around the url name
> "detail", with no apparent effect.
>
> Am I wrong in thinking that this should work?
>
> I'm using Django 1.4.3, Python 2.7.3
>
> Thanks so much.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/M6Qrx89JeBkJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

Sanjay Bhangar

unread,
Jan 24, 2013, 2:52:10 AM1/24/13
to django...@googlegroups.com
(reply inline)
try:
{% url 'detail' object_id=adcall.id %}

since you have object_id defined as a keyword param in the url, you
will need to specify it as a keyword param in the {% url %} tag, I
*think* :)

let know if that works -
best've luck,
Sanjay

mgc_djan...@chamberlain.net.au

unread,
Jan 24, 2013, 2:53:03 AM1/24/13
to django...@googlegroups.com, amy.c...@cbsinteractive.com
On 24/01/2013 10:39 AM, amy.c...@cbsinteractive.com wrote:
> I've been trying to understand how to use generic views. I've
> followed some tutorials, and read through Django docs, but I can't get
> the url function to work in my templates.
>
> I get the error
> NoReverseMatch at /testadcall/
> Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found.
>
> in my urls.py
>
> queryset = {'queryset': Adcall.objects.order_by('name')}
>
> urlpatterns = patterns('django.views.generic.list_detail',
> url(r'^$','object_list', queryset, name="adcalls"),
> url(r'(?P<object_id>\d+)/detail/$', 'object_detail', queryset,
> name="detail"),
> )
>
> in my template for the list view:
>
> {% load url from future %}
> {% if object_list %}
> <ul>
> {% for adcall in object_list %}
> <li><a href="{% url 'detail' adcall.id %}/">{{ adcall.name
> }}</a></li>
> {% endfor %}
> </ul>
> {% endif %}
>
> I've tried no quotes, single quotes, and double quotes around the url
> name "detail", with no apparent effect.
>
> Am I wrong in thinking that this should work?
>

The problem is a mismatch between your urls.py pattern and the
parameters you give to the url templatetag - note in the error message
that it mentions both arguments and keyword arguments (with your example
having a single non-keyword argument). However, in your url pattern for
the "detail" url, you use a named capture (object_id). In this case, you
must use a keyword argument to match:

{% url 'detail' object_id=adcall.id %}

Regards,
Michael.

Amy Cerrito

unread,
Jan 24, 2013, 9:01:30 AM1/24/13
to mgc_djan...@chamberlain.net.au, django...@googlegroups.com
Thanks for your response!

Unfortunately, your suggestion did not eliminate the NoReverseMatch error.

NoReverseMatch at /testadcall/

Reverse for 'detail' with arguments '()' and keyword arguments '{'object_id': 1}' not found.

I have another example where I request the list view, which does not expect arguments, to keep it even simpler.

urls.py

queryset = {'queryset': Adcall.objects.order_by('name')}

urlpatterns = patterns('django.views.generic.list_detail',
    url(r'^$','object_list', queryset, name="adcalls"),
    url(r'^(?P<object_id>\d+)/detail/$', 'object_detail', queryset, name="detail"),
)


template:

EDIT TEMPLATE
{% load url from future %}
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

<a href="{% url 'adcalls' %}">Back to adcall list</a>


Error:

NoReverseMatch at /testadcall/1/detail/

Reverse for 'adcalls' with arguments '()' and keyword arguments '{}' not found.
--
Amy Cerrito
Engineering Manager, AdOps Technology
T 617.284.8697 
55 Davis Sq, Somerville, MA 02143

Bill Freeman

unread,
Jan 24, 2013, 9:18:36 AM1/24/13
to django...@googlegroups.com
Have you tried it without the quotes around 'detail' in the url template tag invocation?

If I'm remembering correctly, the need for the quotes depends on django version and options.

Bill

--
You received this message because you are subscribed to the Google Groups "Django users" group.

Tom Christie

unread,
Jan 24, 2013, 10:07:14 AM1/24/13
to django...@googlegroups.com, mgc_djan...@chamberlain.net.au
Hi Amy,

I'd suggest you check the following...

* Your ROOT_URLCONF setting is correct.
* If the `urls.py` module you're referring to here is a project URL conf and not the root URL conf, make sure that it is being included by the root URL conf.  (This seems like the most likely cause)
* If it is being included by the root URL conf, check if it's being included with a `namespace` argument, and if so make sure to prefix the reverse with the appropiate namespace, eg if it's namespaced as 'myapp', then use 'myapp:addcalls'.

You've already made things really simple, the URL conf as you've declared it looks correct, so this...

   Reverse for 'adcalls' with arguments '()' and keyword arguments '{}' not found.

Suggests that 'adcalls' simply isn't included part of the URL conf of the application.

Cheers,

  Tom

amy.c...@cbsinteractive.com

unread,
Jan 24, 2013, 12:26:32 PM1/24/13
to django...@googlegroups.com, mgc_djan...@chamberlain.net.au
Updating my template code to use namespace:url_name did it!

list url:

<a href="{% url 'testadcall:adcalls' %}">List of all ad calls</a>

detail url:

<a href="{% url 'testadcall:detail' object_id=adcall.id %}">

Thanks again for everyone's feedback!  
Reply all
Reply to author
Forward
0 new messages