怎么用django里的Category模型?有代码。

cndeer 2012-05-26 02:00:16
models.py
# -*- 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



这是views.py
# -*- 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}
)



这是urls.py


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



这是模板文件:
templates/info.html

{% 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 %}


templates/index.html

{% 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 %}


templates/cate.html

{% 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 %}



问题是:

我打开首页的时候那个栏目分类可以调用出来。但是里面的URL地址没有?这里面哪里有错?

我打开分类的时候比如:http://127.0.0.1:8000/cate/1 http://127.0.0.1:8000/cate/2这里面什么也调不出来。


这个是我自已写的。也没有什么教程。

我得是views 里的cate 函数写的有问题。
再一个是模板里的调用也有问题。

请问怎么改上面的代码。

让首页的栏目分类可以正常链接到分类目录里。
并在分类目录里可以正常显示当前目当里的所有的文章?
...全文
46 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