关闭

django学习---urls配置

标签: django
33人阅读 评论(0) 收藏 举报
分类:
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-8
from django.shortcuts import render
from django.template import loader,Context
from django.http import HttpResponse
import 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 url
from django.contrib import admin
from test_urls import views

urlpatterns = [
    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 url
from django.contrib import admin
import views

urlpatterns = [
    
    url(r'^time/',views.time),
    url(r'^name/',views.name)
]

修改study_urls中urls.py文件:
from django.contrib import admin
from django.conf.urls import url, include

urlpatterns = [
    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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:35次
    • 积分:10
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档
    阅读排行
    评论排行