第一课:视图显示

1   建立一个项目:django-admin startproject blog,

进入blog: cd blog

显示:blog(__init__.py settings.py urls.py ) manage.py

2,在当前目录,建立一个应用:django-admin startapp appblog

显示:appblog(__init__.py modules.py views.py tests.py) blog manage.py

3   配置应用vim blog/settings.py下的INSTALLED_APPS (‘appblog',)

编写响应vim blog/urls.py

url(r'^blog/index/$','blog.views.index'),

编写视图vim appblog/views.py:编写def文件,

from django.http import HttpResponse

def index(req):

return HttpResponse('<h1>hello world to Django!</h1>')

4启动项目:python manage.py runserver

第二课:模板映射

1 首先看结构图

1 首先建立外框,即项目:django-admin startproject mysite

然后建立中间框,即应用和模板

django-admin startapp myapp

mkdir templates

2 编写最内框,即各文件

vi templates/index.html

 <html>
<body>
<meta http-equiv="Content-Type" content="text/html" />
<title>mytile my index</title>
</head>
<body>
<h1>mybody my index</h1>
</body>
</html>
~

vi mysite/urls.py

 from django.conf.urls import patterns, include, url

 # Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover() urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mytest.views.home', name='home'),
# url(r'^mytest/', include('mytest.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'^myapp/',include('myapp.urls')),
)
~

vi myapp/urls.py

 from django.conf.urls import *

 urlpatterns = patterns('',
url('^index/$','myapp.views.index'),
)
~
~
~

vi myapp/views.py

 # Create your views here.
from django.shortcuts import render_to_response def index(req):
return render_to_response('index.html') ~
~

vi myapp/settings.py

 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.
#######################################
'/home/django/mytest/templates',
) 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',
###############################
'myapp',
)

3 启动服务器并在浏览器测试
 python manage.py runserver

http://127.0.0.1:8000/myapp/index

4 解析运行原理:

当网页上要请求时,首先进入mysite/urls.py中,执行:

url(r'^myapp/',include('myapp.urls')),

再次调用myapp/urls.py中的文件,执行:

url('^index/$','myapp.views.index'),

接着进入myapp/views视图中的index函数,执行:

def index(req):
     return render_to_response('index.html')
这样就返回模板中的index.html文件,即执行templates/index.html

重要的地方:指向 include() 的正则表达式并不包含一个 $ (字符串结尾匹配符),但是包含了一个斜杆。每当 Django 遇到 include() 时,它将截断匹配的URL,并把【剩余】的字符串发往被包含的 URLconf 进一步处理。

django URL常用配置方法

Django开发网站(二)的更多相关文章

  1. Django开发笔记二

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.xadmin添加主题.修改标题页脚和收起左侧菜单 # ...

  2. Django开发网站(四)

    模型: 配置数据库 首先保证数据库已经安装,默认在Ubuntu下已经安装了sqlite3数据库,然后在项目名下的配置文件settings.py修改如下代码: 安装sqlite3 DATABASES = ...

  3. django开发网站 让局域网中的电脑访问你的主机

    1. 关闭主机电脑上的防火墙(不用关闭,加一个端口号就行) 2.在你的settings.py文件中,找到ALLOWED_HOSTS=[ ],在中括号中加入你在局域网中的IP.如我在局域网中的IP为19 ...

  4. Django开发笔记六

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.登录功能完善 登录成功应该是重定向到首页,而不是转发 ...

  5. Django开发笔记五

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.页面继承 定义base.html: <!DOC ...

  6. Django开发笔记四

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.邮箱激活 users app下,models.py: ...

  7. Django开发笔记三

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.基于类的方式重写登录:views.py: from ...

  8. Django开发笔记一

    Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.运行 python manage.py runser ...

  9. MVC5 网站开发之二 创建项目

    昨天对项目的思路大致理了一下,今天先把解决方案建立起来.整个解决包含Ninesky.Web.Ninesky.Core,Ninesky.DataLibrary等3个项目.Ninesky.Web是web应 ...

随机推荐

  1. css笔记09:选择器优先级

    1. (1) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  2. android 读取串口数据的服务

    2016-09-1813:10:03 继承Service,定义抽象方法onDataReceived,子类通过实现抽象方法获取接收到数据的回调. package com.zrsoft.liftad.se ...

  3. 信号量和PV操作写出Bakery算法的同步程序

    面包店烹制面包及蛋糕,由n个销售员卖出.当有顾客进店购买面包或蛋糕时,应先在取号机上取号,然后等待叫号,若有销售员空闲时便叫下一号,试用信号量和PV操作写出Bakery算法的同步程序. 设计要求 1) ...

  4. python(3)-集合

    集合就是把不同的元素组织在一起,但在集合中不允许有重复的元素. >>> a = set() #创建集合 >>> type(a) <class 'set'> ...

  5. ruby 疑难点之—— attr_accessor attr_reader attr_writer

    普通的实例变量 普通的实例变量,我们没法在 class 外面直接访问 #普通的实例变量,只能在 class 内部访问 class C1 def initialize(name) @name = nam ...

  6. poj 2492 并查集

    思路:当a,b的根节点find(a)与find(b)不同时,就直接将这两个数连接起来.由于每个树的根节点的kind值一定为0,所以,对于a,b的kind值相同,我们就讲其中一个根的kind值变为1,当 ...

  7. hishop网站迁移后出现DataProtectionConfigurationProvider错误(转)

    配置错误说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件.分析器错误信息: 未能使用提供程序“DataProtectionConfiguration ...

  8. 封装SqliteHelper类--Sqlite数据库

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  9. Slickflow.NET 开源工作流引擎基础介绍(一) -- 引擎基本服务接口API介绍

    1. 工作流术语图示                                              图1 流程图形的BPMN图形元素表示 1) 流程模型定义说明流程(Process):是企 ...

  10. js限制文本框输入字数

    //js代码 <script type="text/javascript"> function checkLen(term){ document.all.termLen ...