我不确定如何说出来,但是这里有。我有几个看法,我的urls.py似乎根本没有,我不知道为什么。我是django的新手,并承认对其中的一些工作有点困惑。
在我的模板中,我有:
<ul>
<li><a href="/project/{{project.slug}}/tickets/create">Create Ticket</a></li>
<li><a href="/project/{{ project.slug }}/tickets/edit">Edit Ticket</a></li>
<li><a href="/project/{{ project.slug }}/tickets/delete">Delete Ticket</a></li>
</ul>
在我的urls.py中:
(r'^ticket/(?P<ticket_id>\d+)$', views.editTicket),
(r'^(?P<project_slug>[^\.^/]+)/tickets/create$', views.createTicket),
(r'^(?P<project_slug>[^\.^/]+)/tickets/(?P<ticket_id>\d+)$', views.deleteTicket),
在我的views.py中:
def createTicket(request, project_slug):
project = Project.objects.get(slug=project_slug)
form = None
if request.method == 'POST':
t = Ticket(created_by=request.user, assigned_to=request.user)
form = TicketCreateForm(request.POST, instance=t)
if form.is_valid():
comment = Comment(user=request.user, body=request.POST.get("description", ""), subject="")
comment.save()
t.description = comment
form.save()
return HttpResponseRedirect("/project/ticket/%d" % t.pk)
else:
form = TicketCreateForm(instance=Ticket(project=project, assigned_to=request.user))
payload = { 'project':project, 'form':form }
return render(request, 'project/ticket_add.html', payload)
def editTicket(request, ticket_id):
ticket = Ticket.objects.get(pk=ticket_id)
if request.method == 'POST':
form = TicketCreateForm(request.POST, instance=ticket)
if form.is_valid():
form.setComment(Comment(parent=ticket.description, user=request.user, body=request.POST.get("comment", " ")))
form.save()
form = TicketCreateForm(instance=ticket)
payload = { 'project':ticket.project, 'ticket':ticket, 'ticket_form':form }
return render(request, 'project/ticket.html', payload)
def deleteTicket(request, ticket_id):
ticket = Ticket.objects.get(pk=ticket_id)
ticket.delete()
redirect_to = 'project/tickets.html'
return HttpResponseRedirect(redirect_to)
def editTicket(request, ticket_id):
ticket = Ticket.objects.get(pk=ticket_id)
if request.method == 'POST':
form = TicketCreateForm(request.POST, instance=ticket)
if form.is_valid():
form.setComment(Comment(parent=ticket.description, user=request.user, body=request.POST.get("comment", " ")))
form.save()
form = TicketCreateForm(instance=ticket)
payload = { 'project':ticket.project, 'ticket':ticket, 'ticket_form':form }
return render(request, 'project/ticket.html', payload)
我在这里做错了什么?有人可以帮忙吗?
答案 0 :(得分:1)
这里有几个问题,
首先,除非您修改了相关设置,否则最好在网址的末尾添加/,特别是因为您在网址中包含了$ regexp,而django可能会重定向以添加最终结果削减你的地址。
其次,您的deleteTicket
函数正在使用request
和ticket_id
作为参数,而根据您的正则表达式需要3,project_slug
缺失。
第3,<a href="/project/{{ project.slug }}/tickets/delete">Delete Ticket</a>
根本没有匹配(r'^(?P<project_slug>[^\.^/]+)/tickets/(?P<ticket_id>\d+)$', views.deleteTicket),
,因为没有ticket_id
的迹象。
第4(与匹配问题无关),您正在通过HTTP GET请求删除对象,这可能不是一个好主意(例如,它可能被缓存或搜索引擎可能尝试命中它)。同时调用Ticket.objects.get(pk=ticket_id)
可能会引发异常,请尝试get_object_or_404(Ticket, pk=ticket_id)
,这更有可能实现您想要的效果。它位于from django.shortcuts import get_object_or_404
。
如果这样做不成功,请尝试查看模板中显示的HTML源代码(放置的值代替{{project.slug}}
),以便进行进一步的检查。
希望这可以帮助你。