django学习---urls配置

来源:互联网 发布:网络安全教育文章 编辑:程序博客网 时间:2024/06/13 09:32
urls的配置分为三步:
(1)增加urls映射
(2)在views中定义模板函数
(3)定义新模板

一、首先建立一个新的工程
      (1) django-admin.py startproject study_urls
      (2) 配置settings文件(修改时区、语言,增加app为test_urls)
      (3) 新建app:django-admin.py startapp test_urls

二、编写test_urls包里的视图文件views
      (1)定义了一个时间函数和姓名函数
#coding=utf-8from django.shortcuts import renderfrom django.template import loader,Contextfrom django.http import HttpResponseimport datetime# Create your views here.def time(request):    """时间模版函数,返回当前时间"""    t = loader.get_template("time.html")   #读取模版    time = datetime.datetime.now()          c = Context({"time":time})        #填充数据    return HttpResponse(t.render(c))    def name(request):    """name函数,返回姓名"""    t = loader.get_template("name.html")    name = ['James','Wade','Kobe']    c = Context({"name":name})    return HttpResponse(t.render(c))    
   (2)在test_urls包中新建templates模板文件夹,在模板文件夹写入time.html和name.html

time.html
<!doctype html><html lang="en"> <head>  <meta charset="UTF-8">  <meta name="Generator" content="EditPlus®">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content="">  <title>Now</title> </head> <body>   <center>   <h1> {{time}}</h1>   </center> </body></html>
name.html
<!doctype html><html lang="en"> <head>  <meta charset="UTF-8">  <meta name="Generator" content="EditPlus®">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content="">  <title>NBA top3</title> </head> <body>   <center>    <h3>    {% for name in name %}   <li>{{name}}</li>  {% endfor %}</h3>   </center> </body></html>

三、配置study_urls中的urls.py文件
from django.conf.urls import urlfrom django.contrib import adminfrom test_urls import viewsurlpatterns = [    url(r'^admin/', admin.site.urls),    url(r'^time/',views.time),    url(r'^name/',views.name)]

四、启动服务 manage.py runserver


urls设置站点分离
一、将urls文件拷贝在test_urls包里
修改test_urls中的文件为:
</pre><pre class="python" name="code">from django.conf.urls import urlfrom django.contrib import adminimport viewsurlpatterns = [        url(r'^time/',views.time),    url(r'^name/',views.name)]

修改study_urls中urls.py文件:
from django.contrib import adminfrom django.conf.urls import url, includeurlpatterns = [    url(r'^admin/', admin.site.urls),    url(r'^test_urls/', include('test_urls.urls'))]





urls传参
一、利用request.GET.get传参
修改views视图函数:
    id = request.GET.get('id')    ball = request.GET.get('ball')    c = Context({"name":name,"id":id,"ball":ball})    
修改模版文件
<li> id is {{id}}</li><li> ball is {{ball}}</li>
刷新网页:http://127.0.0.1:8000/test_urls/name/?id=23&ball=basketball



二.利用正则表达式传参
(1)修改test_urls中的urls.py文件
url(r'^info/(\d+)/$',views.info)
(2)修改views文件
def info(request,id1):    """name函数,返回姓名"""    t = loader.get_template("name.html")    name = ['James','Wade','Kobe']    c = Context({"name":name,"id1":id1})    return HttpResponse(t.render(c))

(3)结果:

(4)指定参数名字
url(r'^bar/(?P<no>\d+)/$',views.bar)
在views文件中必须指定bar函数传入的参数名为no




0 0