Improve Django View With Methods
I'm looking to improve my Django web application and mainly project scripts. I'm using many times the same Python code and I would like to simplify my views.py file. For example, I
Solution 1:
It looks like the error is because 'IndividuResume' should be a string, not a variable name.
DjangoFormID(request, IndividuFormulaire, 'IndividuResume')
Inside your template, you should use the variable ReverseTemplate, not the hardcoded string 'ReverseTemplate'
return HttpResponseRedirect(reverse(ReverseTemplate, kwargs={'id': post.id}))
The advantage of including the same boilerplate in each view is that it's very easy to follow what is going on. If you are worried about the repetition, I would consider looking at class based views, because they are designed for this kind of use case. I would avoid defining your own DjangoFormID -- if I was new to your project I would not know what DjangoFormID(request, IndividuFormulaire, 'IndividuResume') did, so it would slow me down.
If you do stick with your DjangoFormID method then here's a couple of recommendations:
- Make sure it always returns a response. At the moment, it returns a redirect response for a valid form, and Nonethe rest of the time, which is going to cause problems.
- when I see DjangoFormIDI assume it's a class, not a function. Rename it todjango_form_id.
Post a Comment for "Improve Django View With Methods"