pyCharm django 中新加app

时间:2022-08-13 15:27:02

1.在manage.py@djangotest中输入命令:

  1.startapp   realnameauth   ---新建一个app,名字为realnameauth

2.在 django 项目中的settings.py文件中配置应用:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'realnameauth ' //新建的app名字 ]

3.把 realnameauth 应用要用到的html页面放到django项目中的templates文件下,命名为realnameauth_form.html

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>real-name-auth json</title>
<link rel="stylesheet" href="/static/css/style.css"> </head>
<body>
<form action="{% url 'go_realnameauth' %}" method="post" class="smart-green">
<div id="box">
<br/>
mid: <input id="mid" name="mid" type="text"/>
<br/><br/>
authKey: <input id="authKey" name="authKey" type="text"/>
<br/><br/>
reqId: <input id="reqId" name="reqId" type="text"/> <br/><br/>
verifyType: <input id="verifyType" name="verifyType" type="text"/>
<br/><br/>
params:
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>idNo: <input id="idNo" name="idNo" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>name: <input id="name" name="name" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>bcNo: <input id="verify-bcNo" name="verify-bcNo" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>mobile: <input id="mobile" name="mobile" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>isHandheld: <input id="isHandheld" name="isHandheld" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>realImg: <input id="realImg" name="realImg" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>gender: <input id="gender" name="gender" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>nation: <input id="nation" name="nation" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>birthday: <input id="birthday" name="birthday" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>address: <input id="address" name="address" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>issuingAuthority: <input id="issuingAuthority" name="issuingAuthority" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>validDate: <input id="validDate" name="validDate" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>expireDate: <input id="expireDate" name="expireDate" type="text"/>
<br/><br/>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>checkValidityPeriod: <input id="checkValidityPeriod" name="checkValidityPeriod" type="text"/> <br/><br/>
<br/><br/>
idImgSide: <input id="idImgSide" name="idImgSide" type="text"/>
<br/><br/>
idImg: <input id="idImg" name="idImg" type="text"/>
<br/><br/>
bcNo: <input id="attr-bcNo" name="attr-bcNo" type="text"/>
</div>
<label>
<span>&nbsp;</span>
<input type="submit" class="button" value="提交"/>
</label>
{% csrf_token %} <!--保证form表提交,是一种安全机制-->
</form>
</body>
</html>

4.在 django 项目中的static/css/style.css,用于配置htm页面显示的样式:

        .smart-green {
margin-left: auto;
margin-right: auto;
max-width: 500px;
background: #F8F8F8;
padding: 30px 30px 20px 30px;
font: 12px Arial, Helvetica, sans-serif;
color: #;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
} .smart-green h1 {
font: 24px "Trebuchet MS", Arial, Helvetica, sans-serif;
padding: 20px 0px 20px 40px;
display: block;
margin: -30px -30px 10px -30px;
color: #FFF;
background: #9DC45F;
text-shadow: 1px 1px 1px #;
border-radius: 5px 5px 0px 0px;
-webkit-border-radius: 5px 5px 0px 0px;
-moz-border-radius: 5px 5px 0px 0px;
border-bottom: 1px solid #89AF4C;
} .smart-green h1 > span {
display: block;
font-size: 11px;
color: #FFF;
} .smart-green label {
display: block;
margin: 0px 0px 5px;
} .smart-green label > span {
float: left;
margin-top: 10px;
color: #5E5E5E;
} .smart-green input[type="text"], .smart-green input[type="email"], .smart-green textarea, .smart-green select {
color: #;
height: 30px;
line-height: 15px;
width: %;
padding: 0px 0px 0px 10px;
margin-top: 2px;
border: 1px solid #E5E5E5;
background: #FBFBFB;
outline: ;
-webkit-box-shadow: inset 1px 1px 2px rgba(, , , 0.2);
box-shadow: inset 1px 1px 2px rgba(, , , 0.2);
font: normal 14px/14px Arial, Helvetica, sans-serif;
} .smart-green textarea {
height: 100px;
padding-top: 10px;
} .smart-green .button {
background-color: #9DC45F;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-border-radius: 5px;
border: none;
padding: 10px 25px 10px 25px;
color: #FFF;
text-shadow: 1px 1px 1px #;
} .smart-green .button:hover {
background-color: #80A24A;
} .error-msg{
color: red;
margin-top: 10px;
}
.success-msg{
color: #80A24A;
margin-top: 10px;
margin-bottom: 10px;
}

5.在 django 项目中的settings.py文件中配置静态文件调用路径:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/ STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]

6.在realnameauth文件夹下的views.py,用于显示html页面:

from django.shortcuts import render

