Created
March 28, 2018 21:44
-
-
Save ricleal/c1032def33a895ebeb228ce2efe3ab7a to your computer and use it in GitHub Desktop.
Overriding as_view in Django
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
#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'), | |
] |
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.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