Add data to DB via Web page

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.