

Django: Putting Form Values in the URL Query String
source link: https://snakeycode.wordpress.com/2019/05/03/django-putting-form-values-in-the-url-query-string/
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: 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 URLs should be beautiful. Not sure I agree any more.
My use case is the user enters some filter parameters, a database query is run and a report is generated. Putting the filter parameters in the query string allows users to return to the page at another time or to share the link.
Here is the code:
from
django.views.generic.edit
import
FormView
from
django.http
import
HttpResponseRedirect
from
django.core.urlresolvers
import
reverse
from
django.utils.http
import
urlencode
class
MyFormView(FormView):
def
form_valid(
self
, form):
base_url
=
reverse(
'filter_tickets_result'
)
# Post process some fields
if
form.cleaned_data[
'job'
]:
form.cleaned_data[
'job'
]
=
form.cleaned_data[
'job'
].
id
qs
=
urlencode(form.cleaned_data)
return
HttpResponseRedirect(base_url
+
'?'
+
qs)
Note, I am using the Django version of urlencode. This avoids issues with Python 2.X and 3.X.
Recommend
-
13
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 ha...
-
12
Inserting Form Values into an Excel Worksheet Using VBA advertisements I'm trying to insert form values into my Excel spreadsheet using vba...
-
5
Sql Query to find duplicates in 2 columns where the values in the first column are the same advertisements I have a table where the first co...
-
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...
-
9
How to write an SQL query to identify duplicate values in a specific field? advertisements This is the table I'm working...
-
10
Problem with query insertion in sqlite? (Values are not inserted) advertisements I am new to Sqlite, I am unable to inser...
-
5
Use the Query String to pass an array of selected values between Blazor PagesApril 28, 2022 · 5 minute read · Tags: blazorA common require...
-
6
The Power Query XML Functions, Missing Or Null Values And Inconsistent Schema Inference A few months ago one of my colleagues at Microsoft, David Browne, showed me an interesting Power Query
-
6
这篇笔记将介绍 reverse、distinct、values 和 values_list 的用法。 本篇笔记目录如下: reverse() values() values_list() distinct() using() 1、reverse() 对 Qu...
-
2
In HTTP request, same query parameter with multiple values are allowed. For example, for query parameter brand, the query string looks like this: brand=foo&brand=bar. How to parse value of this parameter to a list in Fast...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK