您的位置:首页 > 编程语言 > Python开发

创建一个基于Python的Django应用程序

2012-02-28 12:56 369 查看
安装Django:下载:https://www.djangoproject.com/download/解压后运行:
python setup.py
install
参考资料:http://www.cnblogs.com/icyfire/archive/2011/10/10/writing_your_first_django_app.html1. 创建demo工程python django-admin.py startproject demo2. 创建apppython manage.py blog3. 新建model在/demo/blog下修改models.py文件如下:
from 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:
passclass 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
4. 修改demo下settings.py文件:a) INSTALLED_APPS 中添加 'blog',例如:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs','blog',
)
b) TEMPLATE_DIRS 中添加工程所在路径:
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."C:/Users/shenbin/My Documents/Aptana Studio 3 Workspace/demo/demo"
)
5. 建立数据库和表:注意,首先需要检查settings.py文件中关于数据库的设置是否正确。python manage.py syncdb6. 新建view页面: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, No Article here.
</p>
{% endif %}
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 at: {{ object.category.name }}
</div>
</div>
<p>
<a href="/admin/blog/article/{{ object.id }}">Edit</a>
</p>
<p>
<a href="/blog">BACK</a>
</p>
7. 修改urls.py文件如下:from django.conf.urls.defaults import patterns, include, url# Uncomment the next two lines to enable the admin:#from django.contrib import admin#admin.autodiscover()from demo.blog.models import Article#admin.site.register(Article)#from demo.blog.models import Category#admin.site.register(Category)show_list = {'queryset' : Article.objects.all(),}urlpatterns = patterns('',# Examples:# url(r'^$', 'demo.views.home', name='home'),# url(r'^demo/', include('demo.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'^blog/', 'django.views.generic.list_detail.object_list', show_list),url(r'^blog/(?P<id>\d+)/$', 'django.views.generic.list_detail.object_detail', show_list),)启动django:python manage.py runserver 0.0.0.0:8000访问: http://localhost:8000/blog 即可。其他资料:http://www.tingcheba.com/http://simple-is-better.com/news/275
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: