python(django url index)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {{ user_dict.k1 }}
<u1>
    {% for k,row in user_dict.items %}
        <li>{{ k }}-{{ row }}</li>



    {% endfor %}

</u1>

</body>
</html>
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':'[email protected]'},
#     '2':{'name':'root2','email':'[email protected]'},
#     '3':{'name':'root3','email':'[email protected]'},
#     '4':{'name':'root4','email':'[email protected]'},
#     '5':{'name':'root5','email':'[email protected]'},
# }




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


# 账号密码验证
"""
def login(request):
    if request.method =="GET":
        return render(request,'login.html')

    elif request.method == "POST":
        u = request.POST.get('user')
        p = request.POST.get('pwd')
        if u == 'sooele' and p =='123':
            return redirect('/index')
        else:
            return render(request, 'login.html')
    else:
        return redirect('/index/')
"""
# 单选择
"""
def login(request):
    if request.method =="GET":
        return render(request,'login.html')
    elif request.method == "POST":
       v = request.POST.get('gender')
       print(v)
       return render(request, 'login.html')
    else:
        return redirect('/index/')

"""
# 多
"""
def login(request):
    if request.method == "GET":
        return render(request, 'login.html')
    elif request.method == "POST":
        v = request.POST.get('favor')
        print(v)
        return render(request, 'login.html')
    else:
        return redirect('/index/')
"""
# 文件接收
"""
def login(request):
    if request.method == "GET":
        return render(request, 'login.html')
    elif request.method == "POST":
        # radio
        # v = request.POST.get('gender')
        # print(v)
        # v = request.POST.getlist('favor')
        # print(v)
        # v = request.POST.get('fafafa')
        # print(v)
        obj = request.FILES.get('fafafa')
        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()

        from django.core.files.uploadedfile import InMemoryUploadedFile
        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):

    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')

发表评论