

Django: Putting Form Values in the URL Query String
以下为 快照 页面,建议前往来源网站查看,会有更好的阅读体验。
原文链接: https://snakeycode.wordpress.com/2019/05/03/django-putting-form-values-in-the-url-query-string/
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.
猜你喜欢
-
2
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...
-
2
Remove django form field validation errors manuallyRemove django form field validation errors manually Tue 06 December 2011Original post at Makina Corpus Sometimes I look fo...
-
1
Standard Django Form Invalid, Despite No Errors I was using Django CBV FormView. The form was being flagged as invalid but when I inspected the form inside the form_invalid() method...
-
167
Type check values: `is.string('?') //=> true`
-
2
Apache .htaccess tricks and testing the query string code I put up two more lessons on the "Virtual Rachel" page over the weekend. The first one is a collection of useful things...
-
106
As PHP 7 matures with the 7.2 release, some problematic PHP features have been deprecated, and they are scheduled for removal in PHP 8. If you are dealing with Unicode strings, you may need to act now.
-
49
Imagine a system for editing and reviewing code where:
-
49
Flutter is great - it rocks . We have modern, fresh APIs for building complex UIs in a quite small amount of code. Stateful hot reload is great - we can be five screens deep in our navigation hierarch...
-
81
The big question: Do users want to see video ads next to their private messages?
-
20
"A new iOS update is now available. Please update from the iOS 12 beta." If you’re running Apple’s beta software, you’ve likely seen this notification a lot over the last 24 hours. The company just released a new update that should stop it.
关于极客头条
聚合每日国内外有价值,有趣的链接。