首页 > 程序开发 > Web开发 > Python >

Django项目6

2013-01-07

准备工作[plain] django-admin.py startproject web05 django-admin.py startapp blog blog注册到settings.py中 python manage.py runserver blog下建立templates,并建立两个模板文件 配置url[pytho...

准备工作
[plain]
django-admin.py startproject web05
django-admin.py startapp blog
blog注册到settings.py中
python manage.py runserver
blog下建立templates,并建立两个模板文件
配置url
[python]
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^blog/index/$','blog.views.index'),
url(r'^blog/show/$','blog.views.show'),
)
配置views
[python]
#coding:utf8
from django.http import HttpResponse
from django.template import Template,Context,loader
from django.shortcuts import render_to_response #先导入这个牛逼的东西
def index(req):
t = Template(&#39;<h1>name is:{{uname}}</h1>&#39;)
c = Context()
c[&#39;uname&#39;]=&#39;tom&#39;
res = HttpResponse(t.render(c))
res.write(&#39;aaa&#39;) #response可追加,用write方法
return res
def show(req):
return render_to_response(&#39;show.html&#39;,{&#39;name&#39;:&#39;dragon&#39;})#一行搞定别人五行
配置html
[html]
<!-- base.html是模板文件 这里面就一个测试语句 -->
<h1>test</h1>
[html]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<b>welcom {{name}}</b>
<div>
{%include &#39;base.html&#39;%}
</div>
</body>
</html>
相关文章
最新文章
热点推荐