2017-10-19 10 views
1

私は患者データを作成して保存して表示するアプリケーションを作成しようとしています。フォームを保存しないオブジェクトを新しいフォームビューにリダイレクト

問題:フィールドにデータを入力して保存すると、オブジェクトが保存されず、ドキュメントオブジェクトモデルにデータがレンダリングされません。フィールドのデータをフィールドに残したままページを再読み込みします。

私は何が間違っているのか分かりません。ビュー、フォーム、モデルのコードは正確であるようです。私は有用な助けを歓迎します。

ここではコードである:forms.py

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) 

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> 
+0

ビューで 'form.errors'を、テンプレートで' {{form.errors}} 'を表示して、フォームが有効でない理由を確認してください。この問題は、テンプレートに表示されている可能性があります。 – Alasdair

+0

@ Alasdair問題文にテンプレートを追加しました。 –

答えて

1

あなたのテンプレートでレンダリングされているフィールドは、フォームのフィールドに一致しない - そうフォーム検証に失敗します。具体的には、フォームにcontactフィールドがあり、テンプレートでレンダリングしようとしているフィールドはform.Contactです。

ケースの問題 - contactContactと同じではなく、現時点ではcontactフィールドは表示されません。 form.errorsには、この問題を強調表示するエラーメッセージが表示されます。

フィールド名に大文字を使用すると、一貫性がなくなり、このようなミスを簡単にします。 Djangoでの条約は、フィールド名を小文字にすることです。

関連する問題