

Django: Passing Form Values in URL
source link: https://snakeycode.wordpress.com/2020/05/05/django-passing-form-values-in-url/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Standard
Django: Passing Form Values in URL
Lets say you have a form that is used to drive the next page. For example, it might contain parameters for running a report. One way to handle this is to pass the parameters to the report view and run the report in the get_context section of that view.
If you only have a few simple parameters, you could put them as part of the URL. In the past, I would put them in the session and pull them from the session for the report view. This turned out to be a bad choice because users would save the report URL and return to it later; maybe the next week. By that time the session data would no longer be available.
An approach that avoids this problem is to put the form data into the query string of the url. Here is how to do that:
from
django.utils.http
import
urlencode
from
django.core.urlresolvers
import
reverse
def
form_valid(
self
, form):
qs
=
urlencode(form.cleaned_data)
url
=
'?'
.join([reverse(
'my_report'
), qs])
return
HttpResponseRedirect(url)
You may want to alter some of the form.cleaned data to make it easier to use in the report view. For example, if one of the form fields returns an instance, replace the instance with the instance ID.
Recommend
-
15
URL reverse en Javascript avec djangoURL reverse en Javascript avec django Fri 27 May 2011Article original publié chez Makina Corpus Un aspect fondamental de la philosophie djan...
-
23
Standard Django: Putting Form Values in the URL Query String I used to avoid using URLs with long query strings. This was partially due to an essay I read by a Big Shot about how URL...
-
12
Inserting Form Values into an Excel Worksheet Using VBA advertisements I'm trying to insert form values into my Excel spreadsheet using vba...
-
14
PHP passing values to variables in included files advertisements I have a php file (article.php) with variables holding con...
-
10
How do I get Perl to read the values of my html form as unicode? advertisements I have an html form that sends data to .cgi page. Here is the htm...
-
8
django下url函数的用法-阳明的博客|Kubernetes|Istio|Prometheus|Python|Golang|云原生 Django下有一个比较隐含的函数url,在django/conf/urls/defaults模块中,虽然只有短短的10行代码,但功能却很了得。起初初学...
-
4
Passing values eats my URLs advertisements Just upgraded from 1.3 to 2.0.3 and I'm trying to migrate all the changes. I'm...
-
25
Rajesh Shivajirao Shinde Jul 26, 2021 at 08:03 PM SAP PO 7.5 REST Lookup - Issues in passing dynamic values in HTTP Header and custom E...
-
11
A better way for passing Django context Django • Nov 27, 2022 I like Python as a language and Django as a framework. They are sup...
-
9
Jeevitha Palanisamy May 11, 2023 4 minute...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK