Auto-create slug on New TaskList

This commit is contained in:
Scot Hacker 2018-04-07 11:34:47 -07:00
parent 4e8960c12f
commit bb463f3974
3 changed files with 6 additions and 9 deletions

View file

@ -17,7 +17,7 @@ class AddTaskListForm(ModelForm):
class Meta:
model = TaskList
exclude = ['created_date', ]
exclude = ['created_date', 'slug', ]
class AddEditTaskForm(ModelForm):

View file

@ -13,12 +13,6 @@
<input type="text" class="form-control" id="id_name" name="name" aria-describedby="inputNameHelp" placeholder="">
<small id="inputNameHelp" class="form-text text-muted">The full display name for this list.</small>
</div>
<div class="form-group">
<label for="id_slug">Slug</label>
<input type="text" class="form-control" id="id_slug" name="slug" aria-describedby="inputSlugHelp" placeholder="">
<small id="inputSlugHelp" class="form-text text-muted">
To be used in URL for this list e.g. 'ux-tasks'. All lowercase, no spaces.</small>
</div>
<div class="form-group">
<label for="id_group">Group</label>
{{form.group}}

View file

@ -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')