问题
I have a home in which I have a form that I get some info from students to suggest them some programs to apply to. The home view is as below:
def home(request):
template_name = 'home.html'
home_context = {}
if request.POST:
my_form = MyModelForm(request.POST)
if my_form.is_valid():
# do some stuff
return programs(request)
else:
my_form = MyModelForm()
home_context.update({'my_form': my_form, })
return render(request, template_name, home_context)
In the second view, I have the same form and I want this form to be pre-occupied with the information I entered in the home page. That is why in the above, I passed my POST request to programs view that is as below:
def programs(request):
template_name = 'programs.html'
programs_context = {}
if request.POST:
my_form = MyModelForm(request.POST)
if my_form.is_valid():
# do some other stuff
else:
my_form = MyModelForm()
programs_context.update({'my_form': my_form, })
return render(request, template_name, programs_context)
The drawback of this strategy (passing the POST request in the home view to the programs_view) is that the url in url bar does not change to 'example.com/programs' and stays as 'example.com' . I will have some problems including problems in pagination of the programs. The alternative is that I do this:
def home(request):
template_name = 'home.html'
home_context = {}
if request.POST:
my_form = MyModelForm(request.POST)
if my_form.is_valid():
# do some stuff
querystring = request.POST
return redirect(reverse('programs') + '?' + parse_qs(querystring, keep_blank_values=True))
else:
my_form = MyModelForm()
home_context.update({'my_form': my_form, })
return render(request, template_name, home_context)
First Of all, I get an error when I do this: 'QueryDict' object has no attribute 'decode'
second, I do not know what to do in the programs view to make use of the query I am sending to the get branch of the programs view.
Third, I need to the stuff I used to do in the post branch of the programs view in get branch of the programs view if it is from a redirect not from an independent direct get request. How can I distinguish this in programs get request?
Overall, any alternative solution and help are highly appreciated.
回答1:
Use request.GET['your_key'] to get data passed in a query string.
You can check this SO answer.
回答2:
That is not at all what parse_qs does. Parsing is taking a string - ie a querystring - and turning it into usable objects; what you want to do is the opposite, taking the POST and turning it into a querystring; ie encoding it, not parsing it.
Note, despite what you've called it, request.POST is not a querystring, it's a QueryDict. To encode that dict into a string you can use the urlencode method: request.POST.urlencode()
However this still won't work, because a redirect is always a GET and your destination form is expecting a POST. This isn't really the right approach at all; instead what I would do would be to store the querydict in the session and use it from there in the second view.
回答3:
I have a home in which I have a form that I get some info from students to suggest them some programs to apply to. The home view is as below:
def home(request):
template_name = 'home.html'
home_context = {}
if request.POST:
my_form = MyModelForm(request.POST)
if my_form.is_valid():
querystring = urlencode(request.POST)
return redirect(reverse('programs') + '?' + querystring)
else:
my_form = MyModelForm()
home_context.update({'my_form': my_form, })
return render(request, template_name, home_context)
and this in the programs view
def programs(request):
template_name = 'programs.html'
programs_context = {}
if request.POST:
my_form = MyModelForm(request.POST)
if my_form.is_valid():
# do some other stuff
else:
# if the get is from redirect
my_form = MyModelForm(request.GET)
# do some other stuff
# else:
my_form = MyModelForm()
programs_context.update({'my_form': my_form, })
return render(request, template_name, programs_context)
I now want to devide the get method of the programs view into to parts by checking whether a request is coming from entering example.com/programs or it coming from the redirect part of the home view. What is the standard method to do this? What is the standard way of writing the commented if and else above?
来源:https://stackoverflow.com/questions/57941731/how-to-use-parse-qs-in-redirecting-from-view-to-another