如何正确使用reverse进行HttpResponseRedirect?

2024-05-14 03:38:43 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图反转时,得到一个错误“_reverse_with_prefix()参数after*必须是一个序列,而不是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)),
)

Tags: formviewadd视图idurl参数return
3条回答

检查args=(p.id)内部的reverse(),它必须是args=(p.id,)。第一种形式被视为整数而不是序列。

参考文献the docthe tutorial

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses).

另外,使用'reserve.views.view_reviews'而不仅仅是'view_reviews',因此:

reverse('reserve.views.view_reviews', args=(p.id,))

检查the doc of reverse

这是因为args需要元组,但是当您使用args=(p.id)时,实际上,python会认为是整数p.id,您可以将源代码django 1.6查看如下:

def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None):
if urlconf is None:
    urlconf = get_urlconf()
resolver = get_resolver(urlconf)
args = args or []
kwargs = kwargs or {}
if prefix is None:
    prefix = get_script_prefix()
if not isinstance(viewname, six.string_types):
    view = viewname
else:
    parts = viewname.split(':')
    parts.reverse()
    view = parts[0]
    path = parts[1:]
    resolved_path = []
    ns_pattern = ''
    while path:
        ns = path.pop()

        # Lookup the name to see if it could be an app identifier
        try:
            app_list = resolver.app_dict[ns]
            # Yes! Path part matches an app in the current Resolver
            if current_app and current_app in app_list:
                # If we are reversing for a particular app,
                # use that namespace
                ns = current_app
            elif ns not in app_list:
                # The name isn't shared by one of the instances
                # (i.e., the default) so just pick the first instance
                # as the default.
                ns = app_list[0]
        except KeyError:
            pass

        try:
            extra, resolver = resolver.namespace_dict[ns]
            resolved_path.append(ns)
            ns_pattern = ns_pattern + extra
        except KeyError as key:
            if resolved_path:
                raise NoReverseMatch(
                    "%s is not a registered namespace inside '%s'" %
                    (key, ':'.join(resolved_path)))
            else:
                raise NoReverseMatch("%s is not a registered namespace" %
                                     key)
    if ns_pattern:
        resolver = get_ns_resolver(ns_pattern, resolver)

return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

看这个iri_to_uri(resolver.u reverse_with_prefix(view,prefix,*args,**kwargs)),它使用*args,所以您应该确保args是一个序列

根据文档,一个项目的元组应该添加逗号来创建,您的代码应该是:

return HttpResponseRedirect(reverse('view_reviews', args=(p.id,)))

由于模式为变量指定了匹配项,因此将其视为关键字参数,因此应将调用调整为reverse。

return HttpResponseRedirect(reverse('view_reviews', kwargs={'product_id':p.id})

相关问题 更多 >