如何通过单击按钮更新DetailView中的“模型”字段

时间:2019-08-23 18:31:04

标签: python django django-models django-templates django-class-based-views

我创建了一个维护应用程序,该应用程序允许用户创建和查看维护详细信息。我有一个页面“ maintenance-details.html”,其中显示了特定维护的所有详细信息。

这是我的 views.py

class MaintenanceDetailView(DetailView):
    template_name = 'maintenance/maintenance-details.html'
    model = Maintenance

    def get_context_data(self, **kwargs):
        contacts_suppliers = ContactsSupplier.objects.filter(supplier__maintenance=self.object)
        hora_atual = datetime.datetime.now()
        context = super().get_context_data(**kwargs)
        context['contacts_supplier'] = contacts_suppliers
        context['hora_atual'] = hora_atual
        return context

我在模板上创建了一个名为“标记为完成”的按钮。我的维护模型具有一个BooleandField“ done”,目的是将任务设置为已完成或未完成。我正在寻找的是更新模型并将用户单击时将“完成”设置为“真”的最佳方法。

我的 models.py 此处:

class Maintenance(models.Model):
    category = models.ForeignKey(SuppliersCategory, models.DO_NOTHING, db_column='Category')  # Field name made lowercase.
    property = models.ForeignKey(Property, models.DO_NOTHING, db_column='Property_Reference')  # Field name made lowercase.
    name = models.CharField(db_column='Name', max_length=25)  # Field name made lowercase.
    created_date = models.DateTimeField(db_column='Date', auto_now_add=True)  # Field name made lowercase.
    staffmember = models.CharField(db_column='StaffMember', max_length=25, blank=True, null=True)  # Field name made lowercase.
    supplier = models.ForeignKey(Suppliers, db_column='Supplier')  # Field name made lowercase.
    description = models.CharField(db_column='Description', max_length=500, blank=True, null=True)  # Field name made lowercase.
    photo = models.ImageField(upload_to='maintenace/', db_column='Photo', blank=True, null=True)  # Field name made lowercase.
    expirydate = models.DateTimeField(db_column='ExpiryDate', blank=False)  # Field name made lowercase.
    datecompletion = models.DateTimeField(db_column='DateCompletion', blank=True, null=True)  # Field name made lowercase.
    done = models.BooleanField(db_column='Done', default=False)  # Field name made lowercase.

    class Meta:
        db_table = 'Maintenance'

    def get_absolute_url(self):
        return reverse("maintenance:maintenance_detail",kwargs={'pk':self.pk})

    def set_done(self):
        self.done = True
        self.datecompletion = timezone.now()
        self.save()

正确的方法是什么?

2 个答案:

答案 0 :(得分:0)

假设您的模板中有一个带有按钮的表单,如下所示:

<form method="post" action="{% url 'done' object.pk %}">
    <button type="submit">Done</button>
</form>

然后,您需要urls.py来包含名为“ done”的网址

path('/done/<int:pk>', views.done, name="done")

和您的视图文件中

def done (request, pk):
    maintenance = Maintenance.objects.get(pk=pk)
    maintenance.set_done()
    return redirect ('url to your detail view')

答案 1 :(得分:0)

您仍然可以在DetailView中使用post方法。
假设这是您的模板:

<form method="POST">
    <button type="submit" name="set_done">Done</button>
</form>

然后在您的DetailView中:

def post(self, request, *args, **kwargs):
    if "set_done" in request.POST:
        self.object.set_done()
        return HttpResponseRedirect(self.request.path_info)

如果您不想在用户单击按钮后重新加载页面,请在模板中使用ajax而不是常规格式。