Skip to content

Instantly share code, notes, and snippets.

@trey
Created July 30, 2008 06:02
Show Gist options
  • Save trey/3234 to your computer and use it in GitHub Desktop.
Save trey/3234 to your computer and use it in GitHub Desktop.
Basic layout for doing a form in Django.
from django import forms
from myapp.models import Mymodel
class MyForm(forms.ModelForm):
class Meta:
model=Mymodel
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from myapp.forms import MyForm
def my_view(request):
"""
Documentation for my view.
"""
if request.POST:
form = MyForm(data=request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
# else:
# Errors and validation message taken care of for you. Neat.
else:
form = MyForm()
return render_to_response('my_template.html', {'form': form})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment