django-class-based-views

django run another class-based view (CBV) in a CBV?

核能气质少年 提交于 2019-11-30 18:29:18
问题 so I have a CBV (A), CBV (B), and a url like regex=r'^(?P<slug>[-\w]+)/(?P<app>[-\w]+)' I want to read in the slug and app parameters with (A) and then based on those, redirect it to an appropriate CBV, possible (B). I don't want to redirect the user with HttpResponseRedirect or anything like that, but instead basically run another CBV as if it were the one being called. How do I run another CBV, like (B), directly/internally from a CBV (A)? 回答1: You can call it that way: class CBViewA(View):

Django: Only update fields that have been changed in UpdateView

*爱你&永不变心* 提交于 2019-11-30 18:01:10
问题 I'm using an UpdateView to update a series of fields. However, I only want fields that have been modified to be saved to the database. If a value was not provided for a field during update process, I want the previous value to be used as the default. If a new value was provided for a field then only that field should be updated. How do I go about accomplishing this? #views.py class AccountUpdate(UpdateView): """ Updates an account; unchanged fields will not be updated.""" context_object_name

Provide extra context to all views

*爱你&永不变心* 提交于 2019-11-30 12:18:22
I'm putting together a project management website for my team using django. My base template includes a sidebar menu which contains a list of all projects and users, linking to a DetailView for that user or project, respectively. My problem is that I need to provide the User and Project models to every view so that I can render that sidebar. I know how to add extra context ; the problem is that I feel like I'm violating DRY by modifying the context at each level. Is it possible to simply redefine the base TemplateClass so that all child classes— ListView , DetailView , etc.—contain the

success_url in UpdateView, based on passed value

安稳与你 提交于 2019-11-30 10:57:00
问题 How can I set success_url based on a parameter? I really want to go back to where I came from, not some static place. In pseudo code: url(r'^entry/(?P<pk>\d+)/edit/(?P<category>\d+)', UpdateView.as_view(model=Entry, template_name='generic_form_popup.html', success_url='/category/%(category)')), Which would mean: edit entry pk and then return to 'category'. Here an entry can be part of multiple categories. 回答1: Create a class MyUpdateView inheritted from UpdateView and override get_success_url

Django Class-Based Generic Views and Authentication

北慕城南 提交于 2019-11-30 08:35:58
问题 I am pretty new to Django (starting with 1.3). In building an app, I went with the new class-based generic views from day one, using a combination of the built in classes and subclassing them where I needed to add to the context. Now my problem is, I need to go back to my views, and have them accessible only to logged in users. ALL the documentation I have found shows how to do this with the old functional generic views, but not with class-based. Here is an example class: class ListDetailView

Multiple Forms and Formsets in CreateView

烂漫一生 提交于 2019-11-30 03:55:19
I have 2 models, Father and Son . I have a page to register Father . On the same page I have a formset to register Son . On page has a button "more" to add another Father and their respective Son on the same page. Does anyone have any examples using CreateView ? Class based views are still new, so I'll write this out. The process is simple: First, create the forms for your objects. One of the forms will be repeated. Nothing special to be done here. class SonInline(ModelForm): model = Son class FatherForm(ModelForm): model = Father Then, create your formset : FatherInlineFormSet = inlineformset

Is it okay to set instance variables in a Django class based view?

ⅰ亾dé卋堺 提交于 2019-11-30 02:00:08
I trying out Django's class based views (CBVs). class BlahView(TemplateView): template_name = 'blah/blah.html' def get_context_data(self, **kwargs): #code... def get(self, request, **kwargs): #more code... Now, I know that I can get the request params from self.request. Now say I want to parse these request params and store them within the class. Can I store those in self.xxx ? Now, obviously based on how classes work, this seems straightforward. But I can't make out the flow of control, looking at the definition of View (superclass of TemplateView ). The source mentions as_view() to be the

Using django-dynamic-formset with CreateWithInlinesView from django-extra-views - multiple formsets

血红的双手。 提交于 2019-11-30 00:54:42
I got 3 models: class Client(models.Model): first_name = models.CharField(max_length=20) last_name = models.CharField(max_length=40) class Phone(models.Model): number = models.CharField(max_length=10) client = models.ForeignKey(Client) class ClientEmail(models.Model): client = models.ForeignKey(Client) address = models.EmailField(verbose_name='Email') one form and two inline formsets: class ClientForm(ModelForm): class Meta: model = Client class PhoneFormSet(InlineFormSet): model = Phone extra = 1 class EmailFormSet(InlineFormSet): model = ClientEmail extra = 1 view: class ClientCreateView

success_url in UpdateView, based on passed value

与世无争的帅哥 提交于 2019-11-29 23:21:46
How can I set success_url based on a parameter? I really want to go back to where I came from, not some static place. In pseudo code: url(r'^entry/(?P<pk>\d+)/edit/(?P<category>\d+)', UpdateView.as_view(model=Entry, template_name='generic_form_popup.html', success_url='/category/%(category)')), Which would mean: edit entry pk and then return to 'category'. Here an entry can be part of multiple categories. Create a class MyUpdateView inheritted from UpdateView and override get_success_url method: class MyUpdateView(UpdateView): def get_success_url(self): pass #return the appropriate success url

Django combine DetailView and FormView

橙三吉。 提交于 2019-11-29 21:01:51
I have a view where I need to display information about a certain model instance hence I use a DetailView . I also need that same view to handle a regular form (not a model form), both displaying the form on GET and validating it on POST . To do that, I am trying to use a FormView however the combination of both view clases does not work: class FooView(FormView, DetailView): # configs here In GET (for simplicity of the question I will only show the issue with GET since POST has a different issue), it does not work because the form never gets added to the context. The reason has to do with