1、django检索途径
应用 import 句子时,Python 所搜索的系统目录明细。
查询方法:
import sys
print sys.path
通常不用关注 Python 检索途径的设定,Python 和 Django 会在后台管理全自动帮你解决好。
2、url模式匹配
基本上构造:
'^必须配对的url字符串数组$'
PS:事实上最后详细的url串是http://根途径:端口/必须配对的url字符串数组
一键加上的一部分'http://根途径:端口/'
eg:url模式匹配:'^latest_books/$'
最后详细的url字符串数组:'http://127.0.0.1:8000/latest_books/'
1)^:配对“字符串函数头”。
eg:
'^latest_books/'
'http://127.0.0.1:8000/latest_books/',
'http://127.0.0.1:8000/latest_books/test1/',
'http://127.0.0.1:8000/latest_books/test2/',
'http://127.0.0.1:8000/latest_books/test1/aaa/'
都是会被配对上。
2)$:匹配“字符串函数末尾”。
eg:
'latest_books/$'
'http://127.0.0.1:8000/latest_books/',
'http://127.0.0.1:8000/updir_1/latest_books/',
'http://127.0.0.1:8000/updir_2/latest_books/'
都是会被配对上。
3)字符串函数结尾是不是包括'/'
默认设置状况下务必加上(django开发人员的基本上习惯性),如果不加上可能发生如下所示状况:
from django.conf.urls import patterns, url, include
urlpatterns = patterns('',
(r'^latest_books$', 'django_web_app.views.latest_books'),
)
假如字符串函数结尾不愿包括'/',可在setting.py中加上设定:APPEND_SLASH=False
可是务必安裝了CommonMiddleware才会起功效。
4)手动式配备网址“网站根目录”
在没有手动式配备网址“网站根目录”相匹配“主视图函数公式”的情形下,会产生如下所示状况:
手动式配备“网站根目录”相匹配“主视图函数公式”:
a)urls.py
from django.conf.urls import patterns, url, include
urlpatterns = patterns('',
(r'^$','django_web_app.views.home_page'),
(r'^latest_books/$', 'django_web_app.views.latest_books'),
)
b)views.py
def home_page(request):
return render_to_response('home_page.html')
c)home_page.html
<!DOCTYPE html>
<html>
<head>
<title>my home page</title>
</head>
<body>
<h1>This is home page, welcome !</h1>
</body>
</html>
运作結果:
附:正则表达式基本
3、服务器端回应url要求的实行次序
1)新项目构造
django_web
._init__.py
settings.py
urls.py
wsgi.py
django_web_app
._init__.py
admin.py
models.py
tests.py
views.py
templates
home_page.html
latest_books.html
manage.py
2)实行次序
a)运行服务器端——python manage.py runserver
获得setting.py文件中的配备,主要包含:
url投射关联文件路径:
ROOT_URLCONF = 'django_web.urls'
页面文件模版途径:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
数据库查询配备:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': 'feng',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
b)回应次序
第一步:电脑浏览器递交要求
http://127.0.0.1:8000/latest_books/
第二步:服务器端依据要求的url在urls.py中开展配对,并寻找相应的“主视图函数公式”
第三步:读取相匹配的“主视图函数公式” 回到一个HttpResponse目标
第四步:django变换HttpResponse目标为一个合适的HTTP response,并回到给网页页面开展表明
做猪小侠源码的代理,提供一站式服务
如果你不懂得搭建网站或者服务器,小程序,源码之类的怎么办? 第一通过本站学习各种互联网的技术 第二就是联系客服,我帮帮你搭建(当然要收取部分的费用) 第三成为我们的代理,我们提供整套的服务。