django-class-based-views

Django: Accessing request in forms.py clean function

谁都会走 提交于 2019-11-27 01:55:21
问题 Hi Stackoverflow people, In my clean function in forms.py, I would like to save automatically some information in a session variable. However, I do not seem to get access to the request variable. All examples for handing over the request variable are based on function based views, but here I am using a class based view. My forms.py: from django import forms from item.models import Item class CreateItemForm(forms.ModelForm): class Meta: model = Item fields = ('name', 'description') def __init_

Set initial value to modelform in class based generic views

家住魔仙堡 提交于 2019-11-27 01:10:58
问题 I'm using Class based generic views, can anybody suggest me how can i set the initial values to update form? I tried using get_initial() method but didn't got any success. Following is the code which i tried class IncidentUpdateView(UpdateView): form_class = IncidentForm form_class.initial = {"badge_number": '88888'} model = Incident template_name = 'hse/incident/incident_update.html' def get_initial(self, form_class): initials = { "badge_number": '88888' } form = form_class(initial=initials)

Django: Search form in Class Based ListView

自作多情 提交于 2019-11-26 19:12:27
问题 I am trying to realize a Class Based ListView which displays a selection of a table set. If the site is requested the first time, the dataset should be displayed. I would prefer a POST submission, but GET is also fine. That is a problem, which was easy to handle with function based views , however with class based views I have a hard time to get my head around. My problem is that I get a various number of error, which are caused by my limited understanding of the classed based views. I have

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

亡梦爱人 提交于 2019-11-26 15:35:58
问题 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. ` 回答1: How about overriding form

Multiple form classes in django generic (class) views

寵の児 提交于 2019-11-26 14:16:01
问题 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

How to use permission_required decorators on django class-based views

妖精的绣舞 提交于 2019-11-26 14:02:17
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 login_required attribute in the view, but I don't know how to do this on the new views. Is there some section in the django docs explaining this? I didn't found anything. What is wrong in my code? I tried to use the @method_decorator but it replies " TypeError at /spaces/prueba/ _wrapped_view() takes at least 1 argument (0 given) " Here is the code (GPL):

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

六月ゝ 毕业季﹏ 提交于 2019-11-26 12:54:06
问题 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

Example of Django Class-Based DeleteView

≡放荡痞女 提交于 2019-11-26 12:51:40
问题 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. 回答1: 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

How to use permission_required decorators on django class-based views

て烟熏妆下的殇ゞ 提交于 2019-11-26 12:16:10
问题 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 login_required attribute in the view, but I don\'t know how to do this on the new views. Is there some section in the django docs explaining this? I didn\'t found anything. What is wrong in my code? I tried to use the @method_decorator but it replies \"

How to set ForeignKey in CreateView?

不问归期 提交于 2019-11-26 10:35:03
问题 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. 回答1: 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