Skip to content

Instantly share code, notes, and snippets.

@jacobian
Created March 11, 2010 16:10
Show Gist options
  • Save jacobian/329284 to your computer and use it in GitHub Desktop.
Save jacobian/329284 to your computer and use it in GitHub Desktop.
##### yabl/urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^authors/', include('yabl.authors.urls')),
(r'^contact/', include('yabl.contact.urls')),
(r'^entries/', include('yabl.entries.urls')),
(r'^admin/', include(admin.site.urls)),
)
##### yabl/authors/forms.py
from django import forms
from yabl.authors.models import Author
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
##### yabl/authors/urls.py
from django.conf.urls.defaults import *
urlpatterns = patterns('yabl.authors.views',
url(r'^$', 'author_list', name='author_list'),
url(r'^new/$', 'author_edit', name='author_create'),
url(r'^(\d+)/$', 'author_detail', name='author_detail'),
url(r'^(\d+)/edit/$', 'author_edit', name='author_edit'),
)
##### yabl/authors/views.py
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.shortcuts import redirect
from yabl.authors.models import Author
from yabl.authors.forms import AuthorForm
def author_list(request):
authors = Author.objects.all()
return render_to_response(
"authors/list.html",
{"authors": authors},
)
def author_detail(request, author_id):
author = get_object_or_404(Author, id=author_id)
return render_to_response(
"authors/detail.html",
{"author": author}
)
def author_edit(request, author_id=None):
if author_id:
author = get_object_or_404(Author, id=author_id)
else:
author = None
# Python 2.6:
# author = get_object_or_404(Author, id=author_id) if author_id else None
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,
})
###### yabl/templates/authors/form.html
{% extends "base.html" %}
{% block content %}
<form action="" 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