django-class-based-views

Django: Accessing request in forms.py clean function

拜拜、爱过 提交于 2019-11-28 07:42:16
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__(self, request, *args, **kwargs): self.request = request super(CreateItemForm, self).__init__(*args, *

How do I pass a parent id as an fk to child object's ModelForm using generic class-based views in Django?

China☆狼群 提交于 2019-11-28 06:07:51
问题 I am trying to use Django Generic Class-Based Views to build a CRUD interface to a two-model database. I have a working CRUD interface to the parent model, and am stuck trying to get the child Create working. For consistency with other Django examples, take the parent to be Author and the child to be Book. What is the simplest way to allow users to add Books to an Author? In HTML terms, I think that I want to make a link on the Author detail page that includes the ID of the Author, have that

Set initial value to modelform in class based generic views

人盡茶涼 提交于 2019-11-28 05:51:51
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) return form def get_success_url(self): return reverse_lazy('hse-incident', args=[self.object.id]) You

Sending request.user object to ModelForm from class based generic view in Django

删除回忆录丶 提交于 2019-11-27 19:50:58
So, my goal is to be able to filter a ModelChoiceField queryset in my ModelForm to only include Places that request.user has created. My ModelForm is simply: class PlaceEventForm(models.ModelForm): class Meta: model = Event I'd like to be able to add something like: def __init__(self, *args, **kwargs): super(PlaceEventForm, self).__init__(*args, **kwargs) self.fields['place'].queryset = Place.objects.filter(created_by=request.user) However, I can't seem to find a way to access the request in the ModelForm. My View is like so: class PlaceEventFormView(CreateView): form_class = PlaceEventForm

Django: Search form in Class Based ListView

本秂侑毒 提交于 2019-11-27 18:11:21
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 read various documentations and I understand views for direct query requests, but as soon as I would

How do I set initial data on a Django class based generic createview with request data

早过忘川 提交于 2019-11-27 15:07:35
问题 I used Django's generic createview for my model from myproject.app.forms import PersonForm class PersonMixin(object): model = Person form_class = PersontForm class PersonCreateView(PersonMixin, CreateView): pass This works perfectly for displaying a create view of Person with my custom form. However, I have a field in the form that I want to pre-populate with a value. I found this answer: Set initial value to modelform in class based generic views However, my pre-populated value comes from

How do I filter tables with Django generic views?

浪子不回头ぞ 提交于 2019-11-27 13:22:30
问题 I am trying to create a table view with pagination, sorting, and filtering, using the most common/standard/recommended approach for Django 1.6. This seems to be Generic Class-Based Views and django-tables2. I've found at least two different examples of how to add filtering, one with django-filters and crispy-forms and the other with django_filters, but neither includes a complete working example. When I follow either approach, I get stuck filling in the missing material. Using the crispy

Django class based views - UpdateView with two model forms - one submit

被刻印的时光 ゝ 提交于 2019-11-27 13:19:56
问题 I have a page with a list of users, and would like to be able to click a link to update their profile. When 'update' is clicked, I should be able to edit the username, first name, ... email, phone number, department etc, in a single page, using a single submit button. I accomplished this by using two forms, one for User, and one for the extra information. The ListView, DeleteView and CreateView work perfectly with these two forms, but not the UpdateView. I am not able to instantiate the two

django createview how to get the object that is created

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 12:47:10
问题 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 actor to the movie. in my case the Movie = Chiamata and Actor = Offerta (i keep the italians name for what i need :D) fine. those are my urls in the urls.py url(r'^chiamata/$', ChiamataCreate.as_view(),name='chiamata_create'), url(r'^chimamata/(?P

set initial value in CreateView from ForeignKey (non-self.request.user)

若如初见. 提交于 2019-11-27 12:20:19
问题 I am attempting to access ForeignKeys in Class Based Views CreateView. I would like to be able to dynamically set initial values in CBV from ForeignKeys and also dynamically set template links from ForeignKeys. These two questions (1. initial values, 2. template links) may be solved in similar methods, or perhaps by different methods... I'm still learning. Perhaps the first question can be solved within views.py and the second question can be solved with template syntax in ingredient_form