Skip to content

Instantly share code, notes, and snippets.

@jacobian
Created February 25, 2010 15:30
Show Gist options
  • Save jacobian/314630 to your computer and use it in GitHub Desktop.
Save jacobian/314630 to your computer and use it in GitHub Desktop.
#### yabl/urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^authors/', include('yabl.authors.urls')),
(r'^entries/', include('yabl.entries.urls')),
(r'^contact/', include('yabl.contact.urls')),
)
#### yabl/contact/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('yabl.contact.views',
url('^$', 'contact', name='contact_view'),
url('^done/$', 'contact_done', name='contact_is_done'),
)
#### yabl/contact/forms.py
from django import forms
SUBJECT_CHOICES = [
("hi", "Just getting in touch"),
("help", "I need help!"),
("wtf", "What's going on here?"),
]
class ContactForm(forms.Form):
name = forms.CharField(max_length=100, label="Your name")
subject = forms.ChoiceField(choices=SUBJECT_CHOICES,
widget=forms.RadioSelect())
message = forms.CharField(widget=forms.Textarea())
sender = forms.EmailField()
#### yabl/contact/views.py
from django.shortcuts import render_to_response, redirect
from yabl.contact.forms import ContactForm
def contact(request):
initial_data = {"subject": "wtf"}
if request.method == 'POST':
form = ContactForm(request.POST, initial=initial_data)
if form.is_valid():
print form.cleaned_data
return redirect('contact_is_done')
else:
form = ContactForm(initial=initial_data)
return render_to_response('contact/form.html', {
'form': form,
})
def contact_done(request):
return render_to_response('contact/done.html')
#### yabl/templates/contact/form.html
{% extends "base.html" %}
{% block content %}
<form action="{% url contact_view %}" method="post">
<table>{{ form.as_table }}</table>
<input type=submit>
</form>
{% endblock content %}
#### yabl/templates/contact/form.html v2
{% extends "base.html" %}
{% block content %}
<form action="{% url contact_view %}" method="post">
<dl>
{% for field in form %}
<dt>{{ field.label_tag }}</dt>
<dd>{{ field.errors }} {{ field }}</dd>
{% endfor %}
</dl>
<input type=submit>
</form>
{% endblock content %}
#### yabl/templates/contact/done.html
{% extends "base.html" %}
{% block content %}
<h1>Thank you!</h1>
{% endblock content %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment