Questions tagged [django-class-based-views]

Django Class-based Views are Django views that are represented as classes. These allow you to structure your views and reuse code by harnessing inheritance and mixins.

Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views:

  • Organization of code related to specific HTTP methods (GET, POST, etc) can be addressed by separate methods instead of conditional branching.
  • Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components.
1663 questions
185
votes
13 answers

How to use permission_required decorators on django class-based views

I'm having a bit of trouble understanding how the new CBVs work. My question is this, I need to require login in all the views, and in some of them, specific permissions. In function-based views I do that with @permission_required() and the…
167
votes
8 answers

is not JSON serializable

I have the following code for serializing the queryset: def render_to_response(self, context, **response_kwargs): return HttpResponse(json.simplejson.dumps(list(self.get_queryset())), mimetype="application/json") And…
tuna
  • 6,211
  • 11
  • 43
  • 63
121
votes
5 answers

URL-parameters and logic in Django class-based views (TemplateView)

It is unclear to me how it is best to access URL-parameters in class-based-views in Django 1.5. Consider the following: View: from django.views.generic.base import TemplateView class Yearly(TemplateView): template_name =…
user1319936
82
votes
5 answers

What is the advantage of Class-Based views?

I read today that Django 1.3 alpha is shipping, and the most touted new feature is the introduction of class-based views. I've read the relevant documentation, but I find difficult to see the big advantage™ that I could get by using them, so I'm…
Agos
  • 18,542
  • 11
  • 56
  • 70
67
votes
1 answer

How does the order of mixins affect the derived class?

Say, I have the following mixins that overlaps with each other by touching dispatch(): class FooMixin(object): def dispatch(self, *args, **kwargs): # perform check A ... return super(FooMixin, self).dispatch(*args,…
61
votes
4 answers

Example of Django Class-Based DeleteView

Does anyone know of or can anyone please produce a simple example of Django's class-based generic DeleteView? I want to subclass DeleteView and ensure that the currently logged-in user has ownership of the object before it's deleted. Any help would…
Lockjaw
  • 1,882
  • 2
  • 16
  • 15
60
votes
7 answers

django class-based views with inline model-form or formset

I have the following models: class Bill(models.Model): date = models.DateTimeField(_("Date of bill"),null=True,blank=True) class Item(models.Model): name = models.CharField(_("Name"),max_length=100) price =…
Hixi
  • 765
  • 1
  • 8
  • 9
57
votes
5 answers

How to set ForeignKey in CreateView?

I have a model: class Article(models.Model): text = models.CharField() author = models.ForeignKey(User) How do I write class-based view that creates a new model instance and sets author foreign key to request.user? Update: Solution moved to…
Vlad T.
  • 2,568
  • 3
  • 26
  • 40
53
votes
5 answers

Django - Class Based Generic View - "No URL to redirect to"

I'm using the generic CreateView like: #urls.py from django.conf.urls.defaults import * from django.views.generic import CreateView from content.models import myModel urlpatterns = patterns('myApp.views', (r'myCreate/$',…
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
50
votes
4 answers

Getting __init__() got an unexpected keyword argument 'instance' with CreateView of Django

Some details: Request Method: GET Request URL: http://localhost:8080/user/create Django Version: 1.5.1 Exception Type: TypeError Exception Value: ____init____() got an unexpected keyword argument 'instance' Exception Location:…
chachan
  • 2,382
  • 1
  • 26
  • 41
49
votes
1 answer

How do you use get_context_data with TemplateView in Django

I'm trying to do something like this: class AboutView(TemplateView): template_name = 'about.html' def get_context_data(self, **kwargs): context = super(AboutView, self).get_context_data(**kwargs) context['dahl_books'] =…
9-bits
  • 10,395
  • 21
  • 61
  • 83
48
votes
2 answers

django createview how to get the object that is created

i've two concatenated form. Basically user fills in the first form and then is redirected to the second one which adds value to the data of the first form. E.G. I've a form Movie (first form) and then i'm redirected to the form (actor) which add the…
EsseTi
  • 4,079
  • 5
  • 36
  • 63
47
votes
6 answers

'function' object has no attribute 'as_view'

I am trying to use class based views, and get a strange error. The way I'm using the view seems to be the normal way: ingredients/models.py: from django.db import models from django.utils import timezone class Ingredient(models.Model): name …
codyc4321
  • 9,014
  • 22
  • 92
  • 165
47
votes
1 answer

Django, name parameter in urlpatterns

I'm following a tutorial where my urlpatterns are: urlpatterns = patterns('', url(r'^passwords/$', PasswordListView.as_view(), name='passwords_api_root'), url(r'^passwords/(?P[0-9]+)$', PasswordInstanceView.as_view(),…
Leonardo
  • 4,046
  • 5
  • 44
  • 85
42
votes
8 answers

Django Class Based View for both Create and Update

Say I want to create a Class Based View which both updates and creates an object. From a previous question I worked out I could do one of the following things: 1) Use 2 generic views CreateView and UpdateView which I think would mean having two…
GrantU
  • 6,325
  • 16
  • 59
  • 89
1
2 3
99 100