第06章节-Python3.5-Django模板语言循环字典 5

简介: image.png修改views.pyfrom django.shortcuts import render,HttpResponse,redirect# Create your views here.
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',
}


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


'''
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 %}
            <li>{{ k }}-{{ row }}</li>
        {% endfor %}
    </ul>

</body>
</html>
  • 效果图:


    image.png
目录
相关文章
|
2天前
|
存储 Python
python将字典的键或值解包到变量中
【7月更文挑战第5天】
12 4
|
2天前
|
Python
|
3天前
|
Python
Python中字典 直接解包
【7月更文挑战第4天】
10 3
|
2天前
|
Python
python解包字典到函数参数
【7月更文挑战第5天】
6 2
|
3天前
|
Python
Python中字典解包使用*和**操作符
【7月更文挑战第4天】
9 3
|
2天前
|
设计模式 开发者 Python
Python中循环依赖问题及其解决方案
循环依赖是 Python 开发中需要特别注意的问题。通过重新设计模块结构、延迟导入、依赖注入、利用 Python 的动态特性以及代码重构等方法,可以有效地解决循环依赖问题。这些策略不仅有助于提高代码的可维护性和可读性,还能避免潜在的运行时错误。在实际开发中,开发者应该根据具体情况选择合适的解决方案。
|
2天前
|
Python
python解包字典到新的字典
【7月更文挑战第5天】
10 1
|
3天前
|
Python
Python中字典解包解包到变量
【7月更文挑战第4天】
9 1
|
5天前
|
Python
Python中的解包字典
【7月更文挑战第2天】
5 1
|
9天前
|
Python
Python中字典解包
【6月更文挑战第21天】
12 2