3
I’m trying to create a simple to-do list, but I’m having some problems. I want to create a task, within it I want to create some activities. That’s the problem I can’t do.
How can I create an activity using a CreateView
, but I would be within a task that was already created with CreateView
. Now, in this other one, I need to pass the pk
of the task, creating an activity linked to this and, once created, how can I use the reverse
to return to the detailview
of this task?
models:
from django.db import models
from django.core.urlresolvers import reverse
class List(models.Model):
title = models.CharField(blank=True, max_length=100)
class Meta:
ordering = ['id']
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('task-detail', kwargs={'pk': self.pk})
PRIORITY_CHOICES = (
(1, 'Low'),
(2, 'Normal'),
(3, 'High')
)
class Item(models.Model):
title = models.CharField(blank=True, max_length=100)
created = models.DateTimeField(auto_now=False, auto_now_add=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
priority = models.IntegerField(choices=PRIORITY_CHOICES, default=2)
completed = models.BooleanField(default=False)
todo_list = models.ForeignKey(List)
class Meta:
ordering = ['-priority', 'title']
views:
class TaskDetailView(DetailView):
model = List
class TaskCreateView(CreateView):
model = List
fields = ['title']
success_url = '/home'
class TaskUpdateView(UpdateView):
model = List
fields = ['title']
template_name = 'core/update.html'
success_url = '/home'
class TaskDeleteView(DeleteView):
model = List
success_url = reverse_lazy('home')
class ActivityCreateView(CreateView):
model = Item
fields = 'title', 'priority', 'completed'
success_url = '/home'
class ActivityUpdateView(UpdateView):
model = Item
fields = 'title', 'priority', 'completed'
template_name = 'core/update_actvity.html'
success_url = '/home'
class ActivityDeleteView(DeleteView):
model = Item
success_url = reverse_lazy('home')
url:
from django.conf.urls import url, include
from django.contrib import admin
from core.views import ActivityCreateView, ActivityDeleteView, ActivityUpdateView
from core.views import TaskDetailView, TaskCreateView, TaskUpdateView, TaskDeleteView, Home, MyView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('allauth.urls')),
url(r'^$', Home.as_view(), name="index" ),
url(r'^home/$', MyView.as_view(), name="home" ),
url(r'^task/(?P<pk>[0-9]+)/$', TaskDetailView.as_view(), name='task-detail'),
url(r'^task/(?P<pk>[0-9]+)/update/$', TaskUpdateView.as_view(), name='task-update'),
url(r'^task/(?P<pk>[0-9]+)/delete/$', TaskDeleteView.as_view(), name='task-delete'),
url(r'^task/add/$', TaskCreateView.as_view(), name='task-create'),
]
thank you so much for your help
– DiogoSilva