-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjango_test.py
More file actions
34 lines (30 loc) · 1.13 KB
/
django_test.py
File metadata and controls
34 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from django import forms
from django.http import HttpResponseRedirect
from django.shortcuts import redirect, render
def send_message(name, message):
# Code for actually sending the message goes here
pass
class ContactForm(forms.Form):
name = forms.CharField()
message = forms.CharField(widget=forms.Textarea)
def contact_view(request):
# The request method 'POST' indicates
# that the form was submitted
if request.method == 'POST': # 1
# Create a form instance with the submitted data
form = ContactForm(request.POST) # 2
# Validate the form
if form.is_valid(): # 3
# If the form is valid, perform some kind of
# operation, for example sending a message
send_message(
form.cleaned_data['name'],
form.cleaned_data['message']
)
# After the operation was successful,
# redirect to some other page
return redirect('/success/') # 4
else: # 5
# Create an empty form instance
form = ContactForm()
return render(request, 'contact_form.html', {'form': form})