- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
刚开始玩 django,对 get_absolute_url 功能感到困惑。
当我尝试访问某个对象的 url 时,出现了 NoReverseMatch 错误。这是堆栈跟踪
$ 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.
这是我目前所拥有的
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})
restaurant_index 的网站呈现良好,当然,任何餐厅的 url 都是空字符串
模板
{% 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'),
您的 URL 模式将 restaurant_id
捕获为未命名组,因此您需要提供 restaurant_id
作为位置参数,或者更改 url 模式以捕获 restaurant_id
作为命名组
将 URL 模式更改为此
(r'^restaurants/(?P<restaurant_id>\d+)/$', 'food.views.restaurant_details'),
或者从 get_absolute_url
返回这个
return ('food.views.restaurant_details', (str(self.id),), {})
关于python - NoReverseMatch for get_absolute_url() 新手问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5808840/
我有一个简单的“post”模型,它代表我的博客的一个条目: class Post(models.Model): title = models.CharField('title', max_le
我正在尝试制作一个用于列出 django_tables 的通用表格对象。我的一切正常,除了 User 对象上的 get_absolute_urls() 返回: /users// 虽然我可以创建此 UR
您好,我正在创建站点地图,它在我的本地机器上运行良好,但在生产服务器中出现错误object has no attribute 'get_absolute_url'。我有课 class Car(mode
尝试访问我的 sitemap.xml 时,我收到此错误: 'Account' object has no attribute 'get_absolute_url' on line 112. 109.
def get_absolute_url(self): return ('threads_reply', [self.id]) get_absolute_url = model
我对 Django 和编写测试还很陌生。我目前正在从事一个项目,该项目有两个模型,Project 和 Technologies。项目模型与技术模型具有多对多关系。我有一个覆盖 get_queryset
我正在尝试在我的一个模型中对 get_absolute_url 使用 reverse。但是,它返回空白字符串。 网址.py url( regex=r'^$', view=Society
刚开始玩 django,对 get_absolute_url 功能感到困惑。 当我尝试访问某个对象的 url 时,出现了 NoReverseMatch 错误。这是堆栈跟踪 $ python manag
我有一个包含多对多字段的模型,我需要从多对多字段中选择一个 ID。我用 permaling 装饰器装饰了 get_absolute_url 方法。但这不起作用。所以我明白我需要反转关系,从跟踪中很明显
我想在 Django 中启用站点地图生成,所以我执行以下操作,它是如何解释的 here 型号: class Car(models.Model): def __unicode__(self):
这段代码有什么问题? get_absolute_url在我的模板中呈现时为空白,这意味着它在某处失败。 我怀疑这是 slug,因为这是我第一次尝试在 Django 中使用它: 谢谢 型号: class
当我在模板中调用我的模型 get_absolute_url 方法时,我想要一个绝对/完整的 url。在我的入门模型中,我有以下内容: def get_absolute_url(self): r
我正在尝试在 get_absolute_url() 中使用反向函数,但它没有为 View 找到正确的反向匹配。我的 url.py 没有任何错误,因为我可以通过 url“traildetail/4”等访
使用 Django 的分页器和模型方法 get_absolute_url 有什么特殊规则吗?我试图调用这个方法,但它不起作用? 这是我的模板: {% for product in products.
我正在研究 Practical Django Proects,但遇到了困难。我收到错误: Caught ViewDoesNotExist while rendering: Tried tagged_o
我想了解使用 get_absolute_url 调用而不是 url 模板标记有什么好处。 get_absolute_url: class Project(models.Model): @perm
我是 django 的新手,我已经搜索过这个问题,但找不到解决方案。如果解决方案很明显但我似乎无法正确解决,请原谅我。 所以,这就是问题所在。我有两个模型 Parishioner 和 Communit
Django 文档说: get_absolute_url() method to tell Django how to calculate the canonical URL for an objec
刚刚进入 Django Rest Framework。 我有一个简单的示例运行,其中模型通过 REST API 呈现。模型中没有相关字段,只有一堆字符串。 展示 get_absolute_url()
如果我有这样的模型: class Article(models.Model): title = models.CharField(max_length=200) # ... rest
我是一名优秀的程序员,十分优秀!