python - 我的 Django 更新表单没有保存,也没有从患者注册时使用的详细信息中将实例显示为患者
问题描述
当我转到该页面时,我的 django 项目无法识别患者详细信息的实例,并且它也没有保存我输入的新患者详细信息,而是将我带回与我输入的新详细信息相同的页面。当我再次按下提交按钮时,会发生同样的事情(带有新详细信息的同一页面)
模型.py
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils import timezone
class User(AbstractUser):
is_patient = models.BooleanField(default=False)
is_doctor = models.BooleanField(default=False)
class Patient(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
insurance = models.CharField(max_length=200)
insuranceID = models.CharField(max_length=200)
address = models.CharField(max_length=200)
date_of_birth = models.DateField(default=timezone.now)
gender = models.CharField(max_length=200)
phone = models.CharField(max_length=200)
current_medication = models.TextField(null=True, blank=True)
preexisting_medication = models.TextField(null=True, blank=True)
next_of_kin = models.TextField(null=True, blank=True)
def __str__(self):
return self.user.username
class Doctor(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
name = models.CharField(max_length=200)
date_of_birth = models.DateField(default=timezone.now)
gender = models.CharField(max_length=200)
phone = models.CharField(max_length=200)
address = models.CharField(max_length=200)
speciality = models.CharField(max_length=200)
npi= models.CharField(max_length=200)
education = models.TextField()
qualifications = models.TextField()
places_worked = models.TextField()
consultation_fee = models.CharField(max_length=200)
venue_of_operation = models.CharField(max_length=200)
def __str__(self):
return self.user.username
表格.PY
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from .models import Patient,Doctor, User
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
('O','Other')
)
class DoctorSignUpForm(UserCreationForm):
email = forms.EmailField()
name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'DR.'}),max_length=200)
date_of_birth = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
gender = forms.ChoiceField(choices=GENDER_CHOICES, widget=forms.RadioSelect())
phone = forms.CharField(widget=forms.DateInput(attrs={'type': 'number'}),max_length=200)
address = forms.CharField(widget=forms.Textarea)
speciality = forms.CharField(max_length=200)
npi = forms.CharField(max_length=200)
education = forms.CharField(widget=forms.Textarea)
qualifications = forms.CharField(widget=forms.Textarea)
places_worked = forms.CharField(widget=forms.Textarea)
consultation_fee = forms.CharField(widget=forms.TextInput(attrs={'type': 'number'}),max_length=200)
venue_of_operation = forms.CharField(max_length=200)
class Meta(UserCreationForm.Meta):
model = User
@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_doctor = True
user.save()
doctor = Doctor.objects.create(user=user)
doctor.email = self.cleaned_data.get('email')
doctor.name = self.cleaned_data.get('name')
doctor.date_of_birth = self.cleaned_data.get('date_of_birth')
doctor.gender = self.cleaned_data.get('gender')
doctor.phone = self.cleaned_data.get('phone')
doctor.address = self.cleaned_data.get('address')
doctor.speciality = self.cleaned_data.get('speciality')
doctor.npi = self.cleaned_data.get('npi')
doctor.education = self.cleaned_data.get('education')
doctor.qualifications = self.cleaned_data.get('qualifications')
doctor.places_worked = self.cleaned_data.get('places_worked')
doctor.consultation_fee = self.cleaned_data.get('consultation_fee')
doctor.venue_of_operation = self.cleaned_data.get('venue_of_operation')
doctor.save()
return user
class PatientSignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=200)
last_name = forms.CharField(max_length=200)
insurance = forms.CharField(max_length=200)
insuranceID = forms.CharField(max_length=200)
address = forms.CharField(max_length=200)
email = forms.EmailField()
date_of_birth = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
gender = forms.ChoiceField(choices=GENDER_CHOICES, widget=forms.RadioSelect())
phone = forms.CharField(widget=forms.TextInput(attrs={'type': 'number'}),max_length=200)
current_medication = forms.CharField(widget=forms.Textarea)
preexisting_medication = forms.CharField(widget=forms.Textarea)
next_of_kin = forms.CharField(widget=forms.Textarea)
class Meta(UserCreationForm.Meta):
model = User
@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_patient = True
user.save()
patient = Patient.objects.create(user=user)
patient.first_name = self.cleaned_data.get('first_name')
patient.last_name = self.cleaned_data.get('last_name')
patient.insurance = self.cleaned_data.get('insurance')
patient.insuranceID = self.cleaned_data.get('insuranceID')
patient.address = self.cleaned_data.get('address')
patient.email = self.cleaned_data.get('email')
patient.date_of_birth = self.cleaned_data.get('date_of_birth')
patient.gender = self.cleaned_data.get('gender')
patient.phone = self.cleaned_data.get('phone')
patient.current_medication = self.cleaned_data.get('current_medication')
patient.preexisting_medication = self.cleaned_data.get('preexisting_medication')
patient.next_of_kin = self.cleaned_data.get('next_of_kin')
patient.save()
return user
class PatientUpdateForm(forms.ModelForm):
first_name = forms.CharField(max_length=200)
insurance = forms.CharField(max_length=200)
insuranceID = forms.CharField(max_length=200)
address = forms.CharField(max_length=200)
email = forms.EmailField()
date_of_birth = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
gender = forms.ChoiceField(choices=GENDER_CHOICES, widget=forms.RadioSelect())
phone = forms.CharField(widget=forms.TextInput(attrs={'type': 'number'}),max_length=200)
current_medication = forms.CharField(widget=forms.Textarea)
preexisting_medication = forms.CharField(widget=forms.Textarea)
next_of_kin = forms.CharField(widget=forms.Textarea)
class Meta:
model = User
fields = ['first_name','insurance','phone','email','date_of_birth','insuranceID','address','gender','current_medication','preexisting_medication','next_of_kin']
@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_patient = True
user.save()
patient = Patient.objects.create(user=user)
patient.first_name = self.cleaned_data.get('first_name')
patient.insurance = self.cleaned_data.get('insurance')
patient.insuranceID = self.cleaned_data.get('insuranceID')
patient.address = self.cleaned_data.get('address')
patient.email = self.cleaned_data.get('email')
patient.date_of_birth = self.cleaned_data.get('date_of_birth')
patient.gender = self.cleaned_data.get('gender')
patient.phone = self.cleaned_data.get('phone')
patient.current_medication = self.cleaned_data.get('current_medication')
patient.preexisting_medication = self.cleaned_data.get('preexisting_medication')
patient.next_of_kin = self.cleaned_data.get('next_of_kin')
patient.save()
return user
视图.py
def conditions(request):
if request.method == 'POST':
u_form = PatientUpdateForm(request.POST, instance=request.user)
if u_form.is_valid():
u_form.save()
return redirect('profile')
else:
u_form = PatientUpdateForm(instance=request.user)
context = {'u_form': u_form}
return render(request, 'patient/medicalconditions.html', context)
HTML 模板
{% load static %} {% load crispy_forms_tags %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link
href="{% static '../../static/patient/css/style.css' %}"
rel="stylesheet"
/>
<link rel="stylesheet" href="../../static/patient/css/bootstrap.css" />
<link
rel="stylesheet"
href="../../static/patient/css/fontawesome/css/all.css"
/>
</head>
<body style="background-image: url('../../static/patient/images/forest.jpg')">
<div style="background-color: rgba(255, 255, 255, 0.7)">
<div style="background-color: rgb(11, 11, 46); height: 100px">
<span class="text-start display-2"
><a class="text-decoration-none text-light" href="{% url 'home' %}"
>AVANI</a
></span
>
<span class="btn-group float-end clearfix align-items-middle mt-5">
<a
href="{% url 'contact' %}"
class="me-5 float-start text-light text-decoration-none"
>Contact Us</a
>
</span>
</div>
<h5 style="margin-left:70px">Medical Information</h5>
<div
style="
background-color: rgba(224, 102, 102, 0.267);
height: 70%;
width: 90%;
margin: auto;
"
class="my-3 rounded"
>
<div style="width: 90%; margin: auto" class="py-3">
<form method="POST">
{% csrf_token %}
<div class="row">
<div class="col">
{{ u_form.first_name.errors }}
{{ u_form.first_name|as_crispy_field }}
</div>
<div class="col">
{{ u_form.insurance.errors }}
{{ u_form.insurance|as_crispy_field }}
</div>
<div class="col">
{{ u_form.phone.errors }}
{{ u_form.phone|as_crispy_field }}
</div>
<div class="col">
{{ u_form.email.errors }}
{{ u_form.email|as_crispy_field }}
</div>
</div>
<div class="row">
<div class="col">
{{ u_form.date_of_birth.errors }}
{{ u_form.date_of_birth|as_crispy_field }}
</div>
<div class="col">
{{ u_form.insuranceID.errors }}
{{ u_form.insuranceID|as_crispy_field }}
</div>
<div class="col">
{{ u_form.address.errors }}
{{ u_form.address|as_crispy_field}}
</div>
<div class="col">
{{ u_form.gender.errors }}
{{ u_form.gender|as_crispy_field }}
</div>
</div>
<div class="text-center">
{{u_form.current_medication.errors}}
<div class="mb-3">
{{u_form.current_medication|as_crispy_field}}
</div>
{{u_form.preexisting_medication.errors}}
<div class="mb-3">
{{u_form.preexisting_medication|as_crispy_field}}
</div>
{{u_form.next_of_kin.errors}}
<div class="mb-3">
{{u_form.next_of_kin|as_crispy_field}}
</div>
</div>
<div class="text-center mt-3">
<button class="btn btn-dark" type="submit">Submit</button>
</div>
</form>
</div>
</div>
<div class="" style="margin-left: 70px;">
<div class="py-3">
<button type="submit" class="btn btn-primary"> Logout</button>
</div>
<div class="py-3">
<button type="submit" class="btn btn-primary"> Delete Account</button>
</div>
</div>
<footer
class="text-center text-lg-start"
style="background-color: rgb(11, 11, 46)"
>
<!-- Grid container -->
<div class="p-4">
<!--Grid row-->
<div class="row">
<!--Grid column-->
<div class="col-lg-3 col-md-6 mb-4 mb-md-0">
<h5 class="text-uppercase text-light">Avani</h5>
<ul class="list-unstyled mb-0">
<li>
<a class="text-light" href="{% url 'home' %}">Home</a>
</li>
<li>
<a class="text-light" href="{% url 'contact' %}"
>Contact Us</a
>
</li>
</ul>
</div>
<!--Grid column-->
<!--Grid column-->
<div class="col-lg-3 col-md-6 mb-4 mb-md-0">
<h5 class="text-uppercase mb-0 text-light">Insurance</h5>
<ul class="list-unstyled">
<li>
<a class="text-light" href="#!">Britam</a>
</li>
<li>
<a class="text-light" href="#!">Britam</a>
</li>
<li>
<a class="text-light" href="#!">Britam</a>
</li>
<li>
<a class="text-light" href="#!">Britam</a>
</li>
</ul>
</div>
<!--Grid column-->
<!--Grid column-->
<div class="col-lg-3 col-md-6 mb-4 mb-md-0">
<h5 class="text-uppercase text-light">Specialists</h5>
<ul class="list-unstyled mb-0">
<li>
<a class="text-light" href="#!">Dermatologist</a>
</li>
<li>
<a class="text-light" href="#!">Orthodontist</a>
</li>
<li>
<a class="text-light" href="#!">Neurosurgeon</a>
</li>
</ul>
</div>
<!--Grid column-->
</div>
<!--Grid row-->
</div>
<!-- Grid container -->
</footer>
</div>
</body>
</html>
解决方案
我终于明白了
u_form = PatientUpdateForm(instance=request.user.patient)
推荐阅读
- c++ - 如何使用 QCustomPlot 作为头文件?
- grpc - 什么是 grpc 中的 _MultiThreadedRendezvous 以及如何解析它
- c++ - 为什么我的析构函数没有在程序结束时被调用?
- docker - express js中间件没有使用docker-compose连接
- excel - 如果时间戳发生在工作时间之外,如何将日期时间戳提前到下一个工作日的开始?
- java - 如何通过HTTP协议远程调试java
- function - Common Lisp 中的类型化 lambda 函数
- javascript - 无需请求网页即可获得连接
- python - Python 添加 pytest --black 到测试套件
- python - 从 url 中提取单词