The structure is as follows:
mysite-
|
|--monitor
| |
| |--views
| |--urls
|--mysite (created by default)
| |
| |--settings
| |--urls
|--templates
| |
| |--monitor
| |--index.html
Modify mysite.settings.py comment down the line
# ' Django.middleware.csrf.CsrfViewMiddleware ',
Modify mysite.urls.py
Urlpatterns = [ path ('add/', Views.add), # Add input data path ('del/', Views.dell) # Delete all data ]
Edit Monitor.views Content
fromDjango.shortcutsImportRender fromDjango.httpImportHttpResponse fromDjango.templateImportLoader fromMonitor.modelsImportUserInfo as BdefAdd (Request):ifRequest.method = ="POST": U=request. post['Username'] P=request. post['Password'] a=int (Request. post[' Age']) b.objects.create (Username=u,password=p,age=a) K=B.objects.all ()returnRender (Request,'monitor/index.html',{"Lion": k})defDell (Request): B.objects.all (). Delete () K=B.objects.all ()returnRender (Request,'monitor/index.html', {"Lion": k})
Last modified Monitor.index
<!DOCTYPE HTML><HTMLLang= "ZH-CN"><Head> <title>Django</title></Head><Body><formAction= "/add/"Method= "POST"> <P><inputname= "Username"type= "text"></P> <P><inputname= "Password"type= "text"></P> <P><inputname= "Age"type= "text"></P> <P><inputvalue= "Submit"type= "Submit"></P></form><TableBorder= "1"> <thead> <TR> <th>User name</th> <th>Password</th> <th>Age</th> </TR> </thead> <tbody>{% for item in lion%}<TR> <TD>{Item.} Username}}</TD> <TD>{Item.} Password}}</TD> <TD>{Item.} Age}}</TD> </TR>{% endfor%}</tbody></Table></Body></HTML>
View Code
Now access 127.0.0.1:8000/{add,del} to add, delete data content
Add data to DB via Web page