开发者

NoReverseMatch for get_absolute_url() newbie question

Just started playing around with django and was confused with the get_absolute_url functionality.

When I try to access the url for an object I get a NoReverseMatch error. Here is the stack trace

$ python manage.py shell
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from food.models import *
>>> r = Restaurant.objects.get(pk=1)
>>> r
<Restaurant: East Side Marios>
>>> r.get_absolute_url()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py", line 55, in _curried
    return _curried_func(*(args+moreargs), **dict(kwargs, 开发者_运维知识库**morekwargs))
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 887, in get_absolute_url
    return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self, *args, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/__init__.py", line 35, in inner
    return reverse(bits[0], None, *bits[1:3])
  File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 391, in reverse
    *args, **kwargs)))
  File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 337, in reverse
    "arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'food.views.restaurant_details' with arguments '()' and keyword arguments '{'restaurant_id': ['1']}' not found.

Here's what I have so far

server/urls.py

from django.conf.urls.defaults import *

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',

  # ADMIN
  (r'^admin/doc/', include('django.contrib.admindocs.urls')),
  (r'^admin/', include(admin.site.urls)),

  # Restaurants
  (r'^restaurants/$', 'food.views.restaurant_index'),
  (r'^restaurants/(\d+)/$', 'food.views.restaurant_details'),
)

server/food/models.py

class Restaurant(models.Model):
  name        = models.CharField(max_length=50, unique=True)
  description = models.TextField()
  website     = models.URLField(verify_exists=True)
  def __unicode__(self):
    return self.name
  @models.permalink
  def get_absolute_url(self):
    return ('food.views.restaurant_details', (), {'restaurant_id': [str(self.id)]})

server/food/views.py

from food.models import *
from django.shortcuts import render_to_response, get_object_or_404

# Restaurant

def restaurant_index(request):
    restaurant_list = Restaurant.objects.all()
    return render_to_response('food/restaurant/index.html', {'restaurant_list': restaurant_list})

def restaurant_details(request, restaurant_id):
    restaurant = get_object_or_404(Restaurant, pk=restaurant_id)
    menu_list  = Menu.objects.filter(restaurant=restaurant_id)
    return render_to_response('food/restaurant/detail.html', {'restaurant': restaurant, 'menu_list': menu_list})

The website for restaurant_index renders fine except that, of course, the url for any restaurants is the empty string

template

{% if restaurant_list %}
  <ul>
    {% for restaurant in restaurant_list %}
      <li><a href="{{ restaurant.get_absolute_url }}">{{ restaurant }}</a></li>
    {% endfor %}
  </ul>
{% else %}
  <p>No restaurants are currently listed.</p>
{% endif %}

html

<ul>
  <li><a href="">East Side Marios</a></li>
</ul>


(r'^restaurants/(\d+)/$', 'food.views.restaurant_details'),

Your URL pattern is capturing the restaurant_id as non-named group, so you need to either supply restaurant_id as positional argument, or change the url pattern to capture restaurant_id as named group

Change URL pattern to this

(r'^restaurants/(?P<restaurant_id>\d+)/$', 'food.views.restaurant_details'),

or return this from get_absolute_url

return ('food.views.restaurant_details', (str(self.id),), {})


Aside from the solution provided by Imran, you might want to look into named URL patterns:

urls.py

urlpatterns = patterns('',
  url(r'^restaurants/(\d+)/$', 'food.views.restaurant_details', name='restaurant_detail'),
)

restaurant_index.html

<a href="{% url restaurant_detail restaurant.id%}">{{ restaurant }}</a>

This way, you are not bound to the function name of your view and allows more easily to switch to Generic Views.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