前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Django的电子商务网站开发(连载12)

基于Django的电子商务网站开发(连载12)

作者头像
顾翔
发布2019-12-11 15:20:44
3690
发布2019-12-11 15:20:44
举报
文章被收录于专栏:啄木鸟软件测试

3.3.2用户登录

注册的用户可以通过登录页面登录系统。由于这个模块在前面讲得比较多了,在这里不做过多的解释。

1. urls.py

...url(r'^$', views.index),url(r'^index/$', views.index),url(r'^ login_action /$', views.login_action),...

由于第2.6.3介绍,登录页面为系统首页,提供了三个URL,分别对应。

(1)127.0.0.1:8000/。

(2)127.0.0.1:8000/index/。

(3)127.0.0.1:8000/login_action/。

2. views.py

...# 首页(登录)def index(request): uf = LoginForm()return render_to_response('index.html',{'uf':uf})...#用户登录def login_action(request): if request.method == "POST": uf = LoginForm(request.POST) if uf.is_valid(): # 寻找名为 "username"和"password"的POST参数,而且如果参数没有提交,返回一个空的字符串。 username = (request.POST.get('username')).strip() password = (request.POST.get('password')).strip()# 判断输入数据是否为空 if username == '' or password == '': return render(request,"index.html",{'uf':uf,"error":"用户名和密码不能为空"}) else: # 判断用户名和密码是否准确 user = User.objects.filter(username = username,password = password) if user: response = HttpResponseRedirect('/goods_view/') # 登录成功跳转查看商品信息 request.session['username'] = username # 将session 信息写到服务器 return response else: return render(request,"index.html",{'uf':uf,"error":"用户名或者密码错误"}) else: uf = LoginForm()return render_to_response('index.html',{'uf':uf})...

3. 模板

模板文件为index.html,其内容为。

{%load staticfiles%}<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --> <meta name="description" content=""> <meta name="author" content=""> <link rel="icon" href="../../favicon.ico"> <title>电子商务系统-登录</title> <!-- Bootstrap core CSS --> <link href="{%static 'css/signin.css'%}" rel="stylesheet"--> <!-- Custom styles for this template --> <link href="{%static 'css/bootstrap.min.css'%}" rel="stylesheet"--> <link href="{%static 'css/my.css'%}" rel="stylesheet"> </head> <body> <div> <form method="post" action="/login_action/" enctype="multipart/form-data"> <h2>电子商务系统-登录</h2> {{uf.as_p}} <p style="color:red">{{error}}</p><br> <button class="btn btn-lg btn-primary btn-block" type="submit">登录</button><br> <a href="\register\">注册</a> </form> </div><!-- /container --> </body>

(1){{error}}:显示错误提示信息。

(2){{uf.as_p}}:显示表单信息。

如图3-3所示。

图3-3用户登录界面

星云测试

http://www.threadingtest.com/

奇林软件

http://www.kylinpet.com

联合通测

http://www.quicktesting.net

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-12-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 3.3.2用户登录
    • 1. urls.py
      • 2. views.py
        • 3. 模板
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档