• Django 框架之 URL


    • URL配置就像Django所支撑网站的目录。它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表。
    # 示例:
    urlpatterns = [
        path(route, view, kwargs=None, name=None),
    
        re_path(正则表达式, views视图函数, 参数, 别名),
    ]
    # 参数说明:
    #   一个正则表达式字符串
    #   一个可调用对象,通常为一个视图函数或一个指定视图函数路径的字符串
    #   可选的要传递给视图函数的默认参数(字典形式)
    #   一个可选的name参数
    
    
    # 示例一:
    # mysite/mysite/urls.py
    from django.urls import path, re_path
    from blog import views
    
    urlpatterns = [
        path('cur_time', views.cur_time),
        path('userInfo', views.userInfo),
    
        re_path(r'^articles/2003/$', views.special_case_2003),
        re_path(r'^articles/([0-9]{4})/$', views.year_archive)
        re_path(r'^articles/([0-9]{4})/([0-9]{2})/', views.month_archive)
    ]
    
    # mysite/blog/views.py
    def special_case_2003(req):
        return HttpResponse('2003')
    
    def year_archive(req, y):       # 参数y接收: ([0-9]{4}) 传入的值
        return HttpResponse('year':y)   
    
    def month_archive(req, y, m):
        return HttpResponse('year:'+y+'month:'+m)    # 参数y接收: ([0-9]{4}) 传入的值, m 接收([0-9]{2})
    
    
    # 示例二: Named groups
    
    # 预备知识: 正则表达式
    import re
    
    ret = re.search('(?P<id>d{3})/(?P<name>w{3})', 'wwwwwfffttt555/ooo')
    
    print(ret.group())          # 输出: 555/ooo
    print(ret.group('id'))      # 输出: 555
    print(ret.group('name'))    # 输出: ooo
    
    # mysite/mysite/urls.py
    from django.urls import path, re_path
    from blog import views
    
    urlpatterns = [
        re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/', views.month_archive)
    ]
    
    # mysite/blog/views.py
    def month_archive(req, year, month):           # 此处形参必须为 year,month
        return HttpResponse('year:'+year+'month:'+month)    
    
    
    # 示例三:
    # mysite/mysite/urls.py
    from django.urls import path, re_path
    from blog import views
    
    urlpatterns = [
        re_path(r'^index/(?P<name>[0-9]{4})/', views.index, {'name':'xiaohu'})
    ]
    
    # mysite/blog/views.py
    def index(req, name):           # 此处形参必须为 name
        return HttpResponse(name)           # 参数 {'name':'xiaohu'} 会覆盖URL中的参数
    
    
    # 示例四:
    # mysite/mysite/urls.py
    from django.urls import path, re_path
    from blog import views
    
    urlpatterns = [
        re_path(r'^index/', views.index, name='book')    # name表示URL的别名
        re_path(r'^/movie/index/', views.index, name='book')
    ]
    
    # mysite/blog/views.py
    def index(req):          
        if req.method=='POST':
            username=req.POST.get('username')
            pwd=req.POST.get('pwd')
            if username=='xiaohu' and pwd=='123':
                return HttpResponse('登录成功!')
    
        return render(req, 'login.html')
    
    # mysite/templates/login.html
    <body>
        <!-- 相比于 action='/index/', URL地址更灵活 --
        <form action="{% url 'book' %}" method="post">
            <input type="text" name="username">
            <input type="password" name="pwd">
            <input type="submit" value="submit">
        </form>
    </body>
    
    # 访问地址:
    http://localhost:8080/index
    http://localhost:8080/movie/index
    

    参考资料

  • 相关阅读:
    ==与===的区别判断
    javascript中new操作符
    javascript中toString和valueOf方法的区别
    数据库索引
    QVegas-一个升级版的TCP Vegas拥塞算法
    海量数据解决方式,知多少?
    最通俗易懂的网络应用层协议具体解释
    Android视图注入库:butterknife
    POJ 3210 : Coins
    ES6的新特性-让前后端差异越来越小
  • 原文地址:https://www.cnblogs.com/linkworld/p/8683164.html
Copyright © 2020-2023  润新知