Django视图和URL配置详解

yipeiwu_com7年前Python基础

本文研究的主要是Django视图和URL配置,具体介绍如下。

一、视图

1.在mysite文件夹下,创建views.py文件(文件名没有特别的要求);

from django.http import HttpResponse 
   def hello(request): 
     return HttpResponse("Hello world") 

2.修改mysite文件夹下的urls.py文件;

from django.conf.urls import url 
from django.contrib import admin 
 
from mysite.views import hello 
 
urlpatterns = [ 
  url(r'^admin/', admin.site.urls), 
  url(r'^hello/$', hello), 
] 

3.运行命令:python manage.py runserver。在浏览器访问http://127.0.0.1:8000/hello/;

4.你的URL申请在URLconf中没有定义,“404错误”页面就会显示,它精确的告诉你Django调用在哪个URLconf及其包含的每个模式,该页面只会在调试模式(debug mode)下显示;

二、Django是如何处理请求

1.当你运行python manage.py runserver,在manage.py同一个目录下查找名为setting.py的文件,该文件包含了关于所有Django项目的配置信息;

ROOT_URLCONF='mysite.urls'#指向自动生产的urls.py

2.浏览器里敲http://127.0.0.1:8000/hello访问时候,根据ROOT_URLCONF的设置装载URLconf,然后按顺序逐个匹配URLConf里面的URLpatterns,直到找到一个匹配的;

3.当找到这个匹配的URLpatterns就调用相关联的view函数,并把HttpRequest对象作为第一个参数;

4.Django转换成HttpResponse为一个适合的HTTP reponse,以Web page显示出来;

三、动态内容

1.在项目views.py中添加,如下视图:

from django.http import HttpResponse 
import datetime 
 
  def hello(request): 
    return HttpResponse("Hello world") 
 
  def current_datetime(request): 
    now = datetime.datetime.now() 
    html = "<html><body>It is now %s.</body></html>" % now 
    return HttpResponse(html) 

2.在项目urls.py中添加,如下配置:

from django.conf.urls import url 
from django.contrib import admin 
 
from mysite.views import hello , current_datetime 
 
urlpatterns = [ 
  url(r'^admin/', admin.site.urls), 
  url(r'^hello/$', hello), 
  url(r'^time/$', current_datetime), 
] 

3.重新启动服务,访问http://127.0.0.1:8000/time

4.松耦合原则,URL的定义和视图函数之间的松耦合;

四、动态URL

1.在项目views.py中添加,如下视图:

from django.http import HttpResponse , Http404 
import datetime 
... ...  
def hours_ahead(request, offset): 
  try: 
    offset = int(offset) 
  except ValueError: 
    raise Http404() 
  dt = datetime.datetime.now() + datetime.timedelta(hours=offset) 
  html = "<html><body>In %s hours(s), it will be %s.</body></html>" % (offset, dt) 
  return HttpResponse(html) 

2.在项目urls.py中添加,如下配置:

from django.conf.urls import url 
from django.contrib import admin 
 
from mysite.views import hello , current_datetime , hours_ahead 
 
urlpatterns = [ 
  url(r'^admin/', admin.site.urls), 
  ... ...  
  url(r'^time/plus/(\d{1,2})/$', hours_ahead), 
] 

3.重新启动服务,访问http://127.0.0.1:8000/time/1

总结

以上就是本文关于Django视图和URL配置详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

解决uWSGI的编码问题详解

发现问题 最近工作中遇到一个问题,在把 Flask 写的应用通过 Supervisor+uWSGI 部署到正式服务器上时,出现了这样的错误: Unable to print the...

利用Python复制文件的9种方法总结

利用Python复制文件的9种方法总结

以下是演示**“如何在Python中复制文件”的九种方法**。 shutil copyfile()方法 shutil copy()方法 shutil copyfileobj...

Python机器学习之决策树算法实例详解

Python机器学习之决策树算法实例详解

本文实例讲述了Python机器学习之决策树算法。分享给大家供大家参考,具体如下: 决策树学习是应用最广泛的归纳推理算法之一,是一种逼近离散值目标函数的方法,在这种方法中学习到的函数被表示...

python实现图片识别汽车功能

python实现图片识别汽车功能

本文实例为大家分享了python实现图片识别汽车的具体代码,供大家参考,具体内容如下 准备工作 1、登陆开发者控制台 2、安装 pip install baidu-aip 模块 原...

对python3中pathlib库的Path类的使用详解

对python3中pathlib库的Path类的使用详解

用了很久的os.path,今天发现竟然还有这么好用的库,记录下来以便使用。 1.调用库 from pathlib import 2.创建Path对象 p = Path('D:...