Skip to content

Instantly share code, notes, and snippets.

@jacobian
Created February 25, 2010 16:35
Show Gist options
  • Save jacobian/314699 to your computer and use it in GitHub Desktop.
Save jacobian/314699 to your computer and use it in GitHub Desktop.
#### yabl/authors/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('yabl.authors.views',
url(r'^$', 'index', name="author_index"),
url(r'^(\d+)/$', 'detail', name="author_detail"),
url(r'^(\d+)/edit/$', 'edit', name="author_edit"),
)
#### yabl/authors/forms.py
from django import forms
from yabl.authors.models import Author
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
#### yabl/authors/views.py
from django.shortcuts import render_to_response, get_object_or_404, redirect
from yabl.authors.models import Author
from yabl.authors.forms import AuthorForm
def index(request):
return render_to_response('authors/index.html', {
'authors': Author.objects.all()
})
def detail(request, id):
return render_to_response('authors/detail.html', {
'author': get_object_or_404(Author, pk=id),
})
def edit(request, id):
author = get_object_or_404(Author, id=id)
if request.method == 'POST':
form = AuthorForm(request.POST, instance=author)
if form.is_valid():
author = form.save()
return redirect("author_detail", author.id)
else:
form = AuthorForm(instance=author)
return render_to_response("authors/form.html", {
'form': form,
'author': author,
})
#### yabl/templates/authors/form.html
{% extends "base.html" %}
{% block title %}Edit {{ author.name }}{% endblock %}
{% block content %}
<form action="{% url author_edit author.id %}" method="post">
<table>{{ form.as_table }}</table>
<input type=submit>
</form>
{% endblock content %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment