I have some problem to figure out how new django views (template view) and forms can works I also can’t find good resources, official doc don’t explain me how can get request ( I mean get and post) and forms in new django views class
Thanks
added for better explain
for example I have this form :
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)
and this is the code for read and print the form (old fashion way):
def contact(request):
if request.method == 'POST': # If the form has been submitted...
form = ContactForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = ContactForm() # An unbound form
return render_to_response('contact.html', {
'form': form,
})
well my question is how you can do the same thing with template view thanks
I would recommend just plodding through the official tutorial and I think realization will dawn and enlightenment will come automatically.
Basically:
When you issue a request: ”’http://mydomain/myblog/foo/bar”’
Django will:
myblog/foo/barto a function/method call through the patterns defined in urls.pymyblog.views.foo_bar_index(request).The view function usually does the following:
The template generic view allows you to skip writing that function, and just pass in the context dictionary.
Quoting the django docs:
All views.generic.*View classes have views.generic.View as their base. In the docs to that you find the information you require.
Basically:
MyView.as_view will generate a callable that calls views.generic.View.dispatch()
which in turn will call MyView.get(), MyView.post(), MyView.update() etc.
which you can override.
To quote the docs:
The big plusses of the class based views (in my opinion):