Django开发网站(二)
第一课:视图显示
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开发网站(二)的更多相关文章
- Django开发笔记二
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.xadmin添加主题.修改标题页脚和收起左侧菜单 # ...
- Django开发网站(四)
模型: 配置数据库 首先保证数据库已经安装,默认在Ubuntu下已经安装了sqlite3数据库,然后在项目名下的配置文件settings.py修改如下代码: 安装sqlite3 DATABASES = ...
- django开发网站 让局域网中的电脑访问你的主机
1. 关闭主机电脑上的防火墙(不用关闭,加一个端口号就行) 2.在你的settings.py文件中,找到ALLOWED_HOSTS=[ ],在中括号中加入你在局域网中的IP.如我在局域网中的IP为19 ...
- Django开发笔记六
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.登录功能完善 登录成功应该是重定向到首页,而不是转发 ...
- Django开发笔记五
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.页面继承 定义base.html: <!DOC ...
- Django开发笔记四
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.邮箱激活 users app下,models.py: ...
- Django开发笔记三
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.基于类的方式重写登录:views.py: from ...
- Django开发笔记一
Django开发笔记一 Django开发笔记二 Django开发笔记三 Django开发笔记四 Django开发笔记五 Django开发笔记六 1.运行 python manage.py runser ...
- MVC5 网站开发之二 创建项目
昨天对项目的思路大致理了一下,今天先把解决方案建立起来.整个解决包含Ninesky.Web.Ninesky.Core,Ninesky.DataLibrary等3个项目.Ninesky.Web是web应 ...
随机推荐
- A - Robot Bicorn Attack
Description Vasya plays Robot Bicorn Attack. The game consists of three rounds. For each one a non-n ...
- B - 一行盒子
Description 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子 ...
- SqlServer高版本数据本分还原到低版本方法
最近遇见一个问题: 想要将Sqlserver高版本备份的数据还原到低版本SqlServer上去,但是这在SqlServer中是没法直接还原数据库的,所以经过一系列的请教总结出来一下可用方法. 首先.你 ...
- HDU 1043 Eight (A* + HASH + 康托展开)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- FragmentTabHost切换Fragment时避免重复加载UI
使用FragmentTabHost时,Fragment之间切换时每次都会调用onCreateView方法,导致每次Fragment的布局都重绘,无法保持Fragment原有状态. 解决办法:在Frag ...
- 什么是MVC开发模式以及它和传统开发模式的区别
Model1模式:使用纯JSP或者JSP+JavaBean开发,存在如下缺陷:JSP页面中混合了HTML和JAVA代码,从而给代码的开发和阅读带 来了麻烦:系统后期维护和扩展非常困难,例如在JSP页面 ...
- Tomcat - JNDI 配置
1. Create Your JavaBean Class Create the JavaBean class which will be instantiated each time that th ...
- Linux 命令 - traceroute: 数据报传输路径追踪
traceroute 工具用于跟踪数据报的传输路径:当数据报从一台计算机传向另一台计算机时,会经过多重的网关,traceroute 命令能够找到数据报传输路径上的所有路由器.通过 traceroute ...
- iOS开发那些事--性能优化–内存泄露问题的解决(转)
内存泄漏问题的解决 内存泄漏(Memory Leaks)是当一个对象或变量在使用完成后没有释放掉,这个对象一直占有着这块内存,直到应用停止.如果这种对象过多内存就会耗尽,其它的应用就无法运行.这个问题 ...
- php5.4window下连接mssql
第一步 下载 http://www.microsoft.com/en-us/download/details.aspx?id=20098 [PHP_PDO_SQLSRV] extension=php_ ...