gpt4 book ai didi

python - 仅一个表单字段无法显示数据库中的值

转载 作者:行者123 更新时间:2023-12-01 08:26:23 25 4
gpt4 key购买 nike

我的模型:

class DirectoryDoctors (models.Model):
num = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
design_choices = (
('IMO', 'IMO'),
('AIMO', 'AIMO'),
('Dental Surgeon', 'Dental Surgeon'),
('Regional Deputy Director', 'Regional Deputy Director'),
('Joint Director', 'Joint Director'),
('Director', 'Director'),
('unspecified', 'Unspecified')
)
designation = models.CharField(
choices=design_choices, max_length=30, default='unspecified')
mobile = models.CharField(max_length=15, default='')
alternate = models.CharField(max_length=15, default='', blank=True)
email = models.CharField(max_length=50, default='', blank=True)
dob = models.DateField(null=True, blank=True)
specialiast_or_not_choices = (
('Yes', 'Yes'),
('No', 'No'),
('Unspecified', 'Unspecified')
)
specialiast = models.CharField(
choices=specialiast_or_not_choices, max_length=30, default='Unspecified')
specialty_choices = (
('Internal Medicine', 'Internal Medicine'),
('General Surgery', 'General Surgery'),
('ENT', 'ENT'),
('Ophthalmology', 'Ophthalmology'),
('Dermatology', 'Dermatology'),
('Paediatrics', 'Paediatrics'),
('Respiratory Medicine', 'Respiratory Medicine'),
('Psychiatry', 'Psychiatry'),
('Obstetrics and Gynaecology', 'Obstetrics and Gynaecology'),
('Physical Medicine', 'Physical Medicine'),
('Radiodiagnosis', 'Radiodiagnosis'),
('Anaesthesia', 'Anaesthesia'),
('Unspecified', 'Unspecified'),
('Not Applicable', 'Not Applicable')
)
specialty = models.CharField(
choices=specialty_choices, max_length=30, default='Unspecified')
institution = models.ForeignKey(DirectoryHospital, on_delete=models.DO_NOTHING)
bloodgroup_choices = (('apos', 'A+'),
('A-', 'A-'),
('B+', 'B+'),
('B-', 'B-'),
('O+', 'O+'),
('O-', 'O-'),
('AB+', 'AB+'),
('AB-', 'AB-'),
('-', '-')
)
bloodgroup = models.CharField(choices=bloodgroup_choices, max_length=15, blank=True)
spousename = models.CharField(max_length=100, blank=True)
children = models.CharField(max_length=200, blank=True)
present_address = models.CharField(max_length=200, blank=True)
permanent_address = models.CharField(max_length=200, blank=True)

class Meta:
unique_together = ["name", "mobile", "email"]

我的表格:

class DirectoryDoctorsForm(ModelForm):
class Meta:
model = DirectoryDoctors
fields = [
'name',
'designation',
'mobile',
'alternate',
'email',
'dob',
'specialiast',
'specialty',
'institution',
'bloodgroup',
'spousename',
'children',
'present_address',
'permanent_address'
]
unique_together = ["name", "mobile", "email"]

我的看法:

@login_required
def editdoctorprofile(request, person_id):
person_id = int(person_id)
doc = DirectoryDoctors.objects.get(num = person_id)
msg =''
if request.method == 'POST':
print("POST details", request.POST)
form = DirectoryDoctorsForm(request.POST)
if form.is_valid():
print("Form is valid.")
username = request.POST.get('username')
else:
msg = "Invalid details."
print(msg)
else:
form = DirectoryDoctorsForm(instance=doc)

return render(
request, 'app/editdoctor.html', {
'rnd_num': randomnumber(),
'doc': doc,
'form': form
})

app/editdoctor.html:

<form method="post">
{% csrf_token %}

<div class="row my-1">
<div class="col-md-2 mb-6">
<label for="name">Name</label>
</div>
<div class="col-md-18 mb-6">
{{ form.name|add_class:"form-control" }}
</div>
</div>

