第07章节-Python3.5-Django基于正则表达式的URL(一) 6

简介: image.pngurls.py:"""s14day19_2 URL ConfigurationThe `urlpatterns` list routes URLs to views.
image.png
  • urls.py:
"""s14day19_2 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', views.index),
    url(r'^login/', views.login),
    # url(r'^home/', views.home),
    # views.Home.as_view()是固定用法
    url(r'^home/', views.Home.as_view()),
    url(r'^detail/', views.detail),
]

image.png
  • index.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--{{ user_dict.k1 }}-->
    <!--<ul>-->
        <!--{% for k in user_dict.keys %}-->
            <!--<li>{{ k }}</li>-->
        <!--{% endfor %}-->
    <!--</ul>-->
    <!--<ul>-->
        <!--{% for val in user_dict.values %}-->
            <!--<li>{{ val }}</li>-->
        <!--{% endfor %}-->
    <!--</ul>-->
    <ul>
        {% for k,row in user_dict.items %}
        <!--target="_blank"表示在新页面打开-->
        <li><a target="_blank" href="/detail/?nid={{ k }}">{{ row.name }}</a></li>
        {% endfor %}
    </ul>

</body>
</html>
image.png
image.png
  • detail.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>详细信息</h1>
    <h6>用户名: {{ detail_info.name }}</h6>
    <h6>邮箱: {{ detail_info.email }}</h6>

</body>
</html>
image.png

image.png

image.png
  • @ 进一步动态路由

  • 修改urls.py(Django基于正则表达式的URL):


from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', views.index),
    url(r'^login/', views.login),
    # url(r'^home/', views.home),
    # views.Home.as_view()是固定用法
    url(r'^home/', views.Home.as_view()),
    # url(r'^detail/', views.detail),
    url(r'^detail-(\d+).html', views.detail),
]

image.png
  • 修改views.py:
from django.shortcuts import render,HttpResponse,redirect

# Create your views here.

# USER_DICT = {
#     'k1': 'root1',
#     'k2': 'root2',
#     'k3': 'root3',
#     'k4': 'root4',
# }

USER_DICT = {
    '1': {'name': 'root1', 'email': 'root@live.com'},
    '2': {'name': 'root2', 'email': 'root@live.com'},
    '3': {'name': 'root3', 'email': 'root@live.com'},
    '4': {'name': 'root4', 'email': 'root@live.com'},
    '5': {'name': 'root5', 'email': 'root@live.com'},
}


def index(request):
    return render(request, 'index.html', {'user_dict': USER_DICT})


# def detail(request):
#     nid = request.GET.get('nid')
#     detail_info = USER_DICT[nid]
#     return render(request, 'detail.html', {'detail_info': detail_info})


def detail(request, nid):
    # return HttpResponse(nid)
    detail_info = USER_DICT[nid]
    return render(request, 'detail.html', {'detail_info': detail_info})


'''
def login(request):
    # 判断用户获取数据方式是GET,就返回什么数据
    if request.method == "GET":
        return render(request, 'login.html')
    # 判断用户获取数据方式是POST,就判断用户提交的数据是否正确
    elif request.method == "POST":
        u = request.POST.get('user')
        p = request.POST.get('pwd')
        if u == 'alex' and p == '123':
            return redirect('/index/')
        else:
            return render(request, 'login.html')
    else:
        # PUT,DELETE,HEAD,OPTION...
        return redirect("/index/")

'''


def login(request):
    # 判断用户获取数据方式是GET,就返回什么数据
    if request.method == "GET":
        return render(request, 'login.html')
    # 判断用户获取数据方式是POST,就判断用户提交的数据是否正确
    elif request.method == "POST":
        # radio
        # v = request.POST.get('gender')
        # print(v)
        # v = request.POST.getlist('favor')
        # print(v)
        v = request.POST.get('fff')
        print(v)
        # 所有上传文件都上传到request.FILES
        obj = request.FILES.get('fff')
        print(obj, type(obj), obj.name)

        # 把所上传的文件放到所建立的文件夹
        import os
        file_path = os.path.join('upload',obj.name)
        # 把上传文件读取一点一点拿到
        f = open(file_path, mode="wb")
        for i in obj.chunks():
            f.write(i)
        f.close()

        return render(request, 'login.html')
    else:
        # PUT,DELETE,HEAD,OPTION...
        return redirect("/index/")


