A Function-Based view
from django.http import HttpResponse
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)
from django.views import generic
#...
from .models import Question
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
#...
]
In Django 1.4... (End of support Feb. 2013)
def show_list(request, slug=None,
template_name='podcast/show_list.html',
page=0, paginate_by=25, mimetype=None):
"""
Episode list
...
"""
if slug:
shows = Show.objects.filter(slug__exact=slug)
else:
shows = Show.objects.all()
return object_list(
request=request,
queryset=shows,
template_name=template_name,
paginate_by=paginate_by,
page=page)
Also from Django 1.4... (End of support Feb. 2013)
def recipe_list(request, queryset, *args, **kwargs):
recipes = queryset
filter = None
if request.GET:
data = request.GET.copy()
if data.has_key('categoria'):
filter = Category.objects.get(slug=data.get('categoria'))
recipes = recipes.filter(categories=filter)
elif data.has_key('tipo'):
filter = Meal.objects.get(slug=data.get('tipo'))
recipes = recipes.filter(meals=filter)
else:
recipes = recipes.order_by('-id')[:12]
return object_list(request,queryset=recipes,extra_context={'filter':filter})
class LectureSessionListView(DynamicCohortMixin, CurriculumBreadcrumbsMixin,
FrodoHeadlineMixin, generic.ListView):
"""List of all lecture sessions for a particular cohort."""
template_name = "curriculum/lecturesession_list.html"
headline = "Lectures"
def get_queryset(self):
"""Get list of published lecture sessions for this semester."""
cohort = self.get_cohort()
return (LectureSession
.published.filter(cohort=cohort)
.select_related('lecture', 'cohort')
.prefetch_related('staff').order_by('start_at')
)
from django.conf.urls import url
from views import lectures
urlpatterns = [ #...
url(r'^lectures/$', lectures.LectureSessionListView.as_view(),
name='cohort_lectures'), #...
]
Inherits from:
Allows you to:
class BreadcrumbsMixin(object):
"""Provide breadcrumbs to curriculum views.
Things that use this must also the the SetHeadlineMixin.
"""
#...
def get_context_data(self, **kwargs):
context = super(BreadcrumbsMixin, self).get_context_data(**kwargs)
context['breadcrumbs'] = (
self.get_breadcrumbs_root()
+ self.get_breadcrumbs_parents()
+ [{'title': self.get_breadcrumb_headline()}]
)
return context
class NotebookCreateForm(forms.ModelForm):
"""A form to create a notebook and all the drawings in it."""
class Meta:
model = Notebook
fields = ['title', 'description', 'id', 'drawn_at']
widgets = {
'drawn_at': widgets.MonthYearWidget(years=xrange(1980,2050)),
}
class NotebookCreateView(generic.CreateView):
"""Add a notebook and all of its drawings in one go."""
model = Notebook
form_class = NotebookCreateForm
template_name = 'drawings/notebook_create.html'
headline = "Upload a Notebook"
form_invalid_message = "Please correct the error(s)."
form_valid_message = "Information saved."
# "if everything worked out that should have"
def form_valid(self, form):
# can add addl logic here
return super(NotebookCreateView, self).form_valid(form)
Class-based views are classy as hell.