通过ModelForm创建的对象未显示在列表中

时间:2019-06-17 05:52:57

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

我创建了一个模型,只是试图在后台使用ModelForm存储详细信息,但是我无法做到这一点,因为每当我尝试提交表单时,它总是显示我views.py的其他部分->请重试。

如果有人知道我在做什么错,请告诉我。

P.S->我的templates.html中有两个表单方法,但是我现在没有为此编写任何代码,只是尝试现在保存第一个。

我试图通过在此处寻找一些答案来解决此问题,但没有成功。

models.py

class OrgMember(models.Model):
    org_poc = models.CharField(max_length=100, blank=False, verbose_name="Organization POC")
    org_name = models.CharField(max_length=100, blank=False, verbose_name="Organisation Name")
    phone = models.CharField(max_length=10, blank=False, verbose_name="Phone Number")
    email = models.EmailField(blank=False, unique=True, verbose_name="Email ID")

    def __str__(self):
        return self.email

forms.py

class OrgMembersForm(forms.ModelForm):
    class Meta:
        model = OrgMember
        fields = ['org_poc', 'org_name', 'phone', 'email']

views.py

def org_member_view(request):
    if request.method == "POST":
        form = OrgMembersForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, "Member Added Successfully.")
            return redirect('users-members')
        else:
            messages.error(request, "Please Try Again.")
    else:
        form = OrgMembersForm()
    members = OrgMember.objects.all()
    context = {'form': form, 'members': members}
    return render(request, 'users/members.html', context)

template.html

我要在其中存储数据的表

  <table class="table table-responsive">
            <thead>
              <tr>
                <th>#</th>
                <th class="th-lg">Organization POC</th>
                <th class="th-lg">Organization Name</th>
                <th class="th-lg">Phone</th>
                <th class="th-lg">Email</th>
              </tr>
            </thead>
            <tbody>
              {% for member in members %}
              <tr>

                <th scope="row">{{ forloop.counter }}</th>
                <td>{{ member.org_poc }}</td>
                <td>{{ member.org_name }}</td>
                <!--              <td>Table cell</td>-->
                <td>{{ member.phone }}</td>
                <td>{{ member.email }}</td>

              </tr>
              {% endfor %}
            </tbody>
          </table>

表单方法

<form method="POST" action="" enctype="multipart/form-data">
      {% csrf_token %}
      <div class="card">

        <div class="card-body">
          <!--Header -->
          <!--Body -->
          <div class="md-form">
            <i class="fas fa-user prefix grey-text"></i>
            <input type="text" id="form-name" class="form-control">
            <label for="form-name">Organization POC</label>
          </div>
          <div class="md-form">
            <i class="fas fa-user prefix grey-text"></i>
            <input type="text" id="form-name" class="form-control">
            <label for="form-name">Organization Name </label>
          </div>
          <div class="md-form">
            <i class="fas fa-user prefix grey-text"></i>
            <input type="text" id="form-name" class="form-control">
            <label for="form-name">Phone</label>
          </div>

          <div class="md-form">
            <i class="fas fa-envelope prefix grey-text"></i>
            <input type="text" id="form-email" class="form-control">
            <label for="form-email">Email </label>
          </div>
        </div>
      </div>
      <!--Form with header -->

      <!-- Footer -->
      <div class="modal-footer justify-content-center">
        <button type="submit" class="btn btn-success">Submit <i class="far fa-gem ml-1"></i></button>
        <a type="button" class="btn btn-outline-success waves-effect" data-dismiss="modal">Cancel</a>
      </div>
    </form>

1 个答案:

答案 0 :(得分:1)

您的表单在提交时无效,但是您从不输出错误消息或日志输出。

查看有关如何在模板的表单内输出错误消息的文档:https://docs.djangoproject.com/en/2.2/topics/forms/#rendering-fields-manually

或者-至少-将日志输出添加到代码中的该部分:

import logging
log = logging.getLogger(__name__)

# .... further down
    else:
        messages.error(request, "Please Try Again.")
        log.error("form is invalid: %s", form.errors)

但是,我建议将其添加到表单中,以便在表单无效时通知您的用户。