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 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'news') diff --git a/news/models.py b/news/models.py index 88794cb..6c8a703 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 d7148d5..7fa92c8 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 @@ import markdown 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") -- cgit v1.2.3-24-g4f1b