- Author:
- SmileyChris
- Posted:
- April 7, 2009
- Language:
- Python
- Version:
- 1.0
- Score:
- 1 (after 3 ratings)
Using this small helper, you can instanciate your forms in an even DRYer way:
form = MyForm(**form_kwargs(request))
if form.is_valid():
#...
1 2 3 4 5 6 | def form_kwargs(request):
kwargs = {}
if request.method == 'POST':
kwargs['data'] = request.POST
kwargs['files'] = request.FILES
return kwargs
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 9 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
Maybe its just me, but I don't see how this is "DRY"er. Not even sure what the point of it is.
#
Normally, you'd write:
Now you write:
#
PS: if you'd rather not bother with a helper like this, the method of instanciating a form only once for both cases is still useful.
The most simple case is (where files aren't involved and you always expect at least one thing in your POST):
#
Please login first before commenting.