一、安装django
打开cmd命令行进行安装操作:
测试安装是否成功。出现以下图片内容表示django安装成功!
安装数据库软件
安装成功后启动:
二、新建Django工程
导入index.html代码
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<title>行业智慧能力平台</title>
<link rel="stylesheet" type="text/css" href="/static/assets/css/style2.0.css">
<style type="text/css">
ul li{font-size: 30px;color:#2ec0f6;}
.tyg-div{z-index:-1000;float:left;position:absolute;left:5%;top:20%;}
.tyg-p{
font-size: 14px;
font-family: 'microsoft yahei';
position: absolute;
top: 135px;
left: 60px;
}
.tyg-div-denglv{
z-index:1000;float:right;position:absolute;right:3%;top:10%;
}
.tyg-div-form{
background-color: #23305a;
width:300px;
height:auto;
margin:120px auto 0 auto;
color:#2ec0f6;
}
.tyg-div-form form {padding:10px;}
.tyg-div-form form input[type="text"]{
width: 270px;
height: 30px;
margin: 25px 10px 0px 0px;
}
.tyg-div-form form button {
cursor: pointer;
width: 270px;
height: 44px;
margin-top: 25px;
padding: 0;
background: #2ec0f6;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
border: 1px solid #2ec0f6;
-moz-box-shadow:
0 15px 30px 0 rgba(255,255,255,.25) inset,
0 2px 7px 0 rgba(0,0,0,.2);
-webkit-box-shadow:
0 15px 30px 0 rgba(255,255,255,.25) inset,
0 2px 7px 0 rgba(0,0,0,.2);
box-shadow:
0 15px 30px 0 rgba(255,255,255,.25) inset,
0 2px 7px 0 rgba(0,0,0,.2);
font-family: 'PT Sans', Helvetica, Arial, sans-serif;
font-size: 14px;
font-weight: 700;
color: #fff;
text-shadow: 0 1px 2px rgba(0,0,0,.1);
-o-transition: all .2s;
-moz-transition: all .2s;
-webkit-transition: all .2s;
-ms-transition: all .2s;
}
</style>
<body>
<div class="tyg-div">
<ul>
<li>让</li>
<li><div style="margin-left:20px;">数</div></li>
<li><div style="margin-left:40px;">据</div></li>
<li><div style="margin-left:60px;">改</div></li>
<li><div style="margin-left:80px;">变</div></li>
<li><div style="margin-left:100px;">生</div></li>
<li><div style="margin-left:120px;">活</div></li>
</ul>
</div>
<div id="contPar" class="contPar">
<div id="page1" style="z-index:1;">
<div class="title0">行业智慧能力平台</div>
<div class="title1">旅游、交通、气象、公共安全、大数据</div>
<div class="imgGroug">
<ul>
<img alt="" class="img0 png" src="/static/assets/image/page1_0.png">
<img alt="" class="img1 png" src="/static/assets/image/page1_1.png">
<img alt="" class="img2 png" src="/static/assets/image/page1_2.png">
</ul>
</div>
<img alt="" class="img3 png" src="/static/assets/image/page1_3.jpg">
</div>
</div>
<div class="tyg-div-denglv">
<div class="tyg-div-form">
<form action="" method="post">
<h2>登录</h2><p class="tyg-p">欢迎访问 智慧能力</p>
<div style="margin:5px 0px;">
<input type="text" name="user" placeholder="请输入账号..."/>
</div>
<div style="margin:5px 0px;">
<input type="text" name="password" placeholder="请输入密码..."/>
</div>
<button type="submit" >登<span style="width:20px;"></span>录</button>
</form>
</div>
</div>
<script type="text/javascript" src="/static/assets/js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="/static/assets/js/com.js"></script>
<!--[if IE 6]>
<script language="javascript" type="text/javascript" src="./assets/js/ie6_png.js"></script>
<script language="javascript" type="text/javascript">
DD_belatedPNG.fix(".png");
</script>
<![endif]-->
</body>
</html>
修改settings.py文件:
"""
Django settings for test1 project.
Generated by 'django-admin startproject' using Django 2.0.13.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/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.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'bfvu4_t#yerncy85a)0p_$yt^slo+nm)n7nd9!&e3g-ivw&lyc'
# 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',
'user.apps.UserConfig',
]
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 = 'test1.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'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 = 'test1.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tong',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '3306',
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/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.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(__file__),'static')
# 设置图片等静态文件的路径
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static').replace('\\', '/'),
('css',os.path.join(STATIC_ROOT,'css').replace('\\','/') ),
('js',os.path.join(STATIC_ROOT,'js').replace('\\','/') ),
('images',os.path.join(STATIC_ROOT,'images').replace('\\','/') ),
('upload',os.path.join(STATIC_ROOT,'upload').replace('\\','/') ),)
修改models.py文件:
from django.db import models
# Create your models here.
class User(models.Model):
username = models.CharField(max_length= 32)
password = models.CharField(max_length= 64)
修改urls.py文件:
"""test1 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from user import views
urlpatterns = [
path('admin/', admin.site.urls),
path('login/', views.index),
path('login1/', views.index1),
]
修改views.py文件:
from django.shortcuts import render
from user import models
# Create your views here.
def index(request):
if request.method == "GET":
return render(request, 'index.html')
if request.method == "POST":
u = request.POST.get('user')
y = request.POST.get('password')
models.User.objects.create(username=u,password=y)
return render(request, 'index1.html')
def index1(request):
return render(request, 'index1.html')
三、运行结果如图