我们从Python开源项目中,提取了以下32个代码示例,用于说明如何使用django.views.generic.UpdateView()。
def get_object(self, queryset=None): """ This parts allows generic.UpdateView to handle creating products as well. The only distinction between an UpdateView and a CreateView is that self.object is None. We emulate this behavior. This method is also responsible for setting self.product_class and self.parent. """ self.creating = 'pk' not in self.kwargs if self.creating: hook_class_slug = self.kwargs.get('hook_class_slug') self.product_class = get_object_or_404(ProductClass, slug=hook_class_slug) return None # success else: hook = super(HookCreateUpdateView, self).get_object(queryset) self.product_class = hook.get_product_class() return hook
def get_success_url(self): if not self.object.subject.visible: self.object.visible = False self.object.save() if not self.object.visible and not self.object.repository: Resource.objects.filter(topic = self.object).update(visible = False) messages.success(self.request, _('Topic "%s" was updated on virtual enviroment "%s" successfully!')%(self.object.name, self.object.subject.name)) self.log_context['category_id'] = self.object.subject.category.id self.log_context['category_name'] = self.object.subject.category.name self.log_context['category_slug'] = self.object.subject.category.slug self.log_context['subject_id'] = self.object.subject.id self.log_context['subject_name'] = self.object.subject.name self.log_context['subject_slug'] = self.object.subject.slug self.log_context['topic_id'] = self.object.id self.log_context['topic_name'] = self.object.name self.log_context['topic_slug'] = self.object.slug super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) return reverse_lazy('subjects:view', kwargs = {'slug': self.object.subject.slug})
def form_valid(self, form): self.object = form.save(commit = False) self.object.save() msg = _('User "%s" updated successfully')%(self.object.get_short_name()) self.log_context['user_id'] = self.object.id self.log_context['user_name'] = self.object.get_short_name() self.log_context['user_email'] = self.object.email super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) messages.success(self.request, msg) return super(UpdateView, self).form_valid(form)
def get_context_data(self, **kwargs): # This line is the method we use to send data to the template. context = super(UpdateViewCustom, self).get_context_data(**kwargs) # In this line, we perform the super class method to send normal data from the CBV UpdateView. model_name = self.model._meta.verbose_name.title() # In this line, we get the verbose_name property of the defined model. context['model_name'] = model_name # In this line, we send the verbose_name property to the template. context['url_name'] = self.url_name \ # This line allows us to send the name of our URL to the template. return context
def dispatch(self, request, *args, **kwargs): slug = self.kwargs.get('topic_slug', '') topic = get_object_or_404(Topic, slug = slug) if not has_subject_permissions(request.user, topic.subject): return redirect(reverse_lazy('subjects:home')) return super(UpdateView, self).dispatch(request, *args, **kwargs)
def form_valid(self, form, pendencies_form): self.object = form.save(commit = False) if not self.object.topic.visible and not self.object.topic.repository: self.object.visible = False self.object.save() pend_form = pendencies_form.save(commit = False) pend_form.resource = self.object if not pend_form.action == "": pend_form.save() self.log_context['category_id'] = self.object.topic.subject.category.id self.log_context['category_name'] = self.object.topic.subject.category.name self.log_context['category_slug'] = self.object.topic.subject.category.slug self.log_context['subject_id'] = self.object.topic.subject.id self.log_context['subject_name'] = self.object.topic.subject.name self.log_context['subject_slug'] = self.object.topic.subject.slug self.log_context['topic_id'] = self.object.topic.id self.log_context['topic_name'] = self.object.topic.name self.log_context['topic_slug'] = self.object.topic.slug self.log_context['pdffile_id'] = self.object.id self.log_context['pdffile_name'] = self.object.name self.log_context['pdffile_slug'] = self.object.slug super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) return redirect(self.get_success_url())
def get_context_data(self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) context['title'] = _('Update PDF File') slug = self.kwargs.get('topic_slug', '') topic = get_object_or_404(Topic, slug = slug) context['topic'] = topic context['subject'] = topic.subject context['mimeTypes'] = valid_formats context['resource'] = get_object_or_404(PDFFile, slug = self.kwargs.get('slug', '')) return context
def dispatch(self, request, *args, **kwargs): slug = self.kwargs.get('sub_slug', '') subject = get_object_or_404(Subject, slug = slug) if not has_subject_permissions(request.user, subject): return redirect(reverse_lazy('subjects:home')) return super(UpdateView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) context['title'] = _('Update Group') slug = self.kwargs.get('sub_slug', '') subject = get_object_or_404(Subject, slug = slug) context['subject'] = subject return context
def form_valid(self, form, pendencies_form): self.object = form.save(commit = False) if not self.object.topic.visible and not self.object.topic.repository: self.object.visible = False self.object.save() pendencies_form.instance = self.object pendencies_form.save(commit = False) for form in pendencies_form.forms: pend_form = form.save(commit = False) if not pend_form.action == "": pend_form.save() self.log_context['category_id'] = self.object.topic.subject.category.id self.log_context['category_name'] = self.object.topic.subject.category.name self.log_context['category_slug'] = self.object.topic.subject.category.slug self.log_context['subject_id'] = self.object.topic.subject.id self.log_context['subject_name'] = self.object.topic.subject.name self.log_context['subject_slug'] = self.object.topic.subject.slug self.log_context['topic_id'] = self.object.topic.id self.log_context['topic_name'] = self.object.topic.name self.log_context['topic_slug'] = self.object.topic.slug self.log_context['webconference_id'] = self.object.id self.log_context['webconference_name'] = self.object.name self.log_context['webconference_slug'] = self.object.slug super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) return redirect(self.get_success_url())
def get_context_data(self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) context['title'] = _('Update Web Conference') slug = self.kwargs.get('topic_slug', '') topic = get_object_or_404(Topic, slug = slug) context['topic'] = topic context['subject'] = topic.subject context['resource'] = get_object_or_404(Webconference, slug = self.kwargs.get('slug', '')) return context
def form_valid(self, form, pendencies_form): self.object = form.save(commit = False) if not self.object.topic.visible and not self.object.topic.repository: self.object.visible = False self.object.save() pend_form = pendencies_form.save(commit = False) pend_form.resource = self.object if not pend_form.action == "": pend_form.save() self.log_context['category_id'] = self.object.topic.subject.category.id self.log_context['category_name'] = self.object.topic.subject.category.name self.log_context['category_slug'] = self.object.topic.subject.category.slug self.log_context['subject_id'] = self.object.topic.subject.id self.log_context['subject_name'] = self.object.topic.subject.name self.log_context['subject_slug'] = self.object.topic.subject.slug self.log_context['topic_id'] = self.object.topic.id self.log_context['topic_name'] = self.object.topic.name self.log_context['topic_slug'] = self.object.topic.slug self.log_context['bulletin_id'] = self.object.id self.log_context['bulletin_name'] = self.object.name self.log_context['bulletin_slug'] = self.object.slug super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) return redirect(self.get_success_url())
def form_valid(self, form, pendencies_form): self.object = form.save(commit = False) if not self.object.topic.visible and not self.object.topic.repository: self.object.visible = False self.object.save() pendencies_form.instance = self.object pendencies_form.save(commit = False) for form in pendencies_form.forms: pend_form = form.save(commit = False) if not pend_form.action == "": pend_form.save() self.log_context['category_id'] = self.object.topic.subject.category.id self.log_context['category_name'] = self.object.topic.subject.category.name self.log_context['category_slug'] = self.object.topic.subject.category.slug self.log_context['subject_id'] = self.object.topic.subject.id self.log_context['subject_name'] = self.object.topic.subject.name self.log_context['subject_slug'] = self.object.topic.subject.slug self.log_context['topic_id'] = self.object.topic.id self.log_context['topic_name'] = self.object.topic.name self.log_context['topic_slug'] = self.object.topic.slug self.log_context['ytvideo_id'] = self.object.id self.log_context['ytvideo_name'] = self.object.name self.log_context['ytvideo_slug'] = self.object.slug super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) return redirect(self.get_success_url())
def get_context_data(self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) context['title'] = _('Update YouTube Video') slug = self.kwargs.get('topic_slug', '') topic = get_object_or_404(Topic, slug = slug) context['topic'] = topic context['subject'] = topic.subject context['resource'] = get_object_or_404(YTVideo, slug = self.kwargs.get('slug', '')) return context
def form_valid(self, form, pendencies_form): self.object = form.save(commit = False) if not self.object.topic.visible and not self.object.topic.repository: self.object.visible = False self.object.save() pend_form = pendencies_form.save(commit = False) pend_form.resource = self.object if not pend_form.action == "": pend_form.save() self.log_context['category_id'] = self.object.topic.subject.category.id self.log_context['category_name'] = self.object.topic.subject.category.name self.log_context['category_slug'] = self.object.topic.subject.category.slug self.log_context['subject_id'] = self.object.topic.subject.id self.log_context['subject_name'] = self.object.topic.subject.name self.log_context['subject_slug'] = self.object.topic.subject.slug self.log_context['topic_id'] = self.object.topic.id self.log_context['topic_name'] = self.object.topic.name self.log_context['topic_slug'] = self.object.topic.slug self.log_context['webpage_id'] = self.object.id self.log_context['webpage_name'] = self.object.name self.log_context['webpage_slug'] = self.object.slug super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) return redirect(self.get_success_url())
def form_valid(self, form, pendencies_form): self.object = form.save(commit = False) if not self.object.topic.visible and not self.object.topic.repository: self.object.visible = False self.object.save() pend_form = pendencies_form.save(commit = False) pend_form.resource = self.object if not pend_form.action == "": pend_form.save() self.log_context['category_id'] = self.object.topic.subject.category.id self.log_context['category_name'] = self.object.topic.subject.category.name self.log_context['category_slug'] = self.object.topic.subject.category.slug self.log_context['subject_id'] = self.object.topic.subject.id self.log_context['subject_name'] = self.object.topic.subject.name self.log_context['subject_slug'] = self.object.topic.subject.slug self.log_context['topic_id'] = self.object.topic.id self.log_context['topic_name'] = self.object.topic.name self.log_context['topic_slug'] = self.object.topic.slug self.log_context['filelink_id'] = self.object.id self.log_context['filelink_name'] = self.object.name self.log_context['filelink_slug'] = self.object.slug super(UpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) return redirect(self.get_success_url())
def get_context_data(self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) context['title'] = _('Update File Link') slug = self.kwargs.get('topic_slug', '') topic = get_object_or_404(Topic, slug = slug) context['topic'] = topic context['subject'] = topic.subject context['mimeTypes'] = valid_formats context['resource'] = get_object_or_404(FileLink, slug = self.kwargs.get('slug', '')) return context
def get_initial(self): initial = super(UpdateView, self).get_initial() slug = self.kwargs.get('sub_slug', '') initial['subject'] = get_object_or_404(Subject, slug = slug) return initial
def get_form_kwargs(self): kwargs = super(UpdateView, self).get_form_kwargs() kwargs.update({'is_edit': True}) return kwargs
def get_context_data (self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) context['title'] = _("Update User") context['settings_menu_active'] = "settings_menu_active" return context
def get_context_data(self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) context['title'] = _('Update Topic Goals') slug = self.kwargs.get('topic_slug', '') topic = get_object_or_404(Topic, slug = slug) context['topic'] = topic context['subject'] = topic.subject return context