我有一个项目表,可以根据当前驻留在用户数据库中的内容进行打印。我的意图是,当我循环查询,并将它们打印到页面上时,有一个删除按钮。请记住,用户已登录以查看这些内容。 我只是在这里测试了代码,但是得到了一堆错误。我希望链接传递主键的参数,以便可以删除它。
def deleter(request, id, username):
if username == request.user.username:
user = User.objects.get(username=username)
ti = TaskItem.objects.get(pk = id)
ti.delete()
return HttpResponse('deleted')
return HttpResponse('not deleted')
class TaskItem(models.Model):
taskn = models.CharField(max_length = 400)
usern = models.ForeignKey(User)
#Created field will add a time-stamp to sort the tasks from recently added to oldest
created_date = models.DateTimeField('date created', default=timezone.now, editable = False)
created_time = models.TimeField('time created', auto_now_add = True, editable = False)
def __unicode__(self):
return self.taskn + " at " + str(self.created_time)
url(r'^user/(\w+)/delete/(\d+)/$', deleter),
url(r'^user/(\w+)/submitted/$', task_submission, name='submitted')
答案 0 :(得分:0)
def deleter(request, id, username)
参数的顺序对我来说有所不同。
模板中的链接创建是:
<td><a href="/user/{{ user.username }}/delete/{{ task.pk }}">Remove</a></td>