Django视图有一个意外的关键字参数
问题描述:
我有以下网址格式:
urlpatterns = pattern('',
...
url(r'edit-offer/(?P<id>\d+)/$', login_required(edit_offer), name='edit_offer'),
)
和相应的edit_offer视图:
and a corresponding edit_offer view:
def edit_offer(request, id):
# do stuff here
报价页面上的链接导致编辑报价视图:
a link on offer page leads to edit offer view:
<a class="btn" href="{% url edit_offer offer.id %}">Edit</a>
单击按钮会引发TypeError:
clicking on the button throws a TypeError:
edit_offer() got an unexpected keyword argument 'offer_id'
有什么想法吗?我看不出这里有什么问题。我有其他具有类似模式的视图,它们都可以正常工作。
Any ideas what is going on? I don't see what's wrong here. I have other views with similar patterns and they all work ok.
答
尝试一下:
您的 urls.py
:-
urlpatterns = pattern('whatever_your_app.views',
...
url(r'edit-offer/(?P<id>\d+)/$', 'edit_offer', name='edit_offer'),
)
您的 views.py
:-
from django.contrib.auth.decorators import login_required
...
@login_required
def edit_offer(request, id):
# do stuff here
并在您的模板中
:-
{% url 'edit_offer' offer.id %}