【发布时间】:2021-03-03 13:33:07
【问题描述】:
我的简单网络应用程序有两个链接的模型(一对多)。
第一个模型 (Newplate) 有一个名为 plate_complete 的布尔字段。这在开始时设置为False (0)。
问题:
-
在一个 html 页面中,我正在尝试构建一个表单和按钮,按下该按钮会将上述字段设置为
True。在我单击按钮时,页面刷新但数据库没有变化(plate_complete仍然是False)。我该怎么做? -
理想情况下,一旦按下按钮,我还想将用户重定向到另一个网页 (
readplates.html)。此网页不需要 pk 字段(但表单确实可以更改特定记录)因此现在我只是刷新extendingplates.html文件。我该怎么做呢?
我的代码:
"""Model"""
class NewPlate(models.Model):
plate_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=200)
created_date = models.DateTimeField(default=timezone.now)
plate_complete = models.BooleanField()
"""view"""
def publish_plates(request,plate_id):
newplate = get_object_or_404(NewPlate, pk=plate_id)
newplate.plate_complete = True
newplate.save()
#2nd method
NewPlate.objects.filter(pk=plate_id).update(plate_complete = True)
return HttpResponseRedirect(reverse('tablet:extendplates', args=[plate_id]))
"""URLS"""
path('readplates', views.read_plates, name='readplates'),
path('extendplates/<pk>/', views.show_plates, name='showplates'),
path('extendplates/<pk>/', views.publish_plates, name='publishplates'),
"""HTML"""
<form method="POST" action="{% url 'tablet:publishplates' newplate.plate_id %}">
{% csrf_token %}
<button type="submit" class="button" value='True'>Publish</button></form>
-------Added show plates view:---------
def show_plates(request,pk):
mod = NewPlate.objects.all()
newplate= get_object_or_404(mod, pk=pk)
add2plate= Add2Plate.objects.filter(Add2Plateid=pk)
return render(request, 'tablet/show_plates.html', {'newplate': newplate,'add2plate': add2plate})
谢谢
【问题讨论】: