• Web接口开发与自动化测试


    1.配置urls.py

    from django.contrib import admin
    from django.urls import path,re_path
    from sign import views

    urlpatterns = [
      path('admin/', admin.site.urls),
      # re_path 等于url
      re_path(r'index/', views.index),                # 登录页
      re_path('^$', views.index),                       #登录页
      re_path(r'^accounts/login/$', views.index),              #登录页(强制登录跳转)
      re_path(r'^login_action/$', views.login_action),                 #登录处理
      re_path(r'^event_manage/$', views.event_manage)         #首页
    ]

    2.配置视图 views.py

    from django.shortcuts import render
    from django.http import HttpResponse,HttpResponseRedirect
    from django.contrib import auth
    from django.contrib.auth.decorators import login_required
    # Create your views here.

    def index(request):
      # return HttpResponse("hello world!")
      return render(request, 'index.html')

    def login_action(request):
      if request.method == 'POST':

        # 根据name属性获取input值
        username = request.POST.get('username','')
        password = request.POST.get('password','')

        # 验证账号密码是否存在数据库,如果不存在返回None
        user = auth.authenticate(username=username,password=password)
        if user is not None:

          # 登录
          auth.login(request,user)   

          # 保存session到浏览器
            request.session['user'] = username

          # 登录成功后跳转到 首页
          response = HttpResponseRedirect('/event_manage/')

          return response
        else:
          return render(request, 'index.html', {'error':'账号或密码错误!'})

    @login_required
    def event_manage(request):

      # 获取session
      username = request.session.get('user', '')
      return render(request,'event_manage.html',{'user':username})

    3.配置模板 templates

    3.1 index.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>发布会管理系统</title>
      </head>
      <body>
        <h1>发布会管理系统</h1>
        <form method="post" action="/login_action/">
          <input name="username" type="text" placeholder="账号"><br>
          <input name="password" type="text" placeholder="密码"><br>
          {{ error }}<br>   # 获取2 当账号密码错误时的错误提示
          <button id="submit" type="submit">登陆</button>
        </form>
      </body>
    </html>

    3.2 event_manage.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <title>发布会管理</title>
      </head>
      <body>
        <h1>HI! {{user}} </h1>
        <h1>Welcome!</h1>
      </body>
    </html>

    4.使用数据库登录系统,创建Event,Guest表

    4.1 创建超级管理员

      根据指令来显示

      e:guest>python manage.py createsuperuser

    4.2 创建Event,Guest表

    from django.db import models

    # Create your models here.

    # 发布会表
    class Event(models.Model):
      name = models.CharField(max_length=100)
      limit = models.IntegerField
      status = models.BooleanField
      address = models.CharField(max_length=200)
      start_time = models.DateTimeField('events time')
      create_time = models.DateTimeField(auto_now=True)

      def __str__(self):
        return self.name

    # 嘉宾表
    class Guest(models.Model):
      event = models.ForeignKey(Event,on_delete=models.CASCADE)
      realname = models.CharField(max_length=64)
      phone = models.CharField(max_length=16)
      email = models.EmailField
      sign = models.BooleanField
      create_time = models.DateTimeField(auto_now=True)

      class Meta:

        # model内部类
        unique_together = ('event','phone')

      def __str__(self):
        return self.realname

    4.3 配置admin.py

    from django.contrib import admin
    from sign.models import Event,Guest
    # Register your models here.
    class EventAdmin(admin.ModelAdmin):
      list_display = ['id','name','status','address','start_time']
      search_fields = ['name']
      # list_filter = ['status']

    class GuestAdmin(admin.ModelAdmin):
      list_display = ['realname','phone','email','sign','create_time','event']
      search_fields = ['realname','phone']
      # list_filter = ['sign']

    admin.site.register(Event,EventAdmin)
    admin.site.register(Guest,GuestAdmin)

    4.4 数据库迁移

    e:guest>python manage.py makemigrations sign

    e:guest>python manage.py migrate

    5.结果

    登录失败

     登录成功

     

     

  • 相关阅读:
    洛谷 P6599 「EZEC-2」异或 题解
    线段树懒标记以及标记永久化的两种实现方法(模板)
    洛谷P3834 【模板】可持久化线段树 1 题解
    Codeforces 1012B Chemical table (思维+二分图)
    HDU 6386 Age of Moyu (最短路+set)
    CodeForces 739B Alyona and a tree (二分+树上差分)
    Codeforces 444C DZY Loves Colors (线段树)
    HDU 5441 Travel (离线dsu)
    Codeforces 1000E We Need More Bosses (边双连通+最长链)
    CodeForces 219D Choosing Capital for Treeland (树形DP)经典
  • 原文地址:https://www.cnblogs.com/Tester_Dolores/p/11880383.html
Copyright © 2020-2023  润新知