使用django开发最简单博客程序
时间:2008-09-27 来源:riverbird2005
1).下载django,我是在mandriva平台上试验的,可以通过urpmi python-django来>进行下载安装,不过版本有些老,本例使用tar.gz包进行安装。
2).实验平台: mandriva 2008 + sqlite3 + django1.0
3).安装:下载后,运行python setup.py install进行安装,不过,说是/usr/lib/python2.5/site-package/config文件未找到,最后我用了变通的办法,将解压的Django1.0下面的django目录复制至/usr/lib/python2.5/site-package/下面,然后,建立django-admin.py至/usr/local/bin的链接,然后使该程序可执行。
4).建立project:django-admin.py startproject demo
5).修改demo/settings.py文件中的数据库链接项:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'demo.db'
...
INSTALLED_APPS = {
'django.contrib.admin',
...
'demo.blog',
}
6).编辑demo/blog/models.py:
fromm django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=32)
def __unicode__(self):
return self.name
class Admin:
pass
class Article(models.Model):
title = models.CharField(max_length=64)
published_at = models.DateTimeField('date published')
content = models.TextField()
category = models.ForeignKey(Category)
def __unicode__(self):
return self.title
class Admin:
pass
7).根据models模块,建立数据库及表格:
python manage.py sql blog
8).同步数据库:
python manage.py syncdb
9).修改demo/urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^demo/', include('demo.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/(.*)', admin.site.root),
(r'^blog/', include('demo.blog.urls')),
)
10).在demo/blog下建立urls.py,加入:
from django.conf.urls.defaults import *
from demo.blog.models import Article
info_dict = {'queryset': Article.objects.all(),}
urlpatterns = patterns('',
(r'^$', 'django.views.generic.list_detail.object_list', info_dict),
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
)
11).在demo/blog/下建立admin.py,加入:
from demo.blog.models import Article
from demo.blog.models import Category
from django.contrib import admin
admin.site.register(Category)
admin.site.register(Article)
12).启动服务器进入后台:
>>python manage.py runserver
>>登入地址: http://localhost:8000/admin
>>在里面可以看到article和category,进入article可以写新文章了。
13).在/usr/lib/python2.5/site-packages/django/contrib/admin/templates/blog建立article_list.html
{% if object_list %}
{% for article in object_list %}
<div class="article">
<div class="title"><a href="/blog/{{ article.id }}">{{ article.title }}</a></div>
</div>
{% endfor %}
{% else %}
<p>sorry!</p>
{% endif %}
14).在同上目录下建立article_detail.html文件:
<div class="article">
<div class="title">title: {{ object.title }}</div>
<div class="pub_date">{{ object.published_at }}</div>
<div class="content">{{ object.content }}</div>
<div class="category">published: {{ object.category.name }}</div>
</div>
<p><a href="/admin/blog/article/{{ object.id }}">Modify</a></p>
<p><a href="/blog">BACK</a></p>
15)浏览blog: http://localhost:8000/blog/
16).在测试中途中出现的问题:
>>demo/urls.py文件中提示出错;
>>demo/blog/admin.py文件需要建立,不然的话显示不出来article;
>>article_list.html及article_detail.html要加入至上面提到的目录,否则的话
如果放至demo/blog目录下面会提示找不到,不过应该有办法配 置的,只是尚未找到。
17).简单说到这些吧,现在最简单的blog系统就搞定了,参考了http://blog.ericsk.org/archives/815
18).django之前老早看过,不过今天才有一种顿悟的感觉。
2).实验平台: mandriva 2008 + sqlite3 + django1.0
3).安装:下载后,运行python setup.py install进行安装,不过,说是/usr/lib/python2.5/site-package/config文件未找到,最后我用了变通的办法,将解压的Django1.0下面的django目录复制至/usr/lib/python2.5/site-package/下面,然后,建立django-admin.py至/usr/local/bin的链接,然后使该程序可执行。
4).建立project:django-admin.py startproject demo
5).修改demo/settings.py文件中的数据库链接项:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'demo.db'
...
INSTALLED_APPS = {
'django.contrib.admin',
...
'demo.blog',
}
6).编辑demo/blog/models.py:
fromm django.db import models
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=32)
def __unicode__(self):
return self.name
class Admin:
pass
class Article(models.Model):
title = models.CharField(max_length=64)
published_at = models.DateTimeField('date published')
content = models.TextField()
category = models.ForeignKey(Category)
def __unicode__(self):
return self.title
class Admin:
pass
7).根据models模块,建立数据库及表格:
python manage.py sql blog
8).同步数据库:
python manage.py syncdb
9).修改demo/urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^demo/', include('demo.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/(.*)', admin.site.root),
(r'^blog/', include('demo.blog.urls')),
)
10).在demo/blog下建立urls.py,加入:
from django.conf.urls.defaults import *
from demo.blog.models import Article
info_dict = {'queryset': Article.objects.all(),}
urlpatterns = patterns('',
(r'^$', 'django.views.generic.list_detail.object_list', info_dict),
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
)
11).在demo/blog/下建立admin.py,加入:
from demo.blog.models import Article
from demo.blog.models import Category
from django.contrib import admin
admin.site.register(Category)
admin.site.register(Article)
12).启动服务器进入后台:
>>python manage.py runserver
>>登入地址: http://localhost:8000/admin
>>在里面可以看到article和category,进入article可以写新文章了。
13).在/usr/lib/python2.5/site-packages/django/contrib/admin/templates/blog建立article_list.html
{% if object_list %}
{% for article in object_list %}
<div class="article">
<div class="title"><a href="/blog/{{ article.id }}">{{ article.title }}</a></div>
</div>
{% endfor %}
{% else %}
<p>sorry!</p>
{% endif %}
14).在同上目录下建立article_detail.html文件:
<div class="article">
<div class="title">title: {{ object.title }}</div>
<div class="pub_date">{{ object.published_at }}</div>
<div class="content">{{ object.content }}</div>
<div class="category">published: {{ object.category.name }}</div>
</div>
<p><a href="/admin/blog/article/{{ object.id }}">Modify</a></p>
<p><a href="/blog">BACK</a></p>
15)浏览blog: http://localhost:8000/blog/
16).在测试中途中出现的问题:
>>demo/urls.py文件中提示出错;
>>demo/blog/admin.py文件需要建立,不然的话显示不出来article;
>>article_list.html及article_detail.html要加入至上面提到的目录,否则的话
如果放至demo/blog目录下面会提示找不到,不过应该有办法配 置的,只是尚未找到。
17).简单说到这些吧,现在最简单的blog系统就搞定了,参考了http://blog.ericsk.org/archives/815
18).django之前老早看过,不过今天才有一种顿悟的感觉。
相关阅读 更多 +