Skip to content

Instantly share code, notes, and snippets.

@ricleal
Created March 28, 2018 21:44
Show Gist options
  • Save ricleal/c1032def33a895ebeb228ce2efe3ab7a to your computer and use it in GitHub Desktop.
Save ricleal/c1032def33a895ebeb228ce2efe3ab7a to your computer and use it in GitHub Desktop.
Overriding as_view in Django
#coding: utf-8
from django.conf.urls import url
from .views import PostListView, DummyView
urlpatterns = [
url(r'^$', PostListView.as_view(), name='list'),
url(r'^dummy/$', DummyView.as_view(), name='dummy'),
]
#from django.shortcuts import render
from .models import Post
from django.views.generic import ListView, DetailView, CreateView
from django.utils.decorators import classonlymethod
class DummyView(object):
@classonlymethod
def as_view(cls, **initkwargs):
print("* This class name: {}".format(cls.__name__))
view = PostListView.as_view(**initkwargs)
return view
class PostListView(ListView):
model = Post
# template_name = "blog/post_list"
# queryset = Post.objects.all()
queryset = Post.objects.filter(published=True).order_by('-modified_date')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment