gpt4 book ai didi

python - 一个 View 上的 Django 多个表单

转载 作者:行者123 更新时间:2023-11-28 21:45:03 28 4
gpt4 key购买 nike

我有一个 Django 模板,其中包含来自几种不同模型类型的数据组合而成。如果愿意,可以使用仪表板。每个都有一个编辑表单。

是否最好在一个 View 中处理所有这些表单,因为它们被回发到同一个地方并通过如下所示的唯一字段区分它们?

或者如果有很多不同的专用途径是前进的方向?感谢您的指导

class ProjectDetail(DetailView):

template_name = 'project/view.html'

def get_object(self):
try:
return Project.objects.filter(brief__slug=self.kwargs['slug']).filter(team=get_user_team(self.request)).first()
# add loop to allow multiple teams to work on the same brief (project)
except Exception as e:
project_error = '%s (%s)' % (e.message, type(e))
messages.error(self.request, 'OH NO! %s' % project_error)
return redirect('home')

def get_context_data(self, **kwargs):
project = self.get_object()
context = dict()
context['project'] = project
context['milestone_form'] = MilestoneForm(initial={'project': project})
context['view'] = self
return context

def post(self, request, *args, **kwargs):
# get the context for the page
context = self.get_context_data()
try:
# switch for each of the form types on the team profile page (shown if member)
if 'milestone_form_submit' in request.POST:
project=self.get_object()
# set date arbitrarily to half way to brief deadline
brief = Brief.objects.get(project=project)
last_milestone = self.milestones().last()
milestone_del_date = last_milestone.del_date + timedelta(days=7)

new_milestone = Milestone(
project=project,
title_text=request.POST.get('title_text'),
del_date=milestone_del_date,
)
try:
new_milestone.save()
messages.success(self.request, "Excellent! New delivery popped on the bottom of the list")
except Exception as e:
# pass the erroring form back in the context if not
form = MilestoneForm(request.POST)
context['milestone_form'] = form
messages.error(self.request, "OH NO! Deadline didn't save. Be a sport and check what you entered")

elif 'milestone-edit-date-form-submit' in request.POST:
# get object from db
milestone = Milestone.objects.get(pk=request.POST['id'])
# update del_date field sent
milestone.del_date = request.POST['del_date']
# save back to db
milestone.save()
messages.success(self.request, "Updated that delivery right there!")

elif ...

except Exception as e:
messages.error(self.request, "OH NO! Deadline didn't save. Be a sport and check what you entered")
return render(request, self.template_name, context)

最佳答案

您可以使用 mixin 来解决您的问题。

要点中的示例 https://gist.github.com/michelts/1029336

class MultipleFormsMixin(FormMixin):
"""
A mixin that provides a way to show and handle several forms in a
request.
"""
form_classes = {} # set the form classes as a mapping

def get_form_classes(self):
return self.form_classes

def get_forms(self, form_classes):
return dict([(key, klass(**self.get_form_kwargs())) \
for key, klass in form_classes.items()])

def forms_valid(self, forms):
return super(MultipleFormsMixin, self).form_valid(forms)

def forms_invalid(self, forms):
return self.render_to_response(self.get_context_data(forms=forms))

如您所见,当您继承此类时,您可以同时处理多个表单。查看要点的代码并根据您的问题进行调整。

看看这个answer

关于python - 一个 View 上的 Django 多个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40090233/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com