【问题标题】:django the following data would have been submitted to the serverdjango 以下数据将已提交到服务器
【发布时间】:2021-02-17 18:24:50
【问题描述】:

我正在尝试使用 django 创建问答调查,但数据未保存在数据库中 enter image description here

django 以下数据会被提交到服务器

views.py

def add_questions(request):
    form = QuestionForm(request.POST or None)
    if form.is_valid():
        form.save()

        return HttpResponseRedirect(reverse('users:question'))


    form = QuestionForm()
    context = {
            'form': form
        }
    return render(request, 'pages/data-entry-tool/add-question.html', context)

html

<!-- Main content -->
{% block content %}
<section class="content">
    <h2>Add New Question</h2>
    <form method="post" class="form container">
        {% csrf_token %}
        
        {{form.as_p}}

        <button type="submit" class="btn btn-danger">Save</button>

    </form>
</section>


{% endblock %}

型号

django 以下数据会被提交到服务器

class Questions(models.Model):
    title = models.CharField(max_length=250, blank=False)
    values = models.CharField(max_length=1500, blank=False)
    type = models.CharField(max_length=50, )
    is_required = models.CharField(max_length=10)
    min_char = models.IntegerField()
    max_char = models.IntegerField()
    placeholder = models.CharField(max_length=250, blank=False)
    created_at = models.DateTimeField(
        default=timezone.now)
    updated_at = models.DateTimeField(default=timezone.now)
    deleted_at = models.DateTimeField(blank=True, null=True)
    is_deleted = models.BooleanField(default=True)

    def __str__(self):
        return self.title

    def soft_delete(self):
        self.is_deleted = True
        self.deleted_at = timezone.now()
        self.save()

forms.py 我正在尝试使用 django 创建问答调查,但数据未保存在数据库中

django 以下数据会被提交到服务器

class QuestionForm(forms.ModelForm):
    TYPE_CHOICES = (
        ('select', 'Select'),
        ('chekbox', 'chekbox'),
        ('radio', 'radio'),
        ('list', 'list (select)'),
        ('textarea', 'textarea'),
    )
    REQUİRED_CHOICES = (
        ('1', 'Active'),
        ('0', 'Passive')
    )
    İS_DELETE_CHOICES = (
        ('1', 'True'),
        ('0', 'False')
    )

    title = forms.CharField(label="Title")
    values = forms.CharField(label="Question", widget=forms.Textarea)
    type = forms.ChoiceField(label="Type", choices=TYPE_CHOICES)
    is_required = forms.ChoiceField(widget=forms.RadioSelect(attrs={'class': 'Radio'}), initial=1,
                                    choices=REQUİRED_CHOICES)
    is_deleted =forms.ChoiceField(widget=forms.RadioSelect(attrs={'class': 'Radio'}), initial=1,
                                    choices=İS_DELETE_CHOICES)
    min_char = forms.IntegerField(label="min_char")
    max_char = forms.IntegerField(label="max_char")
    placeholder = forms.CharField(label="Placeholder")


    class Meta:
        model = Questions
        fields = ['title', 'values', 'type', 'is_required', 'min_char', 'max_char','is_deleted' ,'placeholder']

【问题讨论】:

  • 你能把add-question.html的html也包括进来吗
  • 我添加了 html @Danoram
  • 当我使用您提供的模型和表单创建 django 应用程序时,我可以成功地将 Questions 对象保存到数据库中。你用的是什么数据库?
  • 我要添加的其他内容,将您的模型字段从 type 重命名为 question_type 之类的其他名称。 type 是您正在覆盖的内置 python 方法。
  • mysql 数据库@Danoram

标签: python-3.x django django-models django-views django-forms


【解决方案1】:

根据您的问题提供的信息,您的表单很可能无效,因此没有调用form.save()

def add_questions(request):

    form = QuestionForm()

    if request.method == 'POST':
        form = QuestionForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('users:question'))

    context = {
            'form': form
        }
    return render(request, 'pages/data-entry-tool/add-question.html', context)

这是一个修改后的视图功能,你可以试试。这里的区别在于,如果表单无效,将再次呈现相同的视图,但会显示表单中的任何错误。

【讨论】:

  • 问题依旧
  • 好的,下一步是将您的 forms.py 和 models.py 包含在问题中,以便我们在那里查看
  • 我在表单和模型中添加了@Danoram
猜你喜欢
  • 1970-01-01
  • 2020-11-27
  • 2015-02-12
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多