单击按钮不更新Django的视图

时间:2020-07-06 00:30:01

标签: django function methods onclick

我有一个包含任务列表的视图,并试图在单击时进行简单的任务分配,但是它不起作用。你能支持吗?我在控制台中看到了这样的内容: “ POST /任务/分配HTTP / 1.1” 302 0。 谢谢。

L

型号:

class Task(models.Model):
name = models.CharField(max_length=248)
note = models.TextField(blank=True, null=True)
priority = models.NullBooleanField(blank=True, null=True)
status = models.IntegerField(choices=TASK_STATUS, default=TASK_STATUS[0][0])
placed_date = models.DateField(auto_now=True)
due_date = models.DateTimeField(blank=True, null=True)
completed = models.BooleanField(default=False)
completed_date = models.DateField(blank=True, null=True)
created_by = models.ForeignKey(
    User,
    null=True,
    blank=True,
    related_name="hana_created_by",
    on_delete=models.CASCADE)
assigned_to = models.ForeignKey(User,
    null=True,
    blank=True,
    related_name="hana_assigned_to",
    on_delete=models.CASCADE)

网址:

 path('excel_table', ex_views.ExcelTableView.as_view(), name = "excel-table"),
path("task/add", ex_views.TaskAddView.as_view(), name="task-add"),
path("task/<int:task_id>/", ex_views.TaskDetailView.as_view(), name="task-detail"),
path("task/<int:pk>/edit", ex_views.TaskEditView.as_view(), name="task-update"),
path("task/allocate", views.task_allocator, name="task-allocate"),

观看次数:

class TaskAllocateView(View):
def post(self, request):
    if request.POST.get("task_allocate") is not None:
        tasks = Task.objects.filter(assigned_to=None)
        for task in tasks:
            task.assigned_to = random.choice(User.objects.all())
            task.status = 1
            task.save()
            current_site = get_current_site(request)
            subject = render_to_string('email/assigned_subject.txt', {"task": task})
            body = render_to_string('email/assigned_body.txt', {
                'task': task,
                'site': current_site,
            })
            to_email = task.assigned_to.email
            email = EmailMessage(subject, body, to=[to_email])
            email.send()
        if tasks:
            messages.success(request, "Tasks succesfully allocated to your employees. Check status!")
            messages.success(request, ('Notification email has been sent to assignees!'))
        else:
            messages.warning(request, "All tasks already allocated!")
            messages.warning(request, ('Notification email already sent!'))

        return redirect(reverse("excel-table"))
    return redirect(reverse("excel-table"))

模板:

<form method="POST" action="{% url "task-allocate" %}" role="form" class="d-inline">
        {% csrf_token %}
        <div style="display:inline;">
          <button class="btn btn-info btn-sm" type="submit" name="task_allocate">
           Task Allocate
          </button>
        </div>
      </form>

1 个答案:

答案 0 :(得分:0)

if t.assigned_to != None:
    t.status = 1
    t.save()
    messages.success(request, "Tasks succesfully allocated to your employees. Check status!")
    return redirect(reverse("excel-table"))

此if永远不会执行,因为created_by在保存之前将保持为Non。

t.assigned_to = random.choice(User.objects.all())
    if t.assigned_to != None:

您以为这个t.assigned_to不再是None了,它仍然保持None是因为您没有保存。

我希望这一点对您有帮助。