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应 ...
随机推荐
- 由c#的值类型与引用类型说开去
之前一直被灌输,C#分值类型和引用类型,在程序运行时,它们分别存在栈(Stack) 和堆(Heap)上.这也是面试经典问题了,但其实其中存在很大的误解.比如某个实例对象中有一个Int型成员.当这个实例 ...
- Spket在Eclipse/MyEclipse下的安装和配置支持Ext(图文教程)
一.安装Spket 第一种方法:网上更新方式 1.插件首页:http://www.spket.com2.插件名称:Spket IDE3.更新连接(Update Site):http://www.spk ...
- Call to undefined function imagettftext()解决方法
由 老高 发表于 2014-10-03 在 代码人生 分类 老高在一个新环境中装DEDECMS的时候发现后台验证码无法显示.直接搜索一下这个错误,有人说session错误,有的说权限错误等等,这不胡 ...
- ceph运维命令合集
一.集群 1.启动一个ceph进程 启动mon进程 [root@ceph-adm ~]#203.109 service ceph start mon.ceph-mon1 启动msd进程 [root@c ...
- Linux下配置Node环境变量及问题详解
这是之前在Linux下配置Node环境变量时踩过的坑,今天又有小伙伴询问这个问题,因此记录下来,不仅是给新童鞋们一些参考,也方便日后查阅 在这之前,相信都已经安装好了,没安装的可以查看博主另一篇文章 ...
- hdu 4585 set应用
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #i ...
- poj 1201 差分约束
http://www.cnblogs.com/wangfang20/p/3196858.html 题意: 求集合Z中至少要包含多少个元素才能是每个区间[ai,bi]中的元素与Z中的元素重合个数为ci. ...
- webView中支持input的file的选择和alert弹出
alert()弹出 input的file现选择(特别说明:不同的android版本弹出的样式不同,选择文件后自动上传) webView.setWebChromeClient(new WebChrome ...
- BZOJ 2962
2962: 序列操作 Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 618 Solved: 225[Submit][Status][Discuss] ...
- IIS6批量转移网站
IIS6.0有个导出配置的功能,但你却找不到界面上的直接导入配置功能,需要用到操作系统自带的iiscnfg.vbs脚本. 1.导出当前的IIS网站配置 打开Internet信息服务(IIS)---&g ...