# def home(request):
#     return HttpResponse('Home')


from django.views import View


class Home(View):

    # 调用父类中的dispatch(相当于助理,)
    def dispatch(self, request, *args, **kwargs):
        print('before')
        result = super(Home,self).dispatch(request, *args, **kwargs)
        print('after')
        return result

    def get(self,request):
        print(request.method)
        return render(request, 'home.html')

    def post(self,request):
        print(request.method, 'POST')
        return render(request, 'home.html')

image.png
  • 修改index.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--{{ user_dict.k1 }}-->
    <!--<ul>-->
        <!--{% for k in user_dict.keys %}-->
            <!--<li>{{ k }}</li>-->
        <!--{% endfor %}-->
    <!--</ul>-->
    <!--<ul>-->
        <!--{% for val in user_dict.values %}-->
            <!--<li>{{ val }}</li>-->
        <!--{% endfor %}-->
    <!--</ul>-->
    <!--<ul>-->
        <!--{% for k,row in user_dict.items %}-->
        <!--&lt;!&ndash;target="_blank"表示在新页面打开&ndash;&gt;-->
        <!--<li><a target="_blank" href="/detail/?nid={{ k }}">{{ row.name }}</a></li>-->
        <!--{% endfor %}-->
    <!--</ul>-->
    <ul>
        {% for k,row in user_dict.items %}
        <!--target="_blank"表示在新页面打开-->
        <li><a target="_blank" href="/detail-{{ k }}.html">{{ row.name }}</a></li>
        {% endfor %}
    </ul>

</body>
</html>
image.png
  • $ 效果图:

image.png
image.png
目录
相关文章
|
1月前
|
编译器 Python
Python正则表达式的7个使用典范(推荐)
Python正则表达式的7个使用典范(推荐)
22 0
|
1月前
|
Python
Python实现正则表达式匹配。
【2月更文挑战第11天】【2月更文挑战第30篇】Python实现正则表达式匹配。
|
7天前
|
数据采集 JSON 网络协议
「Python系列」Python urllib库(操作网页URL对网页的内容进行抓取处理)
`urllib` 是 Python 的一个标准库,用于打开和读取 URLs。它提供了一组模块,允许你以编程方式从网络获取数据,如网页内容、文件等。
29 0
|
1月前
|
Python
请解释Python中的正则表达式以及如何使用它们进行文本处理。
请解释Python中的正则表达式以及如何使用它们进行文本处理。
9 0
|
1月前
|
机器学习/深度学习 Python
请解释Python中的正则表达式是什么?并举例说明其用法。
【2月更文挑战第26天】【2月更文挑战第86篇】请解释Python中的正则表达式是什么?并举例说明其用法。
|
1月前
|
缓存 数据安全/隐私保护 Python
Python快速入门:类、文件操作、正则表达式
Python快速入门:类、文件操作、正则表达式
C4.
|
1月前
|
Python
Python正则表达式
Python正则表达式
C4.
13 1
|
1月前
|
Web App开发 测试技术 Python
使用 Python 结合 Selenium 访问一个 url
使用 Python 结合 Selenium 访问一个 url
25 0
|
1月前
|
Web App开发 安全 定位技术
关于使用 Python 和 Selenium chrome driver 访问 url 时修改 source ip 的问题
关于使用 Python 和 Selenium chrome driver 访问 url 时修改 source ip 的问题
59 0
|
1月前
|
Python
在Python中,如何使用`regex`库进行正则表达式匹配?
在Python中,如何使用`regex`库进行正则表达式匹配?
13 0