<div class="row my-1">
<div class="col-md-2 mb-6">
<label for="designation">Designation</label>
</div>
<div class="col-md-18 mb-6">
{{ form.designation|add_class:"form-control" }}
</div>
</div>

<div class="row my-1">
<div class="col-md-2 mb-6">
<label for="mobile">Mobile</label>
</div>
<div class="col-md-18 mb-6">
{{ form.mobile|add_class:"form-control" }}
</div>
</div>

<div class="row my-1">
<div class="col-md-2 mb-6">
<label for="designation">Alternate</label>
</div>
<div class="col-md-18 mb-6">
{{ form.alternate|add_class:"form-control" }}
</div>
</div>

<div class="row my-1">
<div class="col-md-5 mb-6">
<label for="specialiast">Are you a Specialiast?</label>
</div>
<div class="col-md-15 mb-6">
{{ form.specialiast|add_class:"form-control" }}
</div>
</div>

<div class="row my-1">
<div class="col-md-6 mb-6">
<label for="specialty">If yes, what is your Specialty?</label>
</div>
<div class="col-md-14 mb-6">
{{ form.specialty|add_class:"form-control" }}
</div>
</div>
<div class="row my-1">
<div class="col-md-2 mb-6">
<label for="institution">Station</label>
</div>
<div class="col-md-18 mb-6">
{{ form.institution|add_class:"form-control" }}
</div>
</div>
<div class="row my-1">
<div class="col-md-3 mb-6">
<label for="bloodgroup">Blood Group</label>
</div>
<div class="col-md-17 mb-6">
{{ form.bloodgroup|add_class:"form-control" }}
</div>
</div>
<div class="row my-1">
<div class="col-md-5 mb-6">
<label for="spousename">Name of your spouse</label>
</div>
<div class="col-md-15 mb-6">
{{ form.spousename|add_class:"form-control" }}
</div>
</div>
<div class="row my-1">
<div class="col-md-5 mb-6">
<label for="children">Name of your children</label>
</div>
<div class="col-md-15 mb-6">
{{ form.children|add_class:"form-control" }}
</div>
</div>
<div class="row my-1">
<div class="col-md-4 mb-6">
<label for="present_address">Present Address</label>
</div>
<div class="col-md-16 mb-6">
{{ form.present_address|add_class:"form-control" }}
</div>
</div>
<div class="row my-1">
<div class="col-md-4 mb-6">
<label for="permanent_address">Permanent Address</label>
</div>
<div class="col-md-16 mb-6">
{{ form.permanent_address|add_class:"form-control" }}
</div>
</div>
<div class="row">
<div class="col-md-4 mb-1">
<button class="btn btn-primary btn-block" id="add_doctor" type="submit">Save changes</button>
</div>
</div>

显示此页面时,例如对于 pk 为 175 的查询集 get,仅表单字段血型无法正确显示。它显示值“-----”

但是如果在 django shell 上,我会这样做:

>>> from app.models import DirectoryDoctors,DirectoryHospital
>>> DirectoryDoctors.objects.get(pk=175)
<DirectoryDoctors: Mike Dewey, Deputy Director, Deputy Directorate Angamaly, Angamaly>
>>> DirectoryDoctors.objects.get(pk=175).bloodgroup
'A+'
>>>

为什么仅此字段无法正确显示?

最佳答案

该字段的值似乎无效。

您将bloodgroup字段的第一个选择定义为:

bloodgroup_choices = (
('apos', 'A+'),
...
)

但是在打印该值时,它显示A+(正如您向我们展示的那样):

>>> DirectoryDoctors.objects.get(pk=175).bloodgroup
'A+'

但它必须是apos,小部件才能在 HTML 表单中识别它

<小时/>

如果您尝试使用您的数据,此方法调用应该会突出显示问题:

>>> DirectoryDoctors.objects.get(pk=175).get_bloodgroup_display()

关于python - 仅一个表单字段无法显示数据库中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54217206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com