我們接下來會建立以下幾個頁面
昨天我們做了共享任務的表單,今天來做任務留言的功能。
path('task/comment/<slug:id>',comment_task),
comment_task
視圖@login_required
def comment_task(request, id):
user = request.user
tasks = Task.objects.filter(user_id=user)
# 取得共享的任務
sharedTasks = SharedTask.objects.filter(Q(user=user) | Q(task__user_id=user))
# 排除共享任務後的我的任務
my_tasks = tasks.exclude(id__in=sharedTasks.values_list('task__id', flat=True))
task = Task.objects.get(id=id)
previous_url = request.META.get('HTTP_REFERER')
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.cleaned_data['comment']
try:
comment_id = Comment.objects.latest('id').id + 1
except:
comment_id = 0
Comment.objects.create(id=comment_id, task=task, user=user, content=comment)
return redirect('task') # 重定向到任務列表頁面
else:
form = CommentForm()
action = "留言"
return render(request, 'edit_task.html', locals())
CommentForm
表單class CommentForm(forms.Form):
comment = forms.CharField(label='描述', widget=forms.Textarea(attrs={'name': 'body'}))
無留言
留言表單
有留言