第04章节-Python3.5-Django获取多个数据以及文件上传 3

简介: image.png修改login.html Title ...
image.png
  • 修改login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <form action="/login/" method="post">
        <p>
            <input type="text" name="user" placeholder="用户名">
        </p>
        <p>
            <input type="password" name="pwd" placeholder="密码">
        </p>
        <p>
            男: <input type="radio" name="gender" value="1">
            女: <input type="radio" name="gender" value="2">
            卡米: <input type="radio" name="gender" value="3">
        </p>
        <input type="submit" value="提交">

    </form>

</body>
</html>
image.png
image.png
  • 修改views.py
from django.shortcuts import render,HttpResponse,redirect

# Create your views here.


def index(request):
    return HttpResponse('Index')


'''
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":
        v = request.POST.get('gender')
        print(v)
        return render(request, 'login.html')
    else:
        # PUT,DELETE,HEAD,OPTION...
        return redirect("/index/")


image.png
image.png
image.png
  • 效果图:
image.png
image.png

@进一步

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

# Create your views here.


def index(request):
    return HttpResponse('Index')


'''
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)
        return render(request, 'login.html')
    else:
        # PUT,DELETE,HEAD,OPTION...
        return redirect("/index/")


  • 修改login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <form action="/login/" method="post">
        <p>
            <input type="text" name="user" placeholder="用户名">
        </p>
        <p>
            <input type="password" name="pwd" placeholder="密码">
        </p>
        <p>
            男: <input type="radio" name="gender" value="1">
            女: <input type="radio" name="gender" value="2">
            卡米: <input type="radio" name="gender" value="3">
        </p>
        <p>
            男: <input type="checkbox" name="favor" value="11">
            女: <input type="checkbox" name="favor" value="22">
            卡米: <input type="checkbox" name="favor" value="33">
        </p>
        <input type="submit" value="提交">

    </form>

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


    image.png
image.png

@ 进一步把上传文件拿到

image.png
  • 修改login.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--# 如果要上传文件表单要加上enctype="multipart/form-data"-->
    <form action="/login/" method="post" enctype="multipart/form-data">
        <p>
            <input type="text" name="user" placeholder="用户名">
        </p>
        <p>
            <input type="password" name="pwd" placeholder="密码">
        </p>
        <p>
            男: <input type="radio" name="gender" value="1">
            女: <input type="radio" name="gender" value="2">
            卡米: <input type="radio" name="gender" value="3">
        </p>
        <p>
            男: <input type="checkbox" name="favor" value="11">
            女: <input type="checkbox" name="favor" value="22">
            卡米: <input type="checkbox" name="favor" value="33">
        </p>
        <p>
            <select name="city" id="">
                <option value="sh">上海</option>
                <option value="bj">北京</option>
                <option value="tj">天津</option>
            </select>
        </p>
        <p>
            <!--上传文件的标签-->
            <input type="file" name="fff">
        </p>
        <input type="submit" value="提交">

    </form>

</body>
</html>
image.png
  • $
image.png
  • 修改views.py
from django.shortcuts import render,HttpResponse,redirect

# Create your views here.


def index(request):
    return HttpResponse('Index')


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

        # 把上传文件读取一点一点拿到
        f = open(obj.name, 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/")


image.png
image.png

-@效果图:


image.png
1.PNG
image.png
image.png
  • @ 目的要求:想把所上传的文件放到统一文件里面

image.png
  • 新建upload 文件夹
  • 修改views.py:
from django.shortcuts import render,HttpResponse,redirect

# Create your views here.


def index(request):
    return HttpResponse('Index')


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


image.png
  • $效果图:

image.png
image.png
目录
相关文章
|
7天前
|
数据采集 存储 API
网络爬虫与数据采集:使用Python自动化获取网页数据
【4月更文挑战第12天】本文介绍了Python网络爬虫的基础知识,包括网络爬虫概念(请求网页、解析、存储数据和处理异常)和Python常用的爬虫库requests(发送HTTP请求)与BeautifulSoup(解析HTML)。通过基本流程示例展示了如何导入库、发送请求、解析网页、提取数据、存储数据及处理异常。还提到了Python爬虫的实际应用,如获取新闻数据和商品信息。
|
28天前
|
数据格式 Python
如何使用Python的Pandas库进行数据透视图(melt/cast)操作?
Pandas的`melt()`和`pivot()`函数用于数据透视。基本步骤:导入pandas,创建DataFrame,然后使用这两个函数转换数据格式。示例代码展示了如何通过`melt()`转为长格式,再用`pivot()`恢复为宽格式。输入数据是包含&#39;Name&#39;和&#39;Age&#39;列的DataFrame,最终结果经过转换后呈现出不同的布局。
39 6
|
28天前
|
数据挖掘 数据处理 索引
如何使用Python的Pandas库进行数据筛选和过滤?
Pandas是Python数据分析的核心库,其DataFrame数据结构便于数据操作。筛选与过滤数据主要包括:导入pandas,创建DataFrame,通过布尔索引、`query()`或`loc[]`、`iloc[]`方法筛选。
|
29天前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名?
Pandas在Python中提供数据排序和排名功能。使用`sort_values()`进行排序,如`df.sort_values(by=&#39;A&#39;, ascending=False)`进行降序排序;用`rank()`进行排名,如`df[&#39;A&#39;].rank(ascending=False)`进行降序排名。多列操作可传入列名列表,如`df.sort_values(by=[&#39;A&#39;, &#39;B&#39;], ascending=[True, False])`。
23 6
|
1天前
|
机器学习/深度学习 Python 数据处理
Python中利用长短期记忆模型LSTM进行时间序列预测分析 - 预测电力负荷数据
Python中利用长短期记忆模型LSTM进行时间序列预测分析 - 预测电力负荷数据
12 0
Python中利用长短期记忆模型LSTM进行时间序列预测分析 - 预测电力负荷数据
|
1天前
|
存储 机器学习/深度学习 数据可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
39 6
|
2天前
|
机器学习/深度学习 数据采集 供应链
从数据到决策:scikit-learn在业务分析中的应用
【4月更文挑战第17天】本文探讨了scikit-learn在业务分析中的应用,包括数据预处理、分类、回归和聚类模型的构建,以及模型评估与优化。通过使用scikit-learn,企业能有效处理数据、预测趋势、客户细分并制定决策,从而提升经营效率和市场策略。随着机器学习的发展,scikit-learn在业务分析领域的潜力将持续释放,创造更多价值。
|
2天前
|
算法 数据可视化 Python
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
10 0
|
2天前
|
BI 开发者 数据格式
Python代码填充数据到word模板中
【4月更文挑战第16天】
|
2天前
|
数据可视化 算法 API
Python数据可视化-seaborn Iris鸢尾花数据
Python数据可视化-seaborn Iris鸢尾花数据

热门文章

最新文章