• python ==> Django.view(登录,注册,个人页)


    Django-views

    一:需求介绍

    1.本篇博客主要是想实现一个:通过Django创建一个项目,目的: 三个界面,用户登录界面,用户注册界面,用户登录成功之后的个人页。本篇功能很简单,就是简单的时间页面的跳转,主要以练习为主。

    首先:我们先把项目创建好,这里我创建的Django的项目名为:mysite_views,并且创建一个应用项目:app01.  两种创建方式:

    一:图形界面

    二:命令行

    注意:要在对应的目录路径下创建。

    创建项目名:  django-admin startproject  项目名称 mysite_views

    创建一个应用: python  manage.py  startapp   应用名称  app01

     注意:同时装了两个版本的pycharm,创建一个应用要用 python3 manage.py startapp 应用名称 app01

     创建好之后,对应的目录格式如下图:

    然后:展示下各主要文件的代码。

    urls.py :映射关系

    """mysite_views URL Configuration
    
    The `urlpatterns` list routes URLs to views. For more information please see:
        https://docs.djangoproject.com/en/1.11/topics/http/urls/
    Examples:
    Function views
        1. Add an import:  from my_app import views
        2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
    Class-based views
        1. Add an import:  from other_app.views import Home
        2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
    Including another URLconf
        1. Import the include() function: from django.conf.urls import url, include
        2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
    """
    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views              #导入模块
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^login.html/',views.login),                 #用户登录
        url(r'^register.html/',views.register),                   # 用户注册
        url(r'^index.html',views.index)                            #登录成功之后的个人页   
    ]
    

    views.py: 视图逻辑处理

    from django.shortcuts import render,HttpResponse,redirect
    
    # Create your views here.
    
    # 1.登录
    def login(request):
        print('----------------------------------->',)
        if request.method == "POST":
            username = request.POST.get("user")
            password = request.POST.get("pwd")
            print('----------------------------------->',username,password)
            import pymysql
            # 连接数据库
            conn = pymysql.connect(host="" ,port=3306,user="root",passwd="",db="s6")
            # 创建游标
            cur=conn.cursor()
            sql = "select * from userinfo where name = '%s' and password = '%s'" %(username,password)
            ret = cur.execute(sql)
            print('----------------------------------->')
            if ret:
                print('----------------------------------->login')
                return redirect ("/index.html/?name=%s"%username)
            else:
                return render(request,"login.html")
    
    
        # 3.跳转页面
        # return HttpResponse('ok')
        return render(request,"login.html")
    
    # 2注册
    def register(request):
        if request.method == "POST":
            # 1.取数据
            username = request.POST.get("user")
            passwd = request.POST.get("pwd")
            # 2.存放数据库
            import pymysql
            # # 连接数据库
            conn = pymysql.connect(host='', port=3306, user='root', passwd='', db='s6')  # db:库名
            # # 创建游标
            cur = conn.cursor()
            sql = "select * from userinfo where NAME='%s' AND password='%s'" % (username, passwd)
            ret = cur.execute(sql)
            if ret:
                return render(request,"register.html")
            else:
                ss = 'insert into userinfo (name,password) value ("%s","%s")' % (username, passwd)
                print(ss)
                cur.execute(ss)
                conn.commit()
                # 关闭指针对象
                cur.close()
                # 关闭连接对象
                conn.close()
                return redirect("/login.html/")
            # 3.跳转页面
            return redirect("/login.html/")
        return render(request,"register.html")
    
    # 3.个人页
    def index(request):
        # print("GET", request.POST)
        # user = request.GET.get("username")
        # n = user
        # print(request.get_full_path())
        ret = request.GET.get('name')
        print(request.GET.get('name'))
        # return HttpResponse('ok')
        return render(request,"index.html",{"n":ret})

    templates下的前端页面:

      login.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>login</title>
    </head>
    <body>
    
    <form action="" method="post">
        <h1>登录页面</h1>
        <p>用户名<input type="username" name="user"></p>
        <p>密码&nbsp &nbsp<input type="password" name="pwd"></p>
        <input type="submit" value="登录">
    </form>
    </body>
    </html>
    

        register.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>注册</title>
    </head>
    <body>
    <form action="/register.html/" method="post">
        <h1>注册页面</h1>
        <p>用户名<input type="text" name="user"></p>
        <p>密码&nbsp &nbsp<input type="password" name="pwd"></p>
        <input type="submit" value="确认注册">
    </form>
    </body>
    </html>
    

      index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Title</title>
    </head>
    <body>
    <h1>Hello   {{ n }}
        welcome come to plase! </h1>
    </body>
    </html> 

    注意事项:

      setting里的配置文件:

    运行代码:

    启动: python manage.py runserver 8080

    成功进入到个人页面。

      

      

  • 相关阅读:
    Mybatis分页插件PageHelper使用
    JAVA面试笔记
    基于Fusioncharts的报表统计
    微信支付开发流程
    Java 使用 Jxl 实现 Excel 导入导出
    从navicat中导入sql文件过大:Got a packet bigger than 'max_allowed_packet' bytes
    一个故事告诉你比特币的原理及运作机制
    Linux和Windows下tomcat开机自启动设置
    Linux下安装MySQL
    ubuntu 13.10使用fcitx输入法
  • 原文地址:https://www.cnblogs.com/zhongbokun/p/7726620.html
Copyright © 2020-2023  润新知