第一次使用django,需要一些帮助...
错误:
Reverse for 'anuncio' with arguments '(u'Restaurante Avenida',)' and keyword arguments '{}' not found.
请求方法:GET Django版本:1.5.2 异常类型:NoReverseMatch 例外值:
使用参数'(u'Restaurante Avenida',)'以及找不到关键字参数'{}来反转'anuncio'。
异常位置:/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py in render,line 424 Python可执行文件:/ usr / bin / python Python版本:2.7.3
URL:
url(r'^anuncio/(?P<titulo>\d+)$', anuncio),
模板:
<a href="{% url 'anuncio' user.userprofile.anuncio %}"> {{user.userprofile.anuncio}} </a>
视图:
def anuncio(request, titulo):
Anuncio = Anuncio.objects.get(titulo = titulo)
variables = RequestContext(request, {'anuncio': Anuncio})
return render_to_response('anuncio.html', variables)
答案 0 :(得分:2)
你的问题在这里:
url(r'^anuncio/(?P<titulo>\d+)$', anuncio),
\d+
匹配数字。
您需要的是匹配字母和空格。
尝试
url(r'^anuncio/(?P<titulo>[\w ]+)$', anuncio, name = 'anuncio'),
此外,这里
Anuncio = Anuncio.objects.get(titulo = titulo)
请使用其他变量名称。 不覆盖模型名称。
anuncio = Anuncio.objects.get(titulo = titulo)
还有一件事,如果没有匹配,.get()
会抛出错误。所以,你可能想要考虑
anuncio = get_object_or_404(Anuncio, titulo = titulo)
最后一件事:这里
<a href="{% url 'anuncio' user.userprofile.anuncio %}"> {{user.userprofile.anuncio}} </a>
我建议使用id
而不是anuncio
字段。像user.userprofile.id
这样的东西,并将regex
保持为\d+
- 避免模型对象中的空格(并且是唯一的)