当我尝试反转时,我收到一个错误“_reverse_with_prefix() 参数后 * 必须是一个序列,而不是 int”。我之前在视图中对参数进行了硬编码,但正在尝试使其动态化。有什么建议吗?
看法:
def add_review(request, product_id):
p = get_object_or_404(Product, pk=product_id)
if request.method == 'POST':
form = ReviewForm(request.POST)
if form.is_valid():
form.save()
#HARDCODED: return HttpResponseRedirect('/products/1/reviews/')
return HttpResponseRedirect(reverse('view_reviews', args=(p.id)))
else:
form = ReviewForm()
variables = RequestContext(request, {'form': form})
return render_to_response('reserve/templates/create_review.html', variables)
def view_reviews(request, product_id):
product = get_object_or_404(Product, pk=product_id)
reviews = Review.objects.filter(product_id=product_id)
return render_to_response('reserve/templates/view_reviews.html', {'product':product, 'reviews':reviews},
context_instance=RequestContext(request))
urlpatterns = patterns('reserve.views',
url(r'^clubs/$', 'index'),
url(r'^products/(?P<product_id>\d+)/reviews/$', 'view_reviews'),
url(r'^products/(?P<product_id>\d+)/add_review/$', 'add_review'),
url(r'^admin/', include(admin.site.urls)),
)