- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用了this使用 Django 成功设置动态内联表单集的教程。 CreateView 工作得很好,但我无法让 UpdateView 实际更新相关字段。没有抛出任何错误,但项目不会更新。我相信我已将错误隔离到 form_valid 函数。代码如下。感谢您的帮助。
class ApplicantCreate(CreateView):
model = Applicant
success_message = 'Your application was submitted successfully.'
form_class = forms.ApplicantForm
template_name = 'careers/add_applicant.html'
success_url = reverse_lazy('careers:thanks')
def get_context_data(self, **kwargs):
data = super(ApplicantCreate, self).get_context_data(**kwargs)
positions = super(ApplicantCreate, self).get_context_data(**kwargs)
if self.request.POST:
data['employer'] = forms.ApplicantEmployerFormSet(
self.request.POST,
prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(
self.request.POST,
prefix='education')
else:
data['employer'] = forms.ApplicantEmployerFormSet(prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(prefix='education')
return data
context['unfilled_positions'] = Position.objects.filter(filled=False)
return positions
def form_valid(self, form):
context = self.get_context_data()
employer = context['employer']
education = context['education']
with transaction.atomic():
form.instance.created_by = self.request.user
self.object = form.save()
if employer.is_valid():
employer.instance = self.object
employer.save()
if education.is_valid():
education.instance = self.object
education.save()
return super(ApplicantCreate, self).form_valid(form)
def get_success_url(self):
return reverse_lazy('careers:thanks')
class ApplicantUpdate(SuccessMessageMixin,LoginRequiredMixin,GroupRequiredMixin,UpdateView):
group_required = [u'careers-admin',u'careers']
model = Applicant
success_message = '%(first_name)s %(last_name)s was updated successfully.'
form_class = forms.ApplicantUpdateForm
template_name = 'careers/edit_applicant.html'
def get_context_data(self, **kwargs):
data = super(ApplicantUpdate, self).get_context_data(**kwargs)
positions = super(ApplicantUpdate, self).get_context_data(**kwargs)
if self.request.POST:
data['employer'] = forms.ApplicantEmployerFormSet(
self.request.POST,
instance=self.object,
prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(
self.request.POST,
instance=self.object,
prefix='education')
else:
data['employer'] = forms.ApplicantEmployerFormSet(
instance=self.object,
prefix='employer')
data['education'] = forms.ApplicantEducationFormSet(
instance=self.object,
prefix='education')
return data
context['unfilled_positions'] = Position.objects.filter(filled=False)
return positions
def form_valid(self, form):
context = self.get_context_data()
employer = context['employer']
education = context['education']
with transaction.atomic():
form.instance.created_by = self.request.user
self.object = form.save()
if employer.is_valid():
employer.instance = self.object
employer.save()
if education.is_valid():
education.instance = self.object
education.save()
return super(ApplicantUpdate, self).form_valid(form)
def get_success_url(self):
return reverse_lazy('careers:applicant_detail',kwargs={'pk': self.object.pk})
最佳答案
这不是一篇好的博客文章,因为如果任何内联表单无效,则不会显示任何错误。作为用户,我不希望 View 只是默默地忽略内联表单中的错误,成功保存我的主实例并且不报告这些错误。
请注意,我不知道错误是什么,也许这只是管理表单的问题。但无论如何,应该在实际保存主对象之前处理表单集错误。
一般来说,如果需要编写具有多种表单(包括表单集)的 View ,最好使用基于函数的 View 或 View
在你写get
的地方和post
而不是试图将其强制转换为通用的基于类的 View ,这并不是为此目的。
def multiple_forms_view(request, object_id):
# if this is a POST request we need to process the form data
obj = get_object_or_404(MyModel, pk=object_id)
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = MyForm(request.POST, instance=obj)
formset = MyFormSet(request.POST, instance=obj, ...)
# check whether it's valid:
if form.is_valid() and formset.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = MyForm(instance=obj)
formset = MyFormSet(instance=obj, ...)
return render(request, 'name.html', {'form': form, 'formset': formset})
这样您的模板就可以呈现每个表单的错误,包括表单集中的错误。如前所述,您也可以在 View
中执行此操作(这样你的 mixins 就可以工作),只需写 get
和post
方法和返回值与上面基于函数的 View 中的相同。
关于python - Django 动态表单集 UpdateView 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59616742/
我有一个表单类,看起来像.. #forms.py class ExampleForm(forms.Form): color = forms.CharField(max_length=25)
是否可以从与pk相关的网址中删除UpdateView? 例如,如果我有 url(r'^myobj/update/(?P\d+)/$', views.UpdateMyObj.as_view(), nam
我创建了一个继承UpdateView的类 View 。我已经指定了应该构建表单的字段和模型。现在假设我有一封现场电子邮件,那么我想在表单中禁用它。我不知道如何做到这一点。 class UserUpda
是否可以将多个模型传入 UpdateView ? 就像是: models = (FirstModel, SecondModel) 最佳答案 不是通过 models UpdateView 的属性. 但是
我有以 slugfield 作为主键的模型。 当我点击更新按钮时,我有更新 View ,它会创建新对象。 例如: 旧记录:标题=数据1 更新条目:标题=数据2 输出: 数据2 数据1 模型.py cl
所以我将 UpdateView 设置为向表单发送一个 request 对象,这样我就可以修改表单中的查询集(基于 request.user) 我的观点.py: class DataSourceUpda
我有一个 Users和 Jobs .如果一些User创建一个 Job , 然后他/她才能编辑这个 Job 的一些信息. 于是他访问了url .../job/update/ .如果Job是由他创建的(U
我有以下单元测试 def test_category_update_view(self): """ Make sure that a category can be updated
我有这样的架构: 模型.py: class Evento(models.Model): [...] user = ForeignKey(model=User) 表单.py: class
我正在使用 UpdateView 更新一系列字段。但是,我只希望将已修改的字段保存到数据库中。如果在更新过程中未为某个字段提供值,我希望将以前的值用作默认值。如果为字段提供了新值,则只应更新该字段。我
Django 和编程菜鸟在这里。我已经制作了一个我想要部署的应用程序,但我需要弄清楚如何将对该对象的创建者的 UpdateView 访问限制为,我很难过。 当前,用户可以使用 CreateView .
我想控制只有 super 用户,例如老师,可以访问继承自 UpdateView 的页面,并将其他人重定向到 /404 页面. 我在 views.py 中的类: class Edit_news(Upda
我有一个网站,用户可以在其中创建和编辑自己的列表。 我正在使用通用 View CreateView 来允许用户创建列表。 我想使用通用 View UpdateView 来允许他们编辑列表,但 logi
在Django中基于类的UpdateView中,我排除了用户字段,因为它是系统内部的,并且我不会要求它。现在,将用户传递到表单的Django正确方法是什么。 (我现在的操作方式是,将用户传递到表单的
我希望我的用户能够查看页面、更新该页面,然后返回到该页面或继续进行更多编辑。 这是显示信息的 View : # urls.py url(r'^gameview/$', views.G
我有一个具有 ManyToMany 关系的模型,我想用 CheckBoxSelectMultiple 小部件更新它,而其他所有小部件都使用默认的通用表单,但是当我重新定义一个表单字段时,它是唯一一个显
我有一个 Expense模型和 ExpenseLineItem模型。就像典型的费用/发票一样,一项费用可以有多个行项目来构成发票的总成本。我正在尝试使用基于类的 View 来创建和更新费用。我已经成功
我尝试了通用 View 的简约 django 实现来上传个人资料图片。 View .py class UpdateProfile(UpdateView): form_class = Updat
编辑: 对我来说更好的解决方案是使用权限系统,特别是因为我需要对对象进行其他类型的受控访问。我现在使用 Django-guardian 来帮助处理这样的对象级权限。 原文: 我正在通过让用户上传故事以
有没有办法在更新 View 中更新唯一字段?我有一个具有 name 和 age 字段的模型,但是当我尝试更新 age 时甚至不更改 的值name,它返回一个错误,该名称已存在于数据库中 模型.py c
我是一名优秀的程序员,十分优秀!