django 3.0更新详细信息的模型

发布于 2025-01-30 19:34:14 字数 2075 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文