【发布时间】:2021-12-19 05:07:02
【问题描述】:
我有一个网页,它应该允许用户更新数据库中的数据,但数据无法在文本框中显示,我该如何让它能够在文本框中显示?
这是我当前的网页,用户需要在其中手动输入他们想要在文本框中更新的所有数据。
我想要的是这样,数据已经从数据库中检索出来供用户查看,假设他们要更新客户名称,他们只需要更改客户名称并单击提交按钮即可更新它,如何使它能够检索数据?
views.py
@login_required()
def updatedata(request, id):
photo = Photo.objects.get(id=id)
if request.method == 'POST':
Photo.mcoNum = request.POST.get('mcoNum')
Photo.reception = request.POST.get('reception')
form = UpdateForm(request.POST)
if form.is_valid():
form.save()
return redirect('logdata')
else:
form = UpdateForm
return render(request, 'updatedata.html', {'form': form})
updatedata.html
<!doctype html>
{% extends "home.html" %}
{% block content %}
{% load static %}
<br><br>
<h2 class="text-center">Edit Log Data</h2>
<hr>
<div class="col-md-6 offset-md-3">
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update" class="btn btn-secondary">
</form>
</div>
{% endblock %}
forms.py
class UpdateForm(forms.ModelForm):
mcoNum = forms.CharField(label="", widget=forms.TextInput(
attrs={"class": "form-control", "placeholder": "MCO Number"}))
reception = forms.CharField(label="", max_length=100, widget=forms.TextInput(
attrs={"class": "form-control", "placeholder": "reception"}))
partno = forms.CharField(label="", max_length=100, widget=forms.TextInput(
attrs={"class": "form-control", "placeholder": "Part Number"}))
serialno = forms.CharField(label="", max_length=100,
widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "Serial Number"}))
Customername = forms.CharField(label="", max_length=100,
widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "Customer Name"}))
class Meta:
model = Photo
fields = ("mcoNum", "reception", "partno", "serialno", "Customername",)
【问题讨论】:
标签: html django django-views django-forms