本系统使用的Django版本是1.1,数据库是Sqlite3。
首先创建一个project:
django-admin startproject webblog
然后进入webblog目录,修改settings.py:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'webblog.db'
DATABASE_NAME = 'webblog.db'
使用下面的命令创建一个app:
python manage.py startapp blog
进入blog目录,修改models.py:
修改view.py:
返回去修改setting.py,把新创建的app和模板加进去:
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"d:/webblog/",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'webblog.blog',
'django.contrib.admin',
)
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"d:/webblog/",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'webblog.blog',
'django.contrib.admin',
)
在weblog目录下,执行:
python manage.py syncdb
在webblog目录下创建htmls目录,进入该目录,创建index.html:
{% extends "blogbase.html" %}
{% block title %}Articles{% endblock %}
{% block content %}
< h1 >Articles </h1>
{% for article in article_list %}
< p >Headline : {{ article.headline }} </p>
< p >By {{ article.reporter.full_name }} </p>
< p >Published {{ article.pub_date|date:"F j, Y" }} </p>
{% endfor %}
{% endblock %}
{% block title %}Articles{% endblock %}
{% block content %}
< h1 >Articles </h1>
{% for article in article_list %}
< p >Headline : {{ article.headline }} </p>
< p >By {{ article.reporter.full_name }} </p>
< p >Published {{ article.pub_date|date:"F j, Y" }} </p>
{% endfor %}
{% endblock %}
创建blogbase.html:
<
html
>
< head >
< title >{% block title %}{% endblock %} </title>
</head>
< body >
{% block content %}{% endblock %}
</body>
</html>
< head >
< title >{% block title %}{% endblock %} </title>
</head>
< body >
{% block content %}{% endblock %}
</body>
</html>
修改url.py,将访问地址加入进去:
现在,整个博客系统已经配置完毕,我们可以使用
python manage.py runserver
来启动服务器了,在地址栏里输入:
http://localhost:8000/blog
就可以访问博客了~
当然,一开始进去的时候什么资料都没有,我们需要在管理界面里输入一些数据来显示,地址为
http://localhost:8000/admin/
用户名和密码为你在syncdb时设置的,进去后加入一条记录即可~~~
为了admin管理,需要在blog下面添加admin.py