django-class-based-views

To be a Class based view or a generic class base view?

让人想犯罪 __ 提交于 2019-12-08 03:32:45
问题 So below I have just created by first Class based view after watching a Django Con EU talk video. It works and understand what it does. I don't understand the difference between a Class based view or a generic class base view - which I have just build? class GroupListView(ListView): """ List all Groups. """ context_object_name = 'groups' template_name = 'contacts/home.html' def get_context_data(self, **kwargs): """ Get the context for this view. """ # Call the base implementation first to get

Django Using Slug Field for Detail URL

99封情书 提交于 2019-12-07 22:30:32
I'm attempting to setup my site so that the url for my job-detail will use a slug field instead of a pk. It's telling me that it cannot find my job with the given slug (which is an int, 147). Update: After looking at the DetailView description at https://ccbv.co.uk/projects/Django/1.11/django.views.generic.detail/DetailView/ I realized there is a slug_field attribute for DetailView . My new view looks like: class JobDetailView(CacheMixin, DetailView): model = Job slug_field = 'slug' Question: urls: urlpatterns = [ url(r'^careers$', views.job_list, name='job-list'), url(r'^careers/(?P<slug>[0-9

Django CBV: Easy access to url parameters in get_context_data()?

大兔子大兔子 提交于 2019-12-07 17:09:20
问题 I understand that the standard way of accessing named url parameters in a custom get_context_data() method is through self.kwargs . However, the self.kwargs syntax becomes awkward, especially when handling a significant number of parameters. So, I've been resorting to something like this at the top of every get_context_data() method -- just to get easy-to-handle local variables: def get_context_data(self, **kwargs): var1, var2, var3, var4, var5 = [self.kwargs[x] for x in ['var1', 'var2',

Django Class Based View: Validate object in dispatch

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 14:33:29
问题 Is there a established way that i validate an object in the dispatch without making an extra database call when self.get_object() is called later in get/post? Here is what i have so far (slightly altered for this question): class CourseUpdateView(UpdateView): def dispatch(self, request, *args, **kwargs): self.request = request self.kwargs = kwargs self.object = self.get_object() if self.object.is_online: messages.warning(request, "Sorry this one can't be updated") return redirect("course

How do I paginate WeekArchiveView?

徘徊边缘 提交于 2019-12-07 14:24:39
问题 In continuation of my struggle with WeekArchiveView, how do I paginate it by week? All I want is: to know if there is next / previous week available; in case there is, provide a link in the template. I'd like it to also skip empty weeks. The source shows get_next_day / get_prev_day and get_next_month / get_prev_month are available, but nothing for weeks. 回答1: That is definitely interesting. Sure enough MonthMixin includes get_next_month / get_prev_month methods, and DayMixin includes get_next

Django: CreateView with additional field?

无人久伴 提交于 2019-12-07 07:10:24
问题 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:

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

非 Y 不嫁゛ 提交于 2019-12-07 03:47:48
问题 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

Redirect to next URL in django FormView

[亡魂溺海] 提交于 2019-12-07 03:06:30
问题 I am using Django 1.7 with Python 3.4 . I have a scenario where I wish the users to be redirected to another view, as defined in the next GET parameter, after they login. But with my current setup, they always get redirected to the home page. I was hoping there is a way to do this in the get_success_url of the FormView class. Below is my code The next parameter in the URL http://localhost:8000/login/?next=/ads/new views.py from django.views.generic.edit import FormView class LoginView

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

旧街凉风 提交于 2019-12-06 23:59:29
问题 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,

Using Multiple ModelForms with Class-Based Views

萝らか妹 提交于 2019-12-06 20:26:24
I've got a situation where I'd like to add an additional modelform to my CreateView. We have an entry order system that allows someone to add an order and then add items to that order. Typically, when someone adds an order for the first time they'd like to also add an item to that order, so I want to combine those models into a single form and process them on initial order entry. I'm running into a problem when the forms don't validate. I've overridden get_context_data to add the item form to the template and I've overridden post to process the extra form. But when the forms are invalid I need