尝试使用 django 节省输入 mysql 数据库的时间
Trying to save time input into mysql database using django
我正在尝试使用 Django 将 HTML 输入的数据保存到 MySql 数据库中。我创建的其他表格有效。我的结论是,由于时间输入,它不起作用。我已将其转换为 datettime python 类型,应该可以正常工作,但仍然无法保存。请你检查我的代码,看看它没有保存的可能原因是什么?
views.py 文件:
def employee_view(request):
table = employeeModel.objects.all()
context = {"table": table}
if request.method == "POST":
postCopy = request.POST.copy() # to make it mutable
postCopy['barberStartTime'] = datetime.strptime('15/05/22 ' + postCopy['barberStartTime'], '%d/%m/%y %H:%M').time()
postCopy['barberEndTime'] = datetime.strptime('15/05/22 ' + postCopy['barberEndTime'], '%d/%m/%y %H:%M').time()
request.POST = postCopy
form = employeeForm(request.POST or NONE)
if form.is_valid():
form.save()
return render(request, 'accounts/employee.html', context)
return render(request, 'accounts/employee.html', context)
python 员工型号:
class employeeModel(models.Model):
barberName = models.CharField(max_length=50)
barber_workplace = models.ForeignKey(workplacesModel, on_delete=models.CASCADE)
barberStartTime = models.TimeField()
barberEndTime = models.TimeField()
class barbers:
db_table = "home_employeemodel"
python员工表格:
class employeeForm(forms.ModelForm):
barberStartTime = forms.TimeField(widget=forms.TimeInput(format='%H:%M:%S'))
barberEndTime = forms.TimeField(widget=forms.TimeInput(format='%H:%M:%S'))
class Meta:
model = employeeModel
fields = ["barberName", "barber_workplace", "barberStartTime", "barberEndTime"]
html 输入表单:(它是 table 的一部分 - tr/td 标签的原因):
<tr>
<form method="POST">{% csrf_token %}
<td class="id-td" scope="row">---</td>
<td class="barberName-td"><input type="text" name="barberName" placeholder="Meno"/></td>
<td class="barberWorkplace-td"><input type="number" name="barberWorkplace" placeholder="Mesto"/></td>
<td class="barberStartTime-td"><input type="time" name="barberStartTime"/></td>
<td class="barberEndTime-td"><input type="time" name="barberEndTime"/></td>
<td><button class="btn btn-secondary">Pridať</button></td>
<form>
</tr>
所以我终于知道错在哪里了。问题是错误地命名了理发师工作场所的输入。它被命名为 barberWorkplace 而它应该被命名为 barber_workplace.
我正在尝试使用 Django 将 HTML 输入的数据保存到 MySql 数据库中。我创建的其他表格有效。我的结论是,由于时间输入,它不起作用。我已将其转换为 datettime python 类型,应该可以正常工作,但仍然无法保存。请你检查我的代码,看看它没有保存的可能原因是什么?
views.py 文件:
def employee_view(request):
table = employeeModel.objects.all()
context = {"table": table}
if request.method == "POST":
postCopy = request.POST.copy() # to make it mutable
postCopy['barberStartTime'] = datetime.strptime('15/05/22 ' + postCopy['barberStartTime'], '%d/%m/%y %H:%M').time()
postCopy['barberEndTime'] = datetime.strptime('15/05/22 ' + postCopy['barberEndTime'], '%d/%m/%y %H:%M').time()
request.POST = postCopy
form = employeeForm(request.POST or NONE)
if form.is_valid():
form.save()
return render(request, 'accounts/employee.html', context)
return render(request, 'accounts/employee.html', context)
python 员工型号:
class employeeModel(models.Model):
barberName = models.CharField(max_length=50)
barber_workplace = models.ForeignKey(workplacesModel, on_delete=models.CASCADE)
barberStartTime = models.TimeField()
barberEndTime = models.TimeField()
class barbers:
db_table = "home_employeemodel"
python员工表格:
class employeeForm(forms.ModelForm):
barberStartTime = forms.TimeField(widget=forms.TimeInput(format='%H:%M:%S'))
barberEndTime = forms.TimeField(widget=forms.TimeInput(format='%H:%M:%S'))
class Meta:
model = employeeModel
fields = ["barberName", "barber_workplace", "barberStartTime", "barberEndTime"]
html 输入表单:(它是 table 的一部分 - tr/td 标签的原因):
<tr>
<form method="POST">{% csrf_token %}
<td class="id-td" scope="row">---</td>
<td class="barberName-td"><input type="text" name="barberName" placeholder="Meno"/></td>
<td class="barberWorkplace-td"><input type="number" name="barberWorkplace" placeholder="Mesto"/></td>
<td class="barberStartTime-td"><input type="time" name="barberStartTime"/></td>
<td class="barberEndTime-td"><input type="time" name="barberEndTime"/></td>
<td><button class="btn btn-secondary">Pridať</button></td>
<form>
</tr>
所以我终于知道错在哪里了。问题是错误地命名了理发师工作场所的输入。它被命名为 barberWorkplace 而它应该被命名为 barber_workplace.