from .models import FieldMessages
# Create your views here.
def InsertData(request):
#添加数据
if request.method == "POST":
mid = request.POST.get('mid','')
authKey = request.POST.get('authKey','')
reqId = request.POST.get('reqId','')
verifyType = request.POST.get('verifyType','')
params_idNo = request.POST.get('idNo','')
params_name = request.POST.get('name','')
params_bcNo = request.POST.get('verify-bcNo','')
params_mobile = request.POST.get('mobile','')
params_isHandheld = request.POST.get('isHandheld','')
params_realImg = request.POST.get('realImg','')
params_gender = request.POST.get('gender','')
params_nation = request.POST.get('nation','')
params_birthday = request.POST.get('birthday','')
params_address = request.POST.get('address','')
params_issuingAuthority = request.POST.get('issuingAuthority','')
params_validDate = request.POST.get('validDate','')
params_expireDate = request.POST.get('expireDate','')
params_checkValidityPeriod = request.POST.get('checkValidityPeriod','')
idImgSide = request.POST.get('idImgSide','')
idImg = request.POST.get('idImg','')
bcNo = request.POST.get('attr-bcNo','') field_messages = FieldMessages()
field_messages.mid = mid
field_messages.authKey = authKey
field_messages.reqId = reqId
field_messages.verifyType = verifyType
field_messages.params_idNo = params_idNo
field_messages.params_name = params_name
field_messages.params_bcNo = params_bcNo
field_messages.params_mobile = params_mobile
field_messages.params_isHandheld = params_isHandheld
field_messages.params_realImg = params_realImg
field_messages.params_gender = params_gender
field_messages.params_nation = params_nation
field_messages.params_birthday = params_birthday
field_messages.params_address = params_address
field_messages.params_issuingAuthority = params_issuingAuthority
field_messages.params_validDate = params_validDate
field_messages.params_expireDate = params_expireDate
field_messages.params_checkValidityPeriod = params_checkValidityPeriod
field_messages.idImgSide = idImgSide
field_messages.idImg = idImg
field_messages.bcNo = bcNo
field_messages.save()
return render(request, 'realnameauth_form.html')


7.在django 项目中的urls.py配置url:

"""djangotest 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 message.views import getform
from realnameauth.views import InsertData #导入realnameauth的InsertData方法 from realnameauth.views import ReadData #导入realnameauth的ReadData方法 urlpatterns = [
# path('admin/', admin.site.urls),
path('form/',getform, name='go_form'),
path('realnameauth/',InsertData, name='go_realnameauth'), #realnameauth配置路径
path('realnameauth_read/', ReadData, name='go_realnameauth_read') # realnameauthread配置路径
]
 

8.在realnameauth文件夹下的models.py写入models类,来对数据库进行操作:

from django.db import models

# Create your models here.
class FieldMessages(models.Model):
mid = models.CharField(max_length=, verbose_name=u"传入平台分配的商户id")
authKey = models.TextField(verbose_name=u"应用签名信息")
reqId = models.CharField(max_length=, verbose_name=u"唯一请求流水号")
verifyType = models.CharField(max_length=, verbose_name=u"身份验证类型")
params_idNo = models.CharField(max_length=, verbose_name=u"公民身份证号")
params_name = models.CharField(max_length=, verbose_name=u"姓名")
params_bcNo = models.CharField(max_length=, verbose_name=u"银行卡号")
params_mobile = models.CharField(max_length=, verbose_name=u"手机号码")
params_isHandheld = models.CharField(max_length=, verbose_name=u"是否为手持身份证照片")
params_realImg = models.TextField(verbose_name=u"真实照片转换为base64格式。base64(原始图片二进制流)。")
params_gender = models.CharField(max_length=, verbose_name=u"")
params_nation = models.CharField(max_length=, verbose_name=u"")
params_birthday = models.CharField(max_length=, verbose_name=u"")
params_address = models.CharField(max_length=, verbose_name=u"")
params_issuingAuthority = models.CharField(max_length=, verbose_name=u"")
params_validDate = models.CharField(max_length=, verbose_name=u"")
params_expireDate = models.CharField(max_length=, verbose_name=u"")
params_checkValidityPeriod = models.CharField(max_length=, verbose_name=u"是否验证身份证有效期")
idImgSide = models.TextField(verbose_name=u"身份证图片正反面")
idImg = models.TextField(verbose_name=u"身份证图片数据")
bcNo = models.CharField(max_length=, verbose_name=u"银行卡号") class Meta:
verbose_name = "实名认证"

9.运行Task命令:

  1.makemigrations realnameauth   ---预建表

  2.migrate realnameauth   ---把表提交到数据库