
Hier das Formular:
Code: Alles auswählen
from django import forms
class contact(forms.Form):
vorname = forms.CharField()
nachname = forms.CharField()
email = forms.EmailField()
nachricht = forms.CharField(widget=forms.Textarea)
Code: Alles auswählen
from django.conf.urls.defaults import patterns, include, url
from form.views import form_view, thanks_view
urlpatterns = patterns('',
url('^form/$', form_view),
url('^thanks/$', thanks_view),
)
Code: Alles auswählen
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.core.mail import send_mail
from form.forms import contact
def form_view(request):
if request.method == 'POST':
a = contact(request.POST)
if a.is_valid():
b = a.cleaned_data
send_mail(
b['vorname'],
b['nachname'],
b['email'],
b['nachricht'],
b.get('email', 'xyz@abc.de'),
['xyz@abc.de'],
)
return HttpResponseRedirect('/thanks/')
else:
a = contact()
return render_to_response('contact.html', {'a': a})
def thanks_view(request):
return render_to_response('thanks.html')
Code: Alles auswählen
<html>
<head>
<title>Contact us</title>
</head>
<body>
<h1>Contact us</h1>
{% if a.errors %}
<p style="color: red;">
Please correct the error{{ a.errors|pluralize }} below.
</p>
{% endif %}
<form action="." method="post">
<table>
{{ a.as_table }}
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
Code: Alles auswählen
<html>
<head>
<title>Thank You!</title>
</head>
<body>
<h1>Thank You!</h1>
</body>
</html>
Ich verstehe nicht warum es nicht funktioniert! Man wird onsubmit immer direkt zur url /thanks/ weitergeleitet, ohne dass die Eingaben geprüft werden. Ich hab die Validierung in der shell probiert.. die funktioniert auf jeden fall. Was mache ich falsch ?!

Edit: Arbeite grad die dokumentation durch (http://www.djangobook.com/en/2.0/chapter07/)