django-class-based-views

Django: CreateView with additional field?

走远了吗. 提交于 2019-12-05 12:39:18
I am trying to program a Django CreateView (CBV), which takes instead of the user id the user email and determines (or creates) the user based on the email. My model does not contain anything special: class Project(models.Model): name = models.CharField(_('Title'), max_length=100,) user = models.ForeignKey(User, verbose_name=_('user'),) ... My forms.py adds the additional email field to the form: class ProjectCreateForm(forms.ModelForm): email = forms.EmailField(required=True, ) class Meta: model = Project fields = ('name', ...,) In my views.py, I am trying to determine if the user exists or

How to validate mulitple forms in a single formView class Django

北慕城南 提交于 2019-12-05 10:17:13
问题 I have a formView class as you can see below:- view.py class ThreadForm(FormView): template_name = 'thread.html' form_class = ThreadModelForm success_url = '/success' def form_valid(self, form): # This method is called when valid form data has been POSTed. # It should return an HttpResponse. print form.cleaned_data return super(ThreadForm, self).form_valid(form) def get_context_data(self, **kwargs): context = super(ThreadForm, self).get_context_data(**kwargs) context['second_form'] =

Why doesn't self.object in a CreateView have an id after saving to the database?

て烟熏妆下的殇ゞ 提交于 2019-12-05 09:44:21
Following the comment on the accepted answer on django createview how to get the object that is created , I am attempting to use the id from a user created by a CreateView in its get_success_url method. However, even though it is definitely being saved to MySQL and receiving an id, when I access self.object , it doesn't have an id to use. The model does have an id property. Why wouldn't I be able to access the id? If I'm being led astray by the linked comment, what is the right way to get the id? Reference code: models.py from django.db import models class User(models.Model): id = models

Why does UpdateView need to have model/queryset/get_queryset defined when using form_class as opposed to CreateView?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 07:40:40
Works like a charm: MyCreateView(CreateView): template_name = "my_template_name" form_class = MyModelForm success_url = "/success/" But the following doesn't: MyUpdateView(UpdateView): template_name = "my_template_name" form_class = MyModelForm success_url = "/success/" I get this error: MyUpdateView is missing a queryset. Define MyUpdateView.model, MyUpdateView.queryset, or override MyUpdateView.get_queryset(). Why does an UpdateView need model , queryset or get_queryset defined to not cause an error while CreateView doesn't? Shouldn't it be able to automatically derive it from the Model used

Django: model object “has no attribute '_meta'” in class based view

醉酒当歌 提交于 2019-12-05 04:05:08
Hi Stackoverflow people, I am working with class based views and for a test site, I followed the documentation to setup the class based views. For a project site (based on the project model below), I just want to create a quick CRUD application for the simple project model below. models.py class Project(models.Manager): name = models.CharField(_('Name of the Project'), max_length = 100,) slug = models.SlugField(max_length=100,) ... views.py from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.core.urlresolvers import reverse_lazy from project.models import

Django: ListView with post() method?

你。 提交于 2019-12-04 23:32:22
问题 I am trying to process two forms in a Django class based view. The site contains a form called form (based on GET ) for narrowing the list results of the ListView and the second form status_form (based on POST ). Both forms are required since the ListView returns a list of items. Form lets the user restrict the choices and status_forms lets the user flag incorrect items via a modal form (therefore it needs to be in the same template). My trouble is that ListView does not come with the method

How to pass parameters to django generic views

做~自己de王妃 提交于 2019-12-04 21:17:18
I would like to pass a number to my generic view (DetailView) to get one object Here is my code Urlpattern (r'^newreportview/(?P<number>\w+)/$', NewReportView.as_view()), View Class class NewReportView(DetailView): template_name = "report/newreportview.html" context_object_name = "newreportview" def get_queryset(self): task= get_object_or_404(MyTask,applicationnnumber=self.args[0]) return task I guess something is wrong in this line name = get_object_or_404(MyTask,applicationnnumber=self.args[0]) error message: Exception Type: IndexError Exception Value: tuple index out of range How should I

Bootstrap3 tabs in Django

社会主义新天地 提交于 2019-12-04 19:08:19
I want to implement Bootstrap3 tabs in my app, which displays school data by state. So if you go to example.com/ma/ you will see information for the state of Massachusetts and tabs to sort by grade level. I am already using the queryset to filter by state so that on example.com/ma/ only "ma" results appear. And I can show ALL data in one of the tabs, but can't filter it out for multiple tabs. To keep it simple, I just want to do tabs for "All" and "High School" here. Here is my models.py : from django.db import models class School(models.Model): school_name = models.CharField(max_length=200)

How to unit test methods inside django's class based views?

大城市里の小女人 提交于 2019-12-04 17:44:29
问题 I need to test the methods and helper function inside a django Class Based View. Consider this Class Based View: class MyClassBasedView(View): def dispatch(self, request, *args, **kwargs): .... def __get_render_dict(): d = {} ... return d def my_method(self): render_dict = self.__get_render_dict() return render_response(self.request, 'template.html', render_dict) In order to write unit tests for my view, I need to call the methods inside, say __get_render_dict() directly. How can I achieve

How should template names be set dynamically using class based views?

帅比萌擦擦* 提交于 2019-12-04 16:49:03
问题 I've searched through the ref and topics of the class based views Django documentation(Django 1.4) but I haven't found any mentioning of this. How do I set template names dynamically using class based views? I'm looking for the class-based equivalent of the following setup : urls.py from django.conf.urls.defaults import * from mysite.views import dynamic urlspatterns = patterns('', url(r'^dynamic/(?P<template>\w+)/$', dynamic),) ) views.py from django.shortcuts import render_to_response def