第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
目录
相关文章
|
21天前
|
存储 开发者 Python
Python中的collections模块与UserDict:用户自定义字典详解
【4月更文挑战第2天】在Python中,`collections.UserDict`是用于创建自定义字典行为的基类,它提供了一个可扩展的接口。通过继承`UserDict`,可以轻松添加或修改字典功能,如在`__init__`和`__setitem__`等方法中插入自定义逻辑。使用`UserDict`有助于保持代码可读性和可维护性,而不是直接继承内置的`dict`。例如,可以创建一个`LoggingDict`类,在设置键值对时记录操作。这样,开发者可以根据具体需求定制字典行为,同时保持对字典内部管理的抽象。
|
1月前
|
存储 索引 Python
python用户输入和while循环(七)
python用户输入和while循环(七)
16 0
|
5天前
|
前端开发 Java Go
开发语言详解(python、java、Go(Golong)。。。。)
开发语言详解(python、java、Go(Golong)。。。。)
|
6天前
|
BI 开发者 数据格式
Python代码填充数据到word模板中
【4月更文挑战第16天】
|
7天前
|
Python
python-基本类型,运算,循环
python-基本类型,运算,循环
25 0
|
8天前
|
开发者 Python
Django模板系统的强大之处:动态渲染与扩展性
【4月更文挑战第15天】Django模板系统是Web开发中的强大工具,支持动态渲染和扩展性。动态渲染包括变量、标签和过滤器的使用,实现内容根据上下文数据动态生成。模板继承和自定义标签则提升了扩展性,减少代码重复,增强可维护性。通过这些特性,Django模板系统助力开发者构建高效、动态的Web应用。
|
8天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
12天前
|
安全 Python
python字典的内置方法
Python字典主要方法包括:`keys()`(返回所有键)、`values()`(返回所有值)、`items()`(返回所有键值对)、`get()`(安全取值,键不存在时返回默认值)、`setdefault()`(设置默认值)、`update()`(合并字典)、`pop()`(删除并返回值)、`clear()`(清空字典)、`copy()`(浅拷贝)、`fromkeys()`(新建字典并设置默认值)、`popitem()`(随机删除键值对)。
8 0
|
20天前
|
Python
Python 循环使用demo
【4月更文挑战第3天】在Python中,主要的循环结构有for和while。示例包括:使用for循环打印列表[1, 2, 3, 4, 5],以及使用while循环计算1到10的和。`for i in [1, 2, 3, 4, 5]: print(i)`,以及`while count <= 10: sum += count; count += 1; print(sum)`。
11 2
|
21天前
|
存储 Java 程序员
【Python】6. 基础语法(4) -- 列表+元组+字典篇
【Python】6. 基础语法(4) -- 列表+元组+字典篇
41 1