From bb463f397419997698cdb814dd0bc841abd8d6f8 Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Sat, 7 Apr 2018 11:34:47 -0700 Subject: [PATCH] Auto-create slug on New TaskList --- todo/forms.py | 2 +- todo/templates/todo/add_list.html | 6 ------ todo/views.py | 7 +++++-- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/todo/forms.py b/todo/forms.py index 3aea30b..0986aa7 100644 --- a/todo/forms.py +++ b/todo/forms.py @@ -17,7 +17,7 @@ class AddTaskListForm(ModelForm): class Meta: model = TaskList - exclude = ['created_date', ] + exclude = ['created_date', 'slug', ] class AddEditTaskForm(ModelForm): diff --git a/todo/templates/todo/add_list.html b/todo/templates/todo/add_list.html index 3eaaa46..610d918 100644 --- a/todo/templates/todo/add_list.html +++ b/todo/templates/todo/add_list.html @@ -13,12 +13,6 @@ The full display name for this list. -
- - - - To be used in URL for this list e.g. 'ux-tasks'. All lowercase, no spaces. -
{{form.group}} diff --git a/todo/views.py b/todo/views.py index 4d5c3cb..069446b 100644 --- a/todo/views.py +++ b/todo/views.py @@ -12,8 +12,9 @@ from django.db.models import Q from django.http import HttpResponse from django.shortcuts import get_object_or_404, render, redirect from django.template.loader import render_to_string -from django.views.decorators.csrf import csrf_exempt from django.utils import timezone +from django.utils.text import slugify +from django.views.decorators.csrf import csrf_exempt from todo.forms import AddTaskListForm, AddEditTaskForm, AddExternalTaskForm, SearchForm from todo.models import Task, TaskList, Comment @@ -280,7 +281,9 @@ def add_list(request) -> HttpResponse: form = AddTaskListForm(request.user, request.POST) if form.is_valid(): try: - form.save() + newlist = form.save(commit=False) + newlist.slug = slugify(newlist.name) + newlist.save() messages.success(request, "A new list has been added.") return redirect('todo:lists')