django views redirected to url with parameters
- 2021-09-24 23:07:36
- OfStack
When 1 function is completed, it needs to be redirected to 1 url with parameters
URL
path('peopleapply/<int:jobid>/',second_views.peopleapply,name='peopleapply'),
Function
def peopleapply(request,jobid):
Slightly
Call
return redirect('peopleapply',jobid = people.jbnum_id)
peopleapply
Reset name in url for function
jobid
Is the required parameter name
Supplement: Use of multiple redirection methods in DJANGO
This article mainly explains the redirection methods using HttpResponseRedirect, redirect, reverse and configuring URL in the configuration file
This article uses Django 1.8. 2
Use scenarios, for example, after submitting data in Form 1, you need to return to another specified page to use the redirect method
1. Use HttpResponseRedirect
fuhao The first argument to constructor constructor is required required This can can can to This can fully fully qualified or an absolute path path with no domain. [Parameters can be absolute path and relative path]
from django.http import HttpResponseRedirect
@login_required
def update_time(request):
# Form processing OR Logical processing
return HttpResponseRedirect('/') # Jump to the main interface
# If you need to pass parameters,
return HttpResponseRedirect('/commons/index/?message=error')
2 redirect and reverse
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
#https://docs.djangoproject.com/en/1.8.2/topics/http/shortcuts/
@login_required
def update_time(request):
# Carry out the logic to be processed
return redirect(reverse('test.views.invoice_return_index', args=[])) # Jump to index Interface
redirect is similar to HttpResponseRedirect, but you can also use the url format of the string/.. index/? a=add reverse You can specify the redirected handler directly with the views function, where args is the value that matches url.
3. Others
# Others can also be directly in url Configuration in
from django.views.generic.simple import redirect_to
In url Add to (r'^test/$', redirect_to, {'url': '/author/'}),
# We can even use session Method value transfer of
request.session['error_message'] = 'test'
redirect('%s?error_message=test' % reverse('page_index'))
# These methods are similar to refreshing, and the client reassigns url .
4.
# Redirect, if you need to carry parameters, can you call directly views Medium url Corresponding method to implement it, specified by default 1 Parameters.
# For example view There is a method in baseinfo_account , And then another 1 A url (Corresponding to view The method is blance_account ) To redirect to this baseinfo_account .
#url Configuration in:
urlpatterns = patterns('',
url(r'^index/', 'account.views.index_account'),
url(r'^blance/', 'account.views.blance_account'),
)
# views.py
@login_required
def index_account(request, args=None):
# According to the normal url It's a bit inappropriate to write matching. It doesn't look standard
if args:
print args
return render(request, 'accountuserinfo.html', {"user": user})
@login_required
def blance_account(request):
return index_account(request, {"name": "orangleliu"})
# The test is:
#1 Direct access /index Is it normal (Test ok )
#2 Visit /blance Can you redirect to the /index Page, and get the parameters (test ok The page is /index But the address bar of the browser url Still is /blance )
# Such a redirection with parameters is feasible.