Reputation: 5132
I am getting an error "_reverse_with_prefix() argument after * must be a sequence, not int" when I try to reverse. I previously hardcoded the parameter in the view but am trying to make it dynamic. Any advice?
View:
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)),
)
Upvotes: 5
Views: 10847
Reputation: 404
that's because the args need tuple, but when you use the args=(p.id), actually, the python will think is integer p.id, you can look the source code django 1.6 as follow:
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))
look this iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)), it use *args, so you should ensure args is a sequence,
according the docs, tuple with one item should add the comma to create, you code should be this:
return HttpResponseRedirect(reverse('view_reviews', args=(p.id,)))
Upvotes: 0
Reputation: 23871
Check args=(p.id)
inside the reverse()
, it must be args=(p.id,)
. The first form is treated as integer instead of a sequence.
Refs the doc and the 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).
Also, use 'reserve.views.view_reviews'
instead of merely 'view_reviews'
, thus:
reverse('reserve.views.view_reviews', args=(p.id,))
Check the doc of reverse
Upvotes: 21
Reputation: 174614
Since your pattern assigns a match to a variable, it is considered a keyword argument, so you should adjust the call to reverse.
return HttpResponseRedirect(reverse('view_reviews', kwargs={'product_id':p.id})
Upvotes: 2