第15章节-Python3.5-Django实现用户登录与前端交互2 14-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

第15章节-Python3.5-Django实现用户登录与前端交互2 14

简介: 目的我想登陆成功后显示我的后台管理(实现过程): 新建home.html 在templates目录下代码如下: Title ...

目的我想登陆成功后显示我的后台管理(实现过程):

新建home.html 在templates目录下代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="margin: 0">
    <div style="height: 48px;background-color: #dddddd"></div>
    <div>
        <table>
            <tr>
                <td>js</td>
                <td>女</td>
                <td>alex123@163.com</td>
            </tr>
            <tr>
                <td>bs</td>
                <td>女</td>
                <td>alex456@163.com</td>
            </tr>
            <tr>
                <td>cs</td>
                <td>男</td>
                <td>alex123@qq.com</td>
            </tr>
        </table>
    </div>

</body>
</html>
image.png
  • 然后在urls.py 添加以下代码:
from django.conf.urls import url
from django.contrib import admin
from cmdb import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    # url(r'h.html/', views.home),
    url(r'^login', views.login),
    url(r'^home', views.home),
]
image.png
image.png
  • 修改 views.py 代码如下:
from django.shortcuts import render

# Create your views here.
from django.shortcuts import HttpResponse
from django.shortcuts import render
# 重定向
from django.shortcuts import redirect


def login(request):
    # 包含用户提交的所有信息
    # 获取用户提交方法
    # print(request.method)
    # 判断用户名和密码
    error_msg = ""
    if request.method == "POST":
        # 获取用户通过POST提交过来的数据(用户不存在返回None)
        user = request.POST.get('user',None)
        pwd = request.POST.get('pwd',None)
        if user == 'root' and pwd == "123":
            # 去跳转到(重定向)
            return redirect('/home')
        else:
            # 用户密码不正确
            error_msg = "用户名或密码错误"
        # user = request.POST['user']
        # pwd = request.POST['pwd']
        # print(user,pwd)

    return render(request, 'login.html', {'error_msg': error_msg})


def home(request):
    return render(request, 'home.html')


# def login(request):
#     # f = open('templates/login.html', 'r', encoding='utf-8')
#     # date = f.read()
#     # f.close()
#     # return HttpResponse(date)
#     return render(request,'login.html')


# def home(request):
#     return HttpResponse('<h1>CMDB</h1>')

image.png
image.png
  • 效果图:


    image.png

django使用for循环

  • 修改views.py:
from django.shortcuts import render

# Create your views here.
from django.shortcuts import HttpResponse
from django.shortcuts import render
# 重定向
from django.shortcuts import redirect


def login(request):
    # 包含用户提交的所有信息
    # 获取用户提交方法
    # print(request.method)
    # 判断用户名和密码
    error_msg = ""
    if request.method == "POST":
        # 获取用户通过POST提交过来的数据(用户不存在返回None)
        user = request.POST.get('user',None)
        pwd = request.POST.get('pwd',None)
        if user == 'root' and pwd == "123":
            # 去跳转到(重定向)
            return redirect('/home')
        else:
            # 用户密码不正确
            error_msg = "用户名或密码错误"
        # user = request.POST['user']
        # pwd = request.POST['pwd']
        # print(user,pwd)

    return render(request, 'login.html', {'error_msg': error_msg})

USER_LIST = [
    {'username':'alex','email':'asdfasdf',"gender":'男'},
]
for index in range(20):
    temp = {'username':'alex'+str(index),'email':'asdfasdf',"gender":'男'}
    USER_LIST.append(temp)


def home(request):
    return render(request, 'home.html',{'user_list': USER_LIST})


# def login(request):
#     # f = open('templates/login.html', 'r', encoding='utf-8')
#     # date = f.read()
#     # f.close()
#     # return HttpResponse(date)
#     return render(request,'login.html')


# def home(request):
#     return HttpResponse('<h1>CMDB</h1>')

image.png
  • 修改urls.py :
