Created
May 22, 2015 18:51
-
-
Save kennethlove/40a20b25bdbf5b0094ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- authors/templates/authors/form.html --> | |
<form method="POST"> | |
{% csrf_token %} | |
{{ form.as_p }} | |
<input type="submit"> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from .models import Author | |
class AuthorForm(models.ModelForm): | |
class Meta: | |
model = Author |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Author(models.Model): | |
name = models.CharField(max_length=255) | |
bio = models.TextField() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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