无法在Django中创建删除查询的链接

时间:2015-05-19 03:48:42

标签: django

我有一个项目表,可以根据当前驻留在用户数据库中的内容进行打印。我的意图是,当我循环查询,并将它们打印到页面上时,有一个删除按钮。请记住,用户已登录以查看这些内容。 我只是在这里测试了代码,但是得到了一堆错误。我希望链接传递主键的参数,以便可以删除它。

浏览

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

url(r'^user/(\w+)/delete/(\d+)/$', deleter),
url(r'^user/(\w+)/submitted/$', task_submission, name='submitted')

1 个答案:

答案 0 :(得分:0)

def deleter(request, id, username)

参数的顺序对我来说有所不同。

模板中的链接创建是:

<td><a href="/user/{{ user.username }}/delete/{{ task.pk }}">Remove</a></td>