from django.conf.urls import url
from django.contrib import admin
from cmdb import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    # url(r'h.html/', views.home),
    url(r'^login', views.login),
    url(r'^home', views.home),
]
image.png
  • 修改 home.html :
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="margin: 0">
    <div style="height: 48px;background-color: #dddddd"></div>
    <div>
        <table>
            <!--django支持 for 循环,与Python不一样,这是模板语言for循环 有开头就有结尾 endfor-->
            {% for row in user_list %}
            <tr>
                <td>js</td>
                <td>女</td>
                <td>alex123@163.com</td>
            </tr>
            {% endfor %}

        </table>
    </div>

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


    image.png

另一种修改:

  • views.py和urls.py 代码同上不修改
  • home.html 代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="margin: 0">
    <div style="height: 48px;background-color: #dddddd"></div>
    <div>
        <table>
            <!--django支持 for 循环,与Python不一样,这是模板语言for循环 有开头就有结尾 endfor-->
            {% for row in user_list %}
            <tr>
                <td>{{ row.username }}</td>
                <td>{{ row.gender }}</td>
                <td>{{ row.email }}</td>
            </tr>
            {% endfor %}

        </table>
    </div>

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


    image.png

获取用户输入数据展示:

image.png
  • 修改 views.py:
from django.shortcuts import render

# Create your views here.
from django.shortcuts import HttpResponse
from django.shortcuts import render
# 重定向
from django.shortcuts import redirect


def login(request):
    # 包含用户提交的所有信息
    # 获取用户提交方法
    # print(request.method)
    # 判断用户名和密码
    error_msg = ""
    if request.method == "POST":
        # 获取用户通过POST提交过来的数据(用户不存在返回None)
        user = request.POST.get('user',None)
        pwd = request.POST.get('pwd',None)
        if user == 'root' and pwd == "123":
            # 去跳转到(重定向)
            return redirect('/home')
        else:
            # 用户密码不正确
            error_msg = "用户名或密码错误"
        # user = request.POST['user']
        # pwd = request.POST['pwd']
        # print(user,pwd)

    return render(request, 'login.html', {'error_msg': error_msg})

USER_LIST = [
    {'username':'alex','email':'asdfasdf',"gender":'男'},
    {'username':'alex','email':'asdfasdf',"gender":'男'},
    {'username':'alex','email':'asdfasdf',"gender":'男'},
]
# for index in range(20):
#     temp = {'username':'alex'+str(index),'email':'asdfasdf',"gender":'男'}
#     USER_LIST.append(temp)


def home(request):
    if request.method == "POST":
        # 获取用户提交的数据 POST 请求中
        u = request.POST.get('username')
        e = request.POST.get('email')
        g = request.POST.get('gender')
        temp = {'username': u, 'email': e, "gender": g}
        USER_LIST.append(temp)
    return render(request, 'home.html',{'user_list': USER_LIST})


# def login(request):
#     # f = open('templates/login.html', 'r', encoding='utf-8')
#     # date = f.read()
#     # f.close()
#     # return HttpResponse(date)
#     return render(request,'login.html')


# def home(request):
#     return HttpResponse('<h1>CMDB</h1>')

image.png
  • 修改 home.html
    placeholder能在输入显示自己所写的内容(如效果图)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="margin: 0">
    <div style="height: 48px;background-color: #dddddd"></div>
    <div>
        <form action="/home" method="post">
            <input type="text" name="username" placeholder="用户名">
            <input type="text" name="email" placeholder="邮箱">
            <input type="text" name="gender" placeholder="性别">
            <input type="submit" value="添加">
        </form>
    </div>
    <div>
        <table>
            <!--django支持 for 循环,与Python不一样,这是模板语言for循环 有开头就有结尾 endfor-->
            {% for row in user_list %}
            <tr>
                <td>{{ row.username }}</td>
                <td>{{ row.gender }}</td>
                <td>{{ row.email }}</td>
            </tr>
            {% endfor %}

        </table>
    </div>

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


    image.png
image.png

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章