django-class-based-views

Accessing request.user in class based generic view CreateView in order to set FK field in Django

十年热恋 提交于 2019-11-27 11:41:32
So I have a model that includes: class Place(models.Model): .... created_by = models.ForeignKey(User) My view is like so: class PlaceFormView(CreateView): form_class = PlaceForm @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(PlaceFormView, self).dispatch(*args, **kwargs) Is there a way for me to access request.user and set created_by to that user? I've looked through the docs, but can't seem to find any hints toward this. ` How about overriding form_valid which does the form saving? Save it yourself, do whatever you want to it, then do the redirect. class

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

元气小坏坏 提交于 2019-11-27 11:04:17
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 = "calendars/yearly.html" current_year = datetime.datetime.now().year current_month = datetime.datetime.now().month def get_context_data(self, **kwargs): context = super(Yearly, self).get_context_data(**kwargs) context['current_year'] = self.current_year context['current_month'] = self.current_month return context URLCONF: from .views import Yearly urlpatterns = patterns('', url

Multiple form classes in django generic (class) views

蓝咒 提交于 2019-11-27 08:40:42
I'd like to use the class based generic views of django 1.3 for forms, but sometimes have to manage multiple form classes in one form. However, it looks like the existing views based on FormMixin assume a single form class. Is this possible with generic views and how would I do it? EDIT: to clarify, I have one form but more than one (ModelForm based) class. For example in the inline_formset example in the django docs, I would want to present a page where an author and his books can be edited at once, in a single form: author_form = AuthorForm(request.POST, instance = author) books_formset =

How does the order of mixins affect the derived class?

隐身守侯 提交于 2019-11-27 07:04:21
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, **kwargs) class BarMixin(object): def dispatch(self, *args, **kwargs): # perform check B ... return super(FooMixin, self).dispatch(*args, **kwargs) If I want my view to go through the order, check A -> check B, should my code be MyView(FooMixin, BarMixin, View) or MyView(BarMixin, FooMixin, View) ? And why do we always put View or its subclasses after mixins? (I have noticed this

How to set ForeignKey in CreateView?

走远了吗. 提交于 2019-11-27 06:53:05
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 separate answer below. I solved this by overriding form_valid method. Here is verbose style to clarify things: class CreateArticle(CreateView): model = Article def form_valid(self, form): article = form.save(commit=False) article.author = self.request.user #article.save() # This is redundant, see comments. return super(CreateArticle, self).form_valid(form)

Django: Can class-based views accept two forms at a time?

陌路散爱 提交于 2019-11-27 06:25:44
If I have two forms: class ContactForm(forms.Form): name = forms.CharField() message = forms.CharField(widget=forms.Textarea) class SocialForm(forms.Form): name = forms.CharField() message = forms.CharField(widget=forms.Textarea) and wanted to use a class based view, and send both forms to the template, is that even possible? class TestView(FormView): template_name = 'contact.html' form_class = ContactForm It seems the FormView can only accept one form at a time. In function based view though I can easily send two forms to my template and retrieve the content of both within the request.POST

Example of Django Class-Based DeleteView

我的未来我决定 提交于 2019-11-27 06:08:38
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 be very much appreciated. Thank you in advance. Here's a simple one: from django.views.generic import DeleteView from django.http import Http404 class MyDeleteView(DeleteView): def get_object(self, queryset=None): """ Hook to ensure object is owned by request.user. """ obj = super(MyDeleteView, self).get_object() if not obj.owner == self.request

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

一个人想着一个人 提交于 2019-11-27 04:02:23
问题 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 = "calendars/yearly.html" current_year = datetime.datetime.now().year current_month = datetime.datetime.now().month def get_context_data(self, **kwargs): context = super(Yearly, self).get_context_data(**kwargs) context['current_year'] = self.current_year context['current_month'

How does the order of mixins affect the derived class?

冷暖自知 提交于 2019-11-27 03:58:59
问题 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, **kwargs) class BarMixin(object): def dispatch(self, *args, **kwargs): # perform check B ... return super(FooMixin, self).dispatch(*args, **kwargs) If I want my view to go through the order, check A -> check B, should my code be MyView(FooMixin, BarMixin, View) or MyView(BarMixin,

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

ぃ、小莉子 提交于 2019-11-27 03:43:40
问题 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: /place/venv/local/lib/python2.7/site-packages/django/views/generic/edit.py in get_form, line 35 Python Executable: /place/venv/bin/python Python Version: 2.7.3 views.py class UserCreateView(CreateView): model = models.User form_class = forms.UserForm urls.py url(r'^user