python - 表单不保存对象重定向到新表单 View

标签 python django forms

我正在尝试构建一个应用程序来创建患者数据、保存并显示它。

问题:当我填充字段并单击“保存”时,它不会保存对象并将数据呈现到文档对象模型。它重新加载页面,字段数据仍在字段中。

我不知道我做错了什么。 View 、表单和模型代码似乎是准确的。我欢迎任何有用的帮助。

Here is the code :

models.py

from django.db import models
from django.contrib.auth.models import User
from Identity import settings
import datetime


class Identity_unique(models.Model):

    NIS = models.CharField(max_length = 200, primary_key = True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    Timestamp = models.DateTimeField(auto_now = True)
    first_Name = models.CharField(max_length = 80, null = True )
    last_Name = models.CharField(max_length = 80, null = True )
    location = models.CharField(max_length = 100, blank = True)
    date_of_birth = models.DateField(auto_now = False, auto_now_add = False, blank = True, null = True)
    contact = models.CharField(max_length = 15, null = True)

views.py

from django.shortcuts import render, redirect
from django.views.generic import TemplateView, UpdateView
from nesting.forms import Identity_Form, Symptom_Form
from nesting.models import Identity_unique, Symptom_relation


class Identity_view(TemplateView):

    template_name = 'nesting/nesting.html'

    def get(self, request):
        form = Identity_Form()
        Identities = Identity_unique.objects.filter(user = request.user)
        var = {'form': form, 'Identities': Identities}
        return render(request, self.template_name, var)

    def post(self, request):
        form  = Identity_Form(request.POST or None)
        content = None
        if form.is_valid():
            NIS = form.save(commit = False)
            NIS.user = request.user
            NIS.save()

            content = form.cleaned_data['NIS']
            form = Identity_Form()
            return redirect('nesting:nesting')

        var = {'form': form, 'content': content}
        return render(request,self.template_name, var)

forms.py

from django import forms
from nesting.models import Identity_unique
from nesting.models import Symptom_relation


class Identity_Form(forms.ModelForm):

    NIS = forms.CharField(
                    widget=forms.TextInput(
                            attrs={

                                'placeholder': 'Enter NIS',
                                'class' : 'form-control'
                            }
                )
    )

    first_Name = forms.CharField(
                widget=forms.TextInput(
                        attrs={

                            'placeholder': 'Enter First Name',
                            'class' : 'form-control'
                        }
            )
    )
    last_Name = forms.CharField(

       widget=forms.TextInput(
               attrs={

                   'placeholder': 'Enter Last Name',
                   'class' : 'form-control'
               }
        )
    )

    location = forms.CharField(

            widget=forms.TextInput(
                        attrs= {

                        'placeholder':'Enter Address',
                        'class':'form-control'

                        }
            )
    )

    date_of_birth = forms.DateField(

            required = False,
                widget=forms.TextInput(
                            attrs= {

                            'placeholder' : 'Enter Birthday',
                            'class':'form-control'

                            }
                ),
        )

    contact = forms.CharField(

                    widget=forms.TextInput(
                                attrs= {

                                'placeholder':'Enter Contact',
                                'class':'form-control'

                                }
                    )
            )


    class Meta:

        model = Identity_unique

        fields = ('NIS', 'first_Name', 'last_Name', 'location', 'date_of_birth', 'contact',)

编辑

nesting.html

<form method = 'post' novalidate>
    {% csrf_token %}

    {% if form.non_field_errors %}
        <div class="alert alert-danger" role="alert">
        {% for error in form.non_field_errors %}
          {{ error }}
        {% endfor %}
        </div>
    {% endif %}

    <div class = "form-row">
        <div class = "form-group col-md-6">
        {{ form.NIS.errors }}
        {{ form.NIS }}
        </div>

        <div class = "form-group col-md-6">
        {{form.Contact.errors}}
        {{form.Contact}}
        </div>
    </div>

    <div class = "form-row">
        <div class = "form-group col-md-6">
        {{form.first_Name.errors}}
        {{form.first_Name}}
        </div>

        <div class = "form-group col-md-6">
        {{form.last_Name.errors}}
        {{form.last_Name}}
        </div>
    </div>

    <div class = "form-row">
        <div class = "form-group col-md-6">
        {{form.location.errors}}
        {{form.location}}
        </div>

        <div class = "form-group col-md-6">
        {{form.date_of_birth.errors}}
        {{form.date_of_birth}}
        </div>
    </div>
    <button class = "btn-primary btn-large btn ">Submit</button>
</form>

最佳答案

您在模板中呈现的字段与表单中的字段不匹配 - 因此表单将无法验证。具体来说,您的表单有一个 contact 字段,您尝试使用 form.Contact 在模板中呈现该字段。

大小写很重要 - contactContact 不同,因为目前您的 contact 字段根本不会被渲染。 form.errors 将包含一条错误消息,突出显示此问题。

您在字段名称中使用大写字母的情况非常不一致,这很容易犯这样的错误。 Django 中的约定是字段名称小写。

关于python - 表单不保存对象重定向到新表单 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46840474/

相关文章:

python - 具有动态输入的 SQLAlchemy with_entities

python - 100% 堆积条形图 - python (seaborn/matplotlib)

Python Pandas 合并数据帧而不重复列

python - 如何在 tf.while_loop 中使用 tf.scatter_update

python - Django查询优化: find a list of objects based on a many-to-one to a many-to-many

javascript - 选择元素未正确重置

java - Django 概念如何映射到 Java、servlet、Struts 1 和 JSP 页面?

javascript - JS静态文件的管理: code seems to be downloaded but do not work

javascript - JS - 将相同的功能附加到许多 html 表单

html - 如何在不同尺寸的屏幕上用背景图片在合适的位置设置表格