37,741
社区成员
发帖
与我相关
我的任务
分享# -*- coding:utf-8 -*-
from django.db import models
import datetime
#信息分类
class Category(models.Model):
name = models.CharField(max_length=18, unique=True)
sign_id = models.PositiveSmallIntegerField(unique=True)
def __unicode__(self):
return self.name
class Meta:
ordering = ['sign_id']
verbose_name_plural = '栏目'
#文章信息
class Archive(models.Model):
title = models.CharField(max_length=36)
content = models.TextField(max_length=1800)
category = models.ForeignKey(Category, null=True, blank=True)
pub_date = models.DateTimeField(auto_now_add=True)
last_update = models.DateTimeField(auto_now_add=True)
views_amount = models.IntegerField(default=0)
class Meta:
verbose_name = '文章'
verbose_name_plural = '文章列表'
def __unicode__(self):
return self.title
# -*- coding:utf-8 -*-
from django.shortcuts import render_to_response, get_object_or_404
from blog5.core.models import Archive, Category
from django.views.generic import list_detail, date_based
def index(request):
posts = Archive.objects.all()
return render_to_response('index.html', {'posts': posts})
def info(request, post_id):
post = get_object_or_404(Archive, id=post_id)
return list_detail.object_detail(
request,
template_object_name='post',
template_name='info.html',
object_id=post_id,
queryset=Archive.objects.all(),
)
def cate(request, category_id):
category = get_object_or_404(Category, id=category_id)
queryset=category.archive_set.filter()
return list_detail.object_list(
request,
template_object_name='posts',
template_name='cate.html',
queryset=queryset,
#object_id=category_id,
paginate_by=5,
#extra_context={u'分类'+category.name}
)
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from blog5.core.views import index
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'blog5.views.home', name='home'),
# url(r'^blog5/', include('blog5.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^list/(?P<post_id>\d+)/$', 'blog5.core.views.info', name='listurl'),
url(r'^$', index),
url(r'^cate/(?P<category_id>\d+)/$', 'blog5.core.views.cate'),
)
{% block title %} 高青信息首页 {% endblock %}
{% block content %}
<h2>{{ post.title }}</h2>
<p>发布时间:{{ post.pub_date|date:'Y-m-d H:i' }} 栏目分类:{{ post.category }}</p>
<p>{{ post.content }}</p>
{% endblock %}{% block title %} 高青信息首页 {% endblock %}
{% block content %}
{% for post in posts %}
<h2><a href='/list/{{post.id}}'>{{ post.title }}</a></h2>
<p>发布时间:{{ post.pub_date|date:'Y-m-d H:i' }} 栏目分类:<a href='/cate/{{category.id}}/'>{{ post.category }}</a></p>
<p>{{ post.content }}</p>
<p>分类:{% for category in category.archive_set.all %}<a href='#'>{{category}}</a>{% endfor %}</p>
{% endfor %}
{% endblock %}{% block title %} 高青信息首页 {% endblock %}
{% block content %}
{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>发布时间:{{ post.pub_date|date:'Y-m-d H:i' }} 栏目分类:{{ post.category }}</p>
<p>{{ post.content }}</p>
{% endfor %}
{% endblock %}