Skip to content

Instantly share code, notes, and snippets.

@dead23angel
Created February 16, 2014 05:20
Show Gist options
  • Save dead23angel/9029696 to your computer and use it in GitHub Desktop.
Save dead23angel/9029696 to your computer and use it in GitHub Desktop.
# coding: utf8
from django.db import models
from tagging.fields import TagField
from cked.fields import RichTextField
class Post(models.Model):
title = models.CharField(max_length=255, unique=True)
slug = models.SlugField(max_length=255, unique=True)
datetime = models.DateTimeField('Дата публикации')
content = RichTextField('Контент')
category = models.ForeignKey('blog.Category')
tags = TagField('Теги')
class Meta:
db_table = "news"
verbose_name = 'Новость'
verbose_name_plural = 'Новости'
def __unicode__(self):
return '%s' % self.title
class Category(models.Model):
title = models.CharField(max_length=100, db_index=True)
slug = models.SlugField(max_length=100, db_index=True)
class Meta:
db_table = "categorys"
verbose_name = 'Категория'
verbose_name_plural = 'Категории'
def __unicode__(self):
return '%s' % self.title
#coding: utf-8
from django.conf.urls import url, patterns
import views
urlpatterns = patterns('',
url(r'^$', views.PostList.as_view(), name='blog-post-list'),
url(r'^view/(?P<slug>[^\.]+).html', views.PostDetail.as_view(), name='blog-post-detail'),
url(r'^category/(?P<slug>[^\.]+).html', views.CategoryList.as_view(), name='blog-category-list'),
url(r'^tag/(?P<tag>[^/]+)/$', views.TagPostList.as_view(), name='blog-posttag-list'),
)
# coding: utf8
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Post
class PostList(ListView):
queryset = Post.objects.all().order_by('-datetime')
template_name = "blog/post_list.html"
paginate_by = 5
context_object_name = "post_list"
class PostDetail(DetailView):
model = Post
slug_field = 'slug'
template_name = "blog/post_detail.html"
class CategoryList(ListView):
slug_field = 'slug'
template_name = "blog/post_list.html"
def get_queryset(self):
return Post.objects.filter(category__slug=self.kwargs['slug'])
class TagPostList(ListView):
template_name = "blog/post_list.html"
def get_queryset(self):
return Post.objects.filter(tags=self.kwargs['tag'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment