我正在尝试使用django reverse来路由回到一个带有参数vendor_id
的页面。
我正在尝试以这种方式使用此帖子作为参考:POST
return HttpResponseRedirect(reverse('vendor_data', kwargs={'vendor_id':vendor_id}))
我应该调用的视图方法:
def vendorData(request):
vendors = Vendors()
if request.method == 'GET':
vendor_id = request.GET.get('vendor_id')
if vendors.checkValidVendorId(vendor_id) == False:
return HttpResponse('<h2>404<br>Invalid Vendor Id.</h2>')
else:
vendor_details = vendors.getVendorData(vendor_id)
.....
return render(request, 'vendor_data.html', context_dict)
my urls.py:
url(r'^vendor_data/', views.vendorData, name='vendor_data'),
如果我没有传递任何参数,那么它会从我的views方法中显示HttpResponse。
如果我像这样传递参数vendor_id
:
return HttpResponseRedirect(reverse('vendor_data', kwargs={'vendor_id':vendor_id}))
它会抛出错误。追溯是:
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Manish\Desktop\ScratcherAdmin\ScratcherAdmin\vendors\views.py" in editVendorData
162. return HttpResponseRedirect(reverse('vendor_data', kwargs={'vendor_id':vendor_id}))
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in reverse
579. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
496. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /update_vendor_data/
Exception Value: Reverse for 'vendor_data' with arguments '()' and keyword arguments '{'vendor_id': u'20150001'}' not found. 1 pattern(s) tried: ['vendor_data/']
如何解决这个问题?
答案 0 :(得分:1)
view
中您所期望的是来自GET
的{{1}}参数。您期望的URL应该是类似的东西,
query string
以下,
www.example.com/vendor/?vendor_id=12345
会将您重定向到类似
的内容return HttpResponseRedirect(reverse('vendor_data', kwargs={'vendor_id':vendor_id}))
您的www.example.com/vendor/12345
应该有一个额外的参数view
,
vendor_id
因此您的其余代码可能会发生变化。
解决您的问题:
def vendorData(request, vendor_id):