i am trying to use django reverse to route back to a page which take an argument vendor_id
.
我正在尝试使用django reverse来路由回到一个带有参数vendor_id的页面。
I am trying to do it this way using this post as reference: POST
我试图这样做使用这篇文章作为参考:POST
return HttpResponseRedirect(reverse('vendor_data', kwargs={'vendor_id':vendor_id}))
My view method which should be called:
我应该调用的视图方法:
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:
我的urls.py:
url(r'^vendor_data/', views.vendorData, name='vendor_data'),
If i dont pass any argument then it shows the HttpResponse from my views method. If i pass argument vendor_id
like this:
如果我没有传递任何参数,那么它会从我的views方法中显示HttpResponse。如果我像这样传递参数vendor_id:
return HttpResponseRedirect(reverse('vendor_data', kwargs={'vendor_id':vendor_id}))
It throws an error. The traceback is:
它抛出一个错误。追溯是:
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/']
How to solve this?
怎么解决这个?
1 个解决方案
#1
1
What you are expecting inside your view
is a GET
parameter from query string
. Something your expected URL should be something like,
您在视图中期望的是来自查询字符串的GET参数。您期望的URL应该是类似的东西,
www.example.com/vendor/?vendor_id=12345
And below,
在下面,
return HttpResponseRedirect(reverse('vendor_data', kwargs={'vendor_id':vendor_id}))
will redirect you to something like,
将你重定向到像,
www.example.com/vendor/12345
For which your view
should expect an additional parameter vendor_id
,
您的视图应该有一个额外的参数vendor_id,
def vendorData(request, vendor_id):
and accordingly your rest of the code might change.
因此你的其余代码可能会改变。
Solution to your problem:
解决您的问题:
return HttpResponseRedirect("{}?vendor_id={}".format(reverse('vendor_data'), vendor_id))
#1
1
What you are expecting inside your view
is a GET
parameter from query string
. Something your expected URL should be something like,
您在视图中期望的是来自查询字符串的GET参数。您期望的URL应该是类似的东西,
www.example.com/vendor/?vendor_id=12345
And below,
在下面,
return HttpResponseRedirect(reverse('vendor_data', kwargs={'vendor_id':vendor_id}))
will redirect you to something like,
将你重定向到像,
www.example.com/vendor/12345
For which your view
should expect an additional parameter vendor_id
,
您的视图应该有一个额外的参数vendor_id,
def vendorData(request, vendor_id):
and accordingly your rest of the code might change.
因此你的其余代码可能会改变。
Solution to your problem:
解决您的问题:
return HttpResponseRedirect("{}?vendor_id={}".format(reverse('vendor_data'), vendor_id))