当前位置: 动力学知识库 > 问答 > 编程问答 >

URLS and views in DJANGO

问题描述:

I'm not sure exactly how to word this, but here goes. I have several views that my urls.py doesn't seem to get to at all, and I don't know why. I'm new to django and admit to being a bit perplexed as to how some of it works.

In my template I have:

<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>

In my 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),

In my 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)

What am I doing wrong here? Can anyone help?

网友答案:

There are several issues here,

First, unless you have modified the related settings, it is best to add a / to the end of your url's, especially since you are including $ in your URL regexps, and django may redirect to add a final slash to your address.

Second, your deleteTicket function, is taking request and ticket_id as arguments, while it needs 3 according to your regexp, project_slug is missing.

3rd, <a href="/project/{{ project.slug }}/tickets/delete">Delete Ticket</a> simply won't match (r'^(?P<project_slug>[^\.^/]+)/tickets/(?P<ticket_id>\d+)$', views.deleteTicket),, since there is no sign of a ticket_id.

4th (and unrelated to the matching issue), you are deleting an object through an HTTP GET request which may not be a good idea (e.g, it may be cached or search engines may try to hit it). Also calling Ticket.objects.get(pk=ticket_id) might raise an exception, try get_object_or_404(Ticket, pk=ticket_id) which is more likely to do what you want. It is in from django.shortcuts import get_object_or_404.

If this doesn't work out, try viewing the HTML source of that shows up in the template (with values placed instead of {{project.slug}}) so further inspection can be carried out.

Hope this helps you out.

分享给朋友:
您可能感兴趣的文章:
随机阅读: