在windows中安装 django
1、在http://www.djangoproject.com/download中下载安装软件
2、解压下载后的文件,并将解压后的文件放置到(我这里的python放在C盘的C://python33)C://python33中
3、打开命令提示符,进入到c://python33//Django-1.6.2中,并在终端输入命令setup.py install安装django
4、测试django 在python的idle中运行 >>>import django >>>django.VERSION看能否出现对应的版本信息
5、项目的初始设置:在终端中,进入到C:\Python33\Lib\site-packages\django\bin中然后运行命令:django-admin.py startproject newsite 这样子在bin目录下就会出现一个新的文件newsite
6、建立开发服务器:在终端中,进入到newsite中,运行 manage.py runserver命令。 浏览器中测试127.0.0.1:8000
创建示例:
1、在newsite/newsite中创建myfirstview.py,并在该文件中输入代码
from django.http import HttpResponsedef sometext(request): mypage="Welcome to My First Page!
" return HttpResponse(mypage)
2、在url.py文件中添加from newsite.myfirstview import sometext还有在该文件的patterns中添加('^sometext/$', sometext),
则可以在浏览器中运行127.0.0.1:8000/sometext 即可出现对应的界面
使用模板系统:
测试该模板系统
from django.template import Template, Contextmyfirsttemplate = """Welcome to { {owner}}'s Library
"""a = Template(myfirsttemplate)b = Context({ 'owner':'James Payne'})print(a.render(b))
在交互中输入该命令,即可会出现打印的Welcome to James Payne's Library
这个就是模板,其中的{
{}}中的内容就是用来Context中对应的要替换的内容实践操作:
1、在newsite\newsite中创建文件myfirsttemplate.html并编辑内容
Welcome to { {owner}}'s Library
Below you will find a list of {
{owner}}'s favorite booksBook Title: { {books}} Author: { {author}}
2、在newsite\newsite中创建文件myfirstview.py并编辑内容
from django.shortcuts import render_to_responsedef sometext(request): return render_to_response('myfirsttemplate.html', { 'owner':'James Payne', 'books': 'American Gods', 'author': 'Neil Gaimen'})
3、在settings.py中添加
TEMPLATE_DIRS = ('C:/Python33/Lib/site-packages/django/bin/newsite/newsite')
这个是告诉django模板文件在newsite/newsite文件夹下
4、启动manage.py runserver,并在浏览器中输入127.0.0.1:8000/sometext即可出现对应的界面