通過網頁向DB內添加資料

來源:互聯網
上載者:User

標籤:port   alt   att   inpu   AC   model   render   import   form   

結構如下:

mysite-
|
|--monitor
|    |
|    |--views
|      |--urls
|--mysite(預設建立)
|    |
|    |--settings
|      |--urls
|--templates
|    |
|    |--monitor
|          |--index.html

修改mysite.settings.py注釋掉以下行

#‘django.middleware.csrf.CsrfViewMiddleware‘,

修改mysite.urls.py

urlpatterns = [    path(‘add/‘,views.add),   #添加輸入資料    path(‘del/‘,views.dell)  #刪除所有資料]

編輯monitor.views內容

from django.shortcuts import renderfrom django.http import HttpResponsefrom django.template import  loaderfrom monitor.models import UserInfo as bdef add(request):    if request.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()    return  render(request,‘monitor/index.html‘,{"lion":k})def dell(request):    b.objects.all().delete()    k = b.objects.all()    return render(request, ‘monitor/index.html‘, {"lion": k})

最後修改monitor.index

<!doctype html><html lang="zh-CN"><head>    <title>django</title></head><body><form action="/add/" method="post">    <p><input name="Username" type="text"></p>    <p><input name="Password" type="text"></p>    <p><input name="Age" type="text"></p>    <p><input value="提交" type="submit"></p></form><table border="1">    <thead>        <tr>            <th>使用者名稱</th>            <th>密碼</th>            <th>年齡</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

此時訪問127.0.0.1:8000/{add,del}即可添加,刪除資料內容

 

通過網頁向DB內添加資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.