【问题标题】:what should I put in the views.py so that a specific user can add new complaints in a complaint management system我应该在 views.py 中添加什么,以便特定用户可以在投诉管理系统中添加新的投诉
【发布时间】:2021-07-05 13:36:35
【问题描述】:

我有为投诉创建的模型。我尝试了一些方法来帮助用户创建投诉,但没有任何效果,所以我放弃了,我只想了解在 views.py 中添加什么以便我可以做到这一点。该系统基本上有多个用户,我希望用户将他们的投诉发送到管理面板,以便可以根据仪表板上的使用情况查看、编辑或删除它们。现在我的表单确实被创建了,但它没有在管理面板中保存投诉。

models.py:

class Complaints(models.Model):
user = models.ForeignKey(User, on_delete= CASCADE, null = True, blank=True)
title = models.CharField(max_length=300)
description = models.TextField(null=True, blank= True)
highpriority = models.BooleanField(default=False)
document = models.FileField(upload_to='static/documents')

def __str__(self):
    return self.title

在views.py 中添加什么?我尝试了各种方法,但我不知道该怎么做才能让它发挥作用。

this if my views.py 但我觉得整个事情都错了,所以我想要一个全新的视图:

class ComplaintCreate(CreateView):
    model = Complaints
    form = ComplaintForm
    fields = '__all__'
    success_url = reverse_lazy('New')
    template_name = 'new.html'

我希望最终页面看起来像这样:

【问题讨论】:

  • 我想如果你没有从models 导入CASCADE 这行user = models.ForeignKey(User, on_delete= CASCADE, null = True, blank=True) 不会像预期的那样工作它应该看起来像这样user = models.ForeignKey(User, on_delete= models.CASCADE, null = True, blank=True)

标签: django django-models django-views django-forms django-templates


【解决方案1】:

CreateView 中没有表单属性将form 更改为form_class

class ComplaintCreate(CreateView):
    model = Complaints
    form_class = ComplaintForm
    fields = '__all__'
    success_url = reverse_lazy('New')
    template_name = 'new.html'

【讨论】:

  • 哦……还有什么我应该在views.py中添加的吗??
  • 您的视图现在有效吗?是否要为投诉添加请求用户?
猜你喜欢
  • 2021-09-16
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 2012-07-22
  • 1970-01-01
  • 2016-10-11
  • 1970-01-01
相关资源
最近更新 更多