创建一个基于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):4. 修改demo下settings.py文件:a) INSTALLED_APPS 中添加 'blog',例如:
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
INSTALLED_APPS = (b) TEMPLATE_DIRS 中添加工程所在路径:
'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',
)
TEMPLATE_DIRS = (5. 建立数据库和表:注意,首先需要检查settings.py文件中关于数据库的设置是否正确。python manage.py syncdb6. 新建view页面:article_list.html
# 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"
)
{% 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
相关文章推荐
- 第五章 :创建一个简单的基于Table View的应用程序
- 用Python脚本语言建立一个基于应用程序的GUI快速启动
- 在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序
- Qt使用教程之创建一个基于Qt部件的应用程序(二)
- 在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序
- Django(博客系统):基于pycharm如何一个django工程下创建多个app
- 在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序
- 在 Windows Azure 网站上使用 Django、Python 和 MySQL:创建博客应用程序
- s60 python 编程指南—— 如何创建一个应用程序
- python django 第一章 创建一个blog应用
- Kivy A to Z -- 如何从Python创建一个基于Binder的Service及如何从Java访问Python创建的Service
- python使用Pycharm创建一个Django项目
- 基于Django1.11和Python3开发一个简单的投票系统
- Python web入门:Django学习与实践二(简单页面实现和创建一个模板)
- Python的Django框架完成一个完整的论坛(3.创建app的models.py、views.py源码)
- Qt使用教程之创建一个基于Qt部件的应用程序(一)
- pythonWeb -- Django开发-创建一个Django项目
- 基于Python的标准库tkinter创建面向对象的GUI应用程序
- Qt使用教程之创建一个基于Qt部件的应用程序(三)