Skip to content

Instantly share code, notes, and snippets.

@kennethlove
Created May 22, 2015 18:51
Show Gist options
  • Save kennethlove/40a20b25bdbf5b0094ca to your computer and use it in GitHub Desktop.
Save kennethlove/40a20b25bdbf5b0094ca to your computer and use it in GitHub Desktop.
<!-- authors/templates/authors/form.html -->
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
from django import forms
from .models import Author
class AuthorForm(models.ModelForm):
class Meta:
model = Author
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=255)
bio = models.TextField()
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import AuthorForm
def author_create(request):
if request.method == 'GET':
form = AuthorForm()
else:
form = AuthorForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
return render(request, 'authors/form.html', {'form': form})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment