Django copy object: func with self.id in html, urls and views
I try copy my object. I can receive copy from my "def checklist_copy" if i explicitly specify pk.(example)
But, i try work with 'self.pk' and this is not work. I want to copy the object under which I click on the button
i used different variations: (pk=pk) in function,
checklist.id and checklist.pk in html template,
tried passing "self" to function and doing self.pk
doing pk=int(Checklist.object.get.id()) to then transfer this "pk" to (pk=pk) in object.id()
try include func in class DetailView
did not achieve a result
Please help. I don't know where and what to look for
Example:
models.py
from django.urls import reverse
from django.db import models
class Checklist(models.Model):
title = models.CharField(max_length=250)
def get_absolute_url(self):
return reverse ('checklist:checklist', kwargs={'id': self.id, 'title': self.title})
def __str__(self):
return self.title
view.py
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Checklist
from django.shortcuts import redirect
from django.urls import reverse_lazy
class CheckListView(ListView):
model = Checklist
context_object_name = 'checklists'
class CheckListDetail(DetailView):
model = Checklist
context_object_name = 'checkList'
template_name = 'base/checklist.html'
def checklist_copy(request):
dest = Checklist.objects.get(pk=2) #here i need to use self.id(pk), but can't include 'self' in args of func
dest.pk = None
dest.save()
return redirect(reverse_lazy('checklists'))
urls.py (my app)
from django.urls import path
from .views import CheckListView, CheckListDetail
from . import views
urlpatterns = [
path('', CheckListView.as_view(), name='checklists'),
path('checklist/<int:pk>/', CheckListDetail.as_view(), name='checklist'),
path('checklist-copy/', views.checklist_copy, name='checklist_copy'),
]
checklist_list.html
<div id="tasklist" class="task-items-wrapper">
{% for checklist in checklists %}
<div class="task-wrapper" data-position="{{checklist.pk}}">
<div class="task-title">
<a href="{% url 'checklist' checklist.pk %}">{{checklist}}</a>
</div>
</div>
<a class="task-wrapper" href="{% url 'checklist_copy' %}">Copy</a>
{% endfor %}