第09章节-Python3.5-Django对应的路由名称

简介: $ 效果图:image.pngimage.png实现原理:urls.py:from django.conf.
  • $ 效果图:

image.png
image.png

实现原理:

  • urls.py:

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'^asdfasdfasdf/(\d+)/(\d+)/', views.index, name='indexx'),
    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),
    # url(r'^detail-(\d+)-(\d+).html', views.detail),
    # url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html', views.detail),
]

image.png
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,nid,unid):
    # indexx
    print(request.path_info)
    # /asdfasdfasdf/13/
    # reverse 能反转
    from django.urls import reverse
    v = reverse('indexx', args=(90,88,))

    print(v)

    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):
    # print(nid, uid)
    # 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>
    <form action="{{ request.path_info }}" method="post">
        <p><input type="text" name="user" placeholder="用户名"></p>
        <p><input type="text" name="email" placeholder="邮箱"></p>
        <input type="submit" value="提交">
    </form>

    <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
  • @进一步:

  • index.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="{% url 'indexx' nid=1 uid=3 %}" method="post">
        <p><input type="text" name="user" placeholder="用户名"></p>
        <p><input type="text" name="email" placeholder="邮箱"></p>
        <input type="submit" value="提交">
    </form>

    <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
  • 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,nid,uid):
    # indexx
    print(request.path_info)
    # /asdfasdfasdf/13/
    # reverse 能反转
    from django.urls import reverse
    # v = reverse('indexx', args=(90,88,))
    v = reverse('indexx', kwargs={'nid':'1','uid':'99'})
    print(v)

    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):
    # print(nid, uid)
    # 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
  • urls.py

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'^asdfasdfasdf/(?P<nid>\d+)/(?P<uid>\d+)/', views.index, name='indexx'),
    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),
    # url(r'^detail-(\d+)-(\d+).html', views.detail),
    # url(r'^detail-(?P<nid>\d+)-(?P<uid>\d+).html', views.detail),
]

image.png
  • ! 效果图:

image.png
image.png
目录
相关文章
|
1天前
|
Python
Django 框架的路由系统
Django 框架的路由系统
16 6
|
2月前
|
Windows Python
python获取windows机子上运行的程序名称
python获取windows机子上运行的程序名称
|
2月前
|
Python
Python批量复制指定名称文件的技巧
通过上述步骤和示例代码,你可以轻松实现批量复制特定名称文件的功能。这种技术不仅节省了时间,而且通过脚本自动化,提高了工作效率。
42 2
|
2月前
|
缓存 中间件 网络架构
Python Web开发实战:高效利用路由与中间件提升应用性能
在Python Web开发中,路由和中间件是构建高效、可扩展应用的核心组件。路由通过装饰器如`@app.route()`将HTTP请求映射到处理函数;中间件则在请求处理流程中插入自定义逻辑,如日志记录和验证。合理设计路由和中间件能显著提升应用性能和可维护性。本文以Flask为例,详细介绍如何优化路由、避免冲突、使用蓝图管理大型应用,并通过中间件实现缓存、请求验证及异常处理等功能,帮助你构建快速且健壮的Web应用。
27 1
|
2月前
|
数据库 Python
django中数据库外键可以自定义名称吗
django中数据库外键可以自定义名称吗
|
2月前
|
Python
python编程获取续蜀山剑侠传:从目录名称、网址到内容
python编程获取续蜀山剑侠传:从目录名称、网址到内容
|
2月前
|
Python
python编程获取《续蜀山剑侠传》目录信息:目录名称和网址
python编程获取《续蜀山剑侠传》目录信息:目录名称和网址
|
3月前
|
存储 Python
Python 中的名称空间是什么?
【8月更文挑战第29天】
17 0
|
3月前
|
Python SEO
Django入门到放弃之路由
Django入门到放弃之路由
|
3月前
|
开发工具 数据安全/隐私保护 Python
【Azure 环境】通过Python SDK收集所有订阅简略信息,例如订阅id 名称, 资源组及组内资源信息等,如何给Python应用赋予相应的权限才能获取到信息呢?
【Azure 环境】通过Python SDK收集所有订阅简略信息,例如订阅id 名称, 资源组及组内资源信息等,如何给Python应用赋予相应的权限才能获取到信息呢?