From 41dc37c8126f062257ce7d5fc8fcf84286b55a88 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Mon, 20 Sep 2010 16:10:09 -0500 Subject: Update news views to use slug instead of ID Signed-off-by: Dan McGee --- news/models.py | 2 +- news/views.py | 19 ++++++++++++------- templates/news/list.html | 4 ++-- templates/news/view.html | 4 ++-- urls.py | 14 ++++++++------ 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/news/models.py b/news/models.py index 88794cbb..6c8a7039 100644 --- a/news/models.py +++ b/news/models.py @@ -12,7 +12,7 @@ class News(models.Model): content = models.TextField() def get_absolute_url(self): - return '/news/%i/' % self.id + return '/news/%s/' % self.slug def __unicode__(self): return self.title diff --git a/news/views.py b/news/views.py index d7148d5b..7fa92c8a 100644 --- a/news/views.py +++ b/news/views.py @@ -1,7 +1,7 @@ from django import forms from django.contrib.auth.decorators import permission_required from django.http import HttpResponse -from django.shortcuts import redirect +from django.shortcuts import get_object_or_404, redirect from django.views.decorators.cache import never_cache from django.views.generic import list_detail, create_update from django.views.generic.simple import direct_to_template @@ -10,8 +10,13 @@ from .models import News -def view(request, newsid): - return list_detail.object_detail(request, News.objects.all(), newsid, +def view_redirect(request, object_id): + newsitem = get_object_or_404(News, pk=object_id) + return redirect(newsitem, permanent=True) + +def view(request, slug=None): + return list_detail.object_detail(request, News.objects.all(), + slug=slug, template_name="news/view.html", template_object_name='news') @@ -44,19 +49,19 @@ def add(request): @permission_required('news.delete_news') @never_cache -def delete(request, newsid): +def delete(request, slug): return create_update.delete_object(request, News, - object_id=newsid, + slug=slug, post_delete_redirect='/news/', template_name='news/delete.html', template_object_name='news') @permission_required('news.change_news') @never_cache -def edit(request, newsid): +def edit(request, slug): return create_update.update_object(request, - object_id=newsid, + slug=slug, form_class=NewsForm, template_name="news/add.html") diff --git a/templates/news/list.html b/templates/news/list.html index 14ba79b6..26e8c268 100644 --- a/templates/news/list.html +++ b/templates/news/list.html @@ -34,11 +34,11 @@

News Archives

{{ item.author.get_full_name }} {% if perms.main.change_news %} - Edit {% endif %} {% if perms.main.delete_news %} -   Delete {% endif %} diff --git a/templates/news/view.html b/templates/news/view.html index 1c92fe78..86a412de 100644 --- a/templates/news/view.html +++ b/templates/news/view.html @@ -9,9 +9,9 @@

News: {{ news.title }}

{% if perms.main.change_news %} {% endif %} diff --git a/urls.py b/urls.py index 3f394426..5ff57970 100644 --- a/urls.py +++ b/urls.py @@ -62,12 +62,14 @@ (r'^todo/$', 'todolists.views.list'), (r'^todolists/$', 'todolists.views.public_list'), - (r'^news/(\d+)/$', 'news.views.view'), - (r'^news/add/$', 'news.views.add'), - (r'^news/edit/(\d+)/$', 'news.views.edit'), - (r'^news/delete/(\d+)/$', 'news.views.delete'), - (r'^news/preview/$', 'news.views.preview'), - (r'^news/$', 'news.views.list', {}, 'news-list'), + (r'^news/add/$', 'news.views.add'), + (r'^news/preview/$', 'news.views.preview'), + # old news URLs, permanent redirect view so we don't break all links + (r'^news/(?P\d+)/$', 'news.views.view_redirect'), + (r'^news/(?P[-\w]+)/$', 'news.views.view'), + (r'^news/(?P[-\w]+)/edit/$', 'news.views.edit'), + (r'^news/(?P[-\w]+)/delete/$', 'news.views.delete'), + (r'^news/$', 'news.views.list', {}, 'news-list'), (r'^mirrors/$', 'devel.views.mirrorlist', {}, 'mirrors-list'), -- cgit v1.2.3-54-g00ecf