2012-06-25 15 views
5

मुझे एक अनुक्रम होना चाहिए "_reverse_with_prefix() तर्क * अनुक्रम होना चाहिए, int नहीं" जब मैं उलटा करने की कोशिश करता हूं। मैंने पहले दृश्य में पैरामीटर को हार्डकोड किया था लेकिन इसे गतिशील बनाने की कोशिश कर रहा हूं। कोई सलाह?रिवर्स के साथ HttpResponseRedirect सही तरीके से कैसे करें?

दृश्य: reverse() अंदर

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)), 
) 
+0

इसके अलावा रीडायरेक्ट शॉर्टकट https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect – super9

उत्तर

17

चेक args=(p.id), यह args=(p.id,) होना चाहिए। अनुक्रम के बजाय पहले रूप को पूर्णांक के रूप में माना जाता है।

Refs the doc और 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).

इसके अलावा, केवल 'view_reviews' के बजाय 'reserve.views.view_reviews' उपयोग करते हैं, इस प्रकार:

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

चेक the doc of reverse

+0

मैं करने के लिए है कि पहले इस्तेमाल किया देखते हैं। जब मैं इसे वापस args = (p.id,) में बदलता हूं तो मुझे त्रुटि मिलती है: तर्क '(1,)' और कीवर्ड तर्क '{}' के साथ 'view_reviews' के लिए उल्टा। – sharataka

+0

@ शारताका अद्यतन की जांच करें, आप [यूआरएल पैटर्न नाम] का उपयोग भी कर सकते हैं (https://docs.djangoproject.com/en/dev/topics/http/urls/#id2) – okm

1

के बाद से अपने पैटर्न एक चर के लिए एक मैच प्रदान करती है, इसे एक कीवर्ड तर्क माना जाता है, इसलिए आपको कॉल को विपरीत करने के लिए समायोजित करना चाहिए।

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

+0

मैंने यह परिवर्तन किया और मुझे त्रुटि "NoReverseMatch at/products/1/add_review /: तर्क '()' और कीवर्ड तर्क '{' product_id ': 1}' के साथ 'view_reviews' के लिए रिवर्स नहीं मिला। यह अजीब बात है क्योंकि जब मैं हार्डकोडेड संस्करण का उपयोग करता हूं, वहां मूल्य 1 के साथ product_id है। साथ ही, मुझे यकीन नहीं है कि त्रुटि "add_review" के लिए क्यों है ... क्या इसे view_reviews पर रीडायरेक्ट नहीं किया जाना चाहिए? – sharataka

+0

इससे कोई फर्क नहीं पड़ता। – okm

0

है कि क्योंकि आर्ग टपल की जरूरत है, लेकिन जब आप आर्ग = (p.id) का उपयोग, वास्तव में, अजगर सोचेंगे पूर्णांक p.id है, तो आप स्रोत कोड के रूप में 1.6 django देख सकते हैं का पालन करें:

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._reverse_with_prefix (देखने के लिए, उपसर्ग, * args, ** kwargs)), यह, * args उपयोग करें ताकि आप आर्ग यह सुनिश्चित करना चाहिए एक दृश्य है,

अनुसार दस्तावेज़, एक आइटम के साथ tuple बनाने के लिए अल्पविराम जोड़ना चाहिए, आप कोड थाई होना चाहिए रों:

return HttpResponseRedirect(reverse('view_reviews', args=(p.id,))) 
संबंधित मुद्दे