django 3.0更新详细信息的模型
我有一个具有“状态”字段的“项目”模型。状态可以活跃,暂停或完成。我希望能够通过“项目详细信息”视图上的表单更新字段。
我已经阅读了一些解决这个问题的解决方案,但是作为新手,我无法使它起作用。提交表格时,我会收到HTTP 405错误,并且该实例未更新。
模型:
class Project(models.Model):
title = models.CharField(max_length= 200)
description = tinymce_models.HTMLField()
status = models.CharField(max_length=20, choices=PROJECT_CHOICES, default="active")
date = models.DateTimeField(auto_now_add=True, null=True)
created_by = models.ForeignKey(CustomUser, editable=False, null=True, blank=True, on_delete=models.RESTRICT)
objects = ProjectManager()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('company_project:project_detail', args=[str(self.id)])
查看
class CompanyProjectsDetailView(DetailBreadcrumbMixin, FormMixin, DetailView):
model = Project
id = Project.objects.only('id')
template_name = 'company_accounts/project_detail.html'
context_object_name = 'project'
form_class = ProjectStatusForm
notescount = Project.objects.annotate(num_notes=Count('notes'))
documentscount = Project.objects.annotate(num_documents=Count('project_documents'))
todoscount = Project.objects.annotate(num_documents=Count('todo_group'))
def form_valid(self, form):
project = get_object_or_404(Project, id=self.kwargs.get('pk'))
theform = form.save(commit=False)
theform.project = project
form.save()
return super(CompanyProjectsDetailView, self).form_valid(form)
页面上的表单
class ProjectStatusForm(forms.ModelForm):
class Meta:
model = Project
fields = ['status']
labels = {'status': 'project status'}
widgets = {
'status': forms.Select(attrs={'id':'PROJECT_CHOICES'}),
}
我使用此代码添加表单
<form action="" method="post">
{% csrf_token %}
{{ form.media }}
{{ form|crispy }}
</br>
<input type="submit" value="save">
</form>
I have a "project" model that has a "status" field. The status can be active, paused, or complete. I want to be able to update the field via form on the project detail view.
I have read a few solutions to this problem but, as a newbie, I haven't been able to get this to work. When I submit the form I get an http 405 error and the instance is not updated.
the model:
class Project(models.Model):
title = models.CharField(max_length= 200)
description = tinymce_models.HTMLField()
status = models.CharField(max_length=20, choices=PROJECT_CHOICES, default="active")
date = models.DateTimeField(auto_now_add=True, null=True)
created_by = models.ForeignKey(CustomUser, editable=False, null=True, blank=True, on_delete=models.RESTRICT)
objects = ProjectManager()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('company_project:project_detail', args=[str(self.id)])
the view
class CompanyProjectsDetailView(DetailBreadcrumbMixin, FormMixin, DetailView):
model = Project
id = Project.objects.only('id')
template_name = 'company_accounts/project_detail.html'
context_object_name = 'project'
form_class = ProjectStatusForm
notescount = Project.objects.annotate(num_notes=Count('notes'))
documentscount = Project.objects.annotate(num_documents=Count('project_documents'))
todoscount = Project.objects.annotate(num_documents=Count('todo_group'))
def form_valid(self, form):
project = get_object_or_404(Project, id=self.kwargs.get('pk'))
theform = form.save(commit=False)
theform.project = project
form.save()
return super(CompanyProjectsDetailView, self).form_valid(form)
the form
class ProjectStatusForm(forms.ModelForm):
class Meta:
model = Project
fields = ['status']
labels = {'status': 'project status'}
widgets = {
'status': forms.Select(attrs={'id':'PROJECT_CHOICES'}),
}
On the page I use this code to add the form
<form action="" method="post">
{% csrf_token %}
{{ form.media }}
{{ form|crispy }}
</br>
<input type="submit" value="save">
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论