Я не уверен, как именно это сказать, но здесь идет. У меня есть несколько взглядов, к которым мой 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)
Что я здесь не так делаю? Кто-нибудь может помочь?