django-class-based-views

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

半腔热情 提交于 2019-11-29 12:43:11
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 ID be pre-set on the Book form, and then have the Book form processing use that ID as the PK of the

How do I use UpdateView?

断了今生、忘了曾经 提交于 2019-11-29 07:53:40
I have two, presumably related, problems with UpdateView. First, it is not updating the user but creating a new user object. Second, I cannot restrict the fields displayed in the form. Here is my views.py: class RegistrationView(FormView): form_class = RegistrationForm template_name = "register.html" success_url = "/accounts/profile/" def form_valid(self, form): if form.is_valid: user = form.save() user = authenticate(username=user.username, password=form.cleaned_data['password1']) login(self.request, user) return super(RegistrationView, self).form_valid(form) #I still have no idea what this

Django passing variables to templates from class based views

我怕爱的太早我们不能终老 提交于 2019-11-29 06:07:22
问题 If I have a class based view, like this, class SomeView (View): response_template='some_template.html' var1 = 0 var2 = 1 def get(self, request, *args, **kwargs): return render_to_response(self.response_template, locals(), context_instance=RequestContext(request)) My question is, inside the template some_template.html , how do I access var1 and var2 ? As far as I understood this, the locals() sort of just dumps all the local variables into the template, which has worked very well so far. But

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

微笑、不失礼 提交于 2019-11-29 02:10:16
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 the profile for request.user. How do I access the request in PersonCreateView and pass that to the form?

Django UpdateView without pk in url

南笙酒味 提交于 2019-11-29 01:56:35
Is it possible eliminate pk from url related to UpdateView ? For example, if I have url(r'^myobj/update/(?P<pk>\d+)/$', views.UpdateMyObj.as_view(), name="update") is there any way to write it like url(r'^myobj/update/$', views.UpdateMyObj.as_view(), name="update") and then send pk as a parameter in POST or GET request? Yes it is possible you just need to override the get_object method: from django.views.generic.edit import UpdateView class UpdateMyObj(UpdateView): # ..... def get_object(self): return MyModel.objects.get(pk=self.request.GET.get('pk')) # or request.POST 来源: https:/

Multiple Forms and Formsets in CreateView

佐手、 提交于 2019-11-29 01:03:56
问题 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 ? 回答1: 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

cache_page with Class Based Views

孤者浪人 提交于 2019-11-28 22:50:16
I'm trying to do cache_page with class based views (TemplateView) and i'm not able to. I followed instructions here: Django--URL Caching Failing for Class Based Views as well as here: https://github.com/msgre/hazard/blob/master/hazard/urls.py But I get this error: cache_page has a single mandatory positional argument: timeout I read the code for cache_page and it has the following: if len(args) != 1 or callable(args[0]): raise TypeError("cache_page has a single mandatory positional argument: timeout") cache_timeout = args[0] which means it wont allow more than 1 argument. Is there any other

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

牧云@^-^@ 提交于 2019-11-28 22:20:04
问题 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,

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

丶灬走出姿态 提交于 2019-11-28 21:39:20
问题 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

How do I filter tables with Django generic views?

爷,独闯天下 提交于 2019-11-28 20:57:39
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 approach from Nicolas Kuttler, I have: models.py from django.db import models class Author(models.Model):