无偿分享:有点基础的可以自取
代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link href="/static/css/bootstrap.min.css" rel="stylesheet"> </head> {% csrf_token %} <body style=" background: url(/static/image/3.jpg) no-repeat center center fixed; background-size: 100%;"> <form method="post" action="" cellpadding="1" cellspacing="0" border="1" οnsubmit="return sub1()" > {% csrf_token %} <div class="modal-dialog" style="margin-top: 10%;"> <div class="modal-content" style="opacity:0.7;background-color: antiquewhite;" > <div class="modal-header"> <h4 class="modal-title text-center" id="myModalLabel"><h1 style="color: aqua; text-align: center;">登录页面</h1></h4> </div> <div class="modal-body" id = "model-body"> <div class="form-group"> <input type="text" id="name" class="form-control" placeholder="用户名" autocomplete="off" name="logion_name"> </div> <div class="form-group"> <input type="password" id="password" class="form-control" placeholder="密码" autocomplete="off" name="logion_password"> </div> </div> <div class="modal-footer"> <div class="form-group"> <button type="submit" class="btn btn-primary form-control">登录</button> </div> <div class="form-group"> <a href="/loog_add2"><button type="button" class="btn btn-default form-control" href="/loog_add">注册</button></a> </div> </div> <!-- 提示输入的信息错误的显示的代码--> <span style="color: red; background-color: white;" class="badge badge-pill badge-warning"><h4>{{ error_msg }}</h4></span> </div><!-- /.modal-content --> </div><!-- /.modal --> </form> <script> {#设计一个函数来阻止没有输入信息时不能提交功能#} function sub1(){ if(document.getElementById("name").value == ""){ alert("用户名不能为空!"); return false; } if(document.getElementById("password").value == ""){ alert("密码不能为空!"); return false; } } </script> </body> </html>
最核心得代码:
from django.shortcuts import render, redirect, HttpResponsePermanentRedirect, HttpResponse from django import http # Create your views here. from panshaoduo1.models import info # 登录功能 def login(request): sho = "" if request.method == "POST": # 判断和接收数据的类型为POST,数据类型有两种是GET和POST。 name = request.POST.get("name") # 接收数据 pw = request.POST.get("pw") np = info.objects.filter(name=name) # 查询条件 if np: return redirect("/h1") # 路由路径 sho = "你输入的用户名或者密码有误" return render(request, "login.html", {"sho": sho}) # 静态路径加参数 # 显示全部商品部分 def index(request): show = info.objects.all() # 查询全部的 return render(request, "index.html", {"show": show}) # 静态路径加参数 # 添加功能部分 def tianjia(request): if request.method == "POST": name1 = request.POST.get("name") price = request.POST.get("price") cucun = request.POST.get("cucun") xaioliang = request.POST.get("xaioliang") print(name1, price) info.objects.create(name=name1, price=price, cucun=cucun, xaioliang=xaioliang) # 添加的信息的用create这个函数,有两个函数分别是create和save return redirect("/h1") # 路由路径 xianshi = "你没有添加" return render(request, "index.html", {"xiaoshi": xianshi}) # 静态路径加参数 # 修改功能部分 def xiugai(request): if request.method == "POST": id = request.POST.get("id") name1 = request.POST.get("name") price = request.POST.get("price") cucun = request.POST.get("cucun") xaioliang = request.POST.get("xaioliang") print(name1, price) ii = info.objects.get(id=id) # 查询条件 ii.id = id ii.name = name1 ii.price = price ii.cucun = cucun ii.xaioliang = xaioliang ii.save() return redirect("/h1") xianshi = "你没有添加" return render(request, "index.html", {"xiaoshi": xianshi}) # 删除功能部分 def delete(request, id): dee = info.objects.filter(id=id) # 查询的条件 dee.delete() return redirect("/h1") # 查询功能部分 def chaxun(request): if request.method == "POST": name = request.POST.get("name") show = info.objects.filter(name=name) # 查询的条件,查询函数一共有四个,分别是all(),filter(),get(),exclude() return render(request, "index.html", {"show": show}) xianshi = "你没有添加" return render(request, "index.html", {"xiaoshi": xianshi})
跳转路径:后面会给源代码:需要直接下载。
主路由的配置要设好,不然,下载文件不可行
""" Django settings for panshaoduo project. Generated by 'django-admin startproject' using Django 2.2.3. For more information on this file, see https://docs.djangoproject.com/en/2.2/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.2/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '=xexx=1+26bj2$esd**jp4yw8pbxki8+fq=qs8(^!4g*1+*l+e' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'panshaoduo1', # 添加应用文件1 'panshaoduo2', # 添加应用文件2 ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.middleware.csrf.CsrfViewMiddleware', # 如果提交数据是报错是权限的就把这个注释掉 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'panshaoduo.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ["./html"], # 静态路径有n多种方式,但在俺认知这个是最简单的看你喜欢。 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'panshaoduo.wsgi.application' # Database # https://docs.djangoproject.com/en/2.2/ref/settings/#databases DATABASES = { 'default': { # 数据库连接模块,这个没有什么好解释。细观 'ENGINE': 'django.db.backends.mysql', 'NAME': 'cs', 'HOST': '127.0.0.1', 'PORT': '3306', 'USER': 'root', 'PASSWORD': '123456', } } # Password validation # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/2.2/topics/i18n/ LANGUAGE_CODE = 'Zh-Hans' # 改成'Zh-Hans'后台编译成中文模式 TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = False # 本地时间的转变如:excel文件的时间转变要用到 # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/2.2/howto/static-files/ STATIC_URL = '/static/' STATICFILES_DIRS = [ # 图片路径 /static/images/ os.path.join(BASE_DIR, 'static') ] 数据库表如下: 需要源文件的自取 数据库文件 阿里云盘分享 (alipan.com)https://www.alipan.com/s/hkaS4G23RBN 代码文件: 阿里云盘分享 (alipan.com)https://www.alipan.com/s/pYpTNKX8sqh
太多代码了放不下,需要自己下载把,不限时也不需要钱。
运行结构如如下: