html中的Diplay ModelForm

时间:2019-07-08 14:25:42

标签: django

我正在创建一个Web应用程序,用户可以在其中创建一个project,在每个项目中他可以回答一个set of question(在我的应用程序中,我称他们为secondquestions)。

我正在按照以下指南尝试使用ModelForm简化表单的创建:https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/

但是,我不明白如何在html表单中显示。

secondquestions / views.py

def secondquestionstoanswer(request, project_id):
  project = get_object_or_404(Project, pk=project_id)
  if request.method == "POST":
    form = SecondquestionForm(request.POST)
    if form.is_valid():
      form.save()
  return render(request, 'secondquestions/secondquestionstoanswer.html', {'project':project})

secondquestions / models.py

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from projects.models import Project
from django.db import models
from django.forms import ModelForm

    class Secondquestion(models.Model):
        second_one = models.TextField()
        second_two = models.TextField()
        second_three = models.TextField()
        second_four = models.TextField()
        second_five = models.TextField()
        second_six = models.TextField()
        second_seven = models.TextField()
        second_eighth = models.TextField()
        second_nine = models.TextField()
        developer = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
        project = models.OneToOneField(Project, on_delete=models.CASCADE)


    class SecondquestionForm(ModelForm):
        class Meta:
            model = Secondquestion
            fields = ['second_one', 'second_two', 'second_three', 'second_four', 'second_five', 'second_six', 'second_seven', 'second_eighth', 'second_nine']

secondquestions / secondquestionstoanswer.html

{% extends 'base.html' %}
{% block title %}First set{% endblock %}
{% block content %}


    <form method="post">
      {% csrf_token %}
      {{ form }}
      <button type="submit">Submit</button>
    </form>

    {% endblock %}

我的问题: 在我的html中,我只看到“提交”按钮,而不是表单。

2 个答案:

答案 0 :(得分:2)

您从未构造过传递给模板引擎的表单。

from django.shortcuts import redirect

def secondquestionstoanswer(request, project_id):
    project = get_object_or_404(Project, pk=project_id)
    if request.method == "POST":
        form = SecondquestionForm(request.POST)
        if form.is_valid():
            form.instance.project = project
            form.save()
            return redirect('some_view')
    else:
        form = SecondquestionForm()
    return render(
        request,
        'secondquestions/secondquestionstoanswer.html',
        {'project':project, 'form': form}
    )

使用 'some_view' ,可以在提交成功后将视图名称重定向到您要重定向的位置。

<form>中,您可以在action="..."属性中指定触发端点的位置,例如:

<form method="post" action="{% url 'view_name_of_second_question' project.pk %}">
    {% csrf_token %}
    {{ form }}
    <button type="submit">Submit</button>
</form>

答案 1 :(得分:0)

您需要一个forms.py文件,其中应包含您的模型表格

secondquestions / forms.py

from django import forms
from .models import Secondquestion

class SecondquestionForm(forms.ModelForm):
    class Meta:
    model = Secondquestion
    fields = ['second_one', 'second_two', 'second_three', 'second_four', 'second_five', 'second_six', 'second_seven', 'second_eighth', 'second_nine']