Django---Django返回用戶輸入數據


  前面寫了關於HTML和Django結合的文章,通過視圖與HTML結合,然后加上urls渲染返回給用戶瀏覽器。很明顯我們都能看到這些僅僅是靜態HTML,那如何通過Django創建動態的HTML呢?

動態的頁面

我們先通過一個例子來簡單了解下。

1、首先通過在views.py文件中添加一些動態的數據

# Views.py

from django.http import HttpResponse
import datetime

# 創建動態數據
def time(request):
    now = datetime.datetime.now()
    html = '現在的時間為%s'%now
    return HttpResponse(html)

2、通過urls.py文件渲染給瀏覽器

# urls.py

from django.contrib import admin
from django.urls import path
from Anjing import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('time/', views.time),    # 添加對應的路徑
]

3、啟動Django服務

可以看到顯示的是當前數據,重新刷新,數據就會變化。

通過上面例子,已經創建好了一個小小的動態頁面,當然這次要說的不僅僅是這樣,是需要用戶的數據交互,通過用戶輸入的內容,並返回給用戶

接收並返回數據

當我們登錄博客的時候,博客都會返回給我們一個用戶名,而這個用戶名是自己設置的,這里安靜寫一個簡單的登錄網站,網站直接返回登錄的名稱

1、編寫登錄頁面HTML,添加路徑渲染,添加視圖返回

在templates目錄下創建一個login.html(這個頁面為登錄頁面)

# login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登錄</title>
</head>
<body>
    <h1>
        <p style="text-align:center" font size="2">歡迎來到安靜的博客:</p>


    </h1>
    <h1>
        <p style="text-align:center">請輸出賬號密碼:</p>
    </h1>
    <form action="/index/" method="post"><p style="text-align:center">用戶:<input type="text" name="username" /><br />
        </p>
        <p style="text-align:center">密碼:<input type="password" name="password" /><br />
        <input type="submit" value="提交" />
    </form>

</body>
</html>

在templates目錄下創建一個index.html文件(登錄后跳轉的頁面)

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
    <title>安靜博客樂園</title>
</head>
<body>
<center>
    <li>歡迎您:  
    </li>
</center>
</ul>
</body>
</html>

在urls.py文件中添加對應的路徑,然后啟動服務,在瀏覽器中輸入對應的路徑查看

# urls.py

from django.contrib import admin
from django.urls import path
from Anjing import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', views.login),
    path('index/', views.index),
]

在views.py文件中添加視圖返回內容

# views。py

from django.shortcuts import render

def index(request):
    if  request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        return render(request, 'index.html')

全部工作都准備完成了,然后啟動服務,打開瀏覽器,查看我們的頁面渲染

 

 

我們在輸入框內隨便輸入用戶信息點擊提交

發現出現了403,什么情況,我們上面已經對頁面進行了渲染,應該不會出錯哈。

 大家不要着急,其實這個是正常的,這個屬於Djaongo有一個跨站請求保護的機制,我們可以從報錯中發現需要我們加入 {% csrf_token %}

# login.html

<!
DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首頁</title> </head> <body> <h1> <p style="text-align:center" font size="2">歡迎來到安靜的博客:</p> </h1> <h1> <p style="text-align:center">請輸出賬號密碼:</p> </h1> <form action="/index/" method="post"> {% csrf_token %} <p style="text-align:center">用戶:<input type="text" name="username" /><br /> </p> <p style="text-align:center">密碼:<input type="password" name="password" /><br /> <input type="submit" value="提交" /> </form> </body> </html>

當然了我們也要在返回的頁面加入一些參數和修視圖中的代碼

# index.html
<html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <head> <title>安靜博客樂園</title> </head> <body> <center> <li>歡迎您, <tbody> <tr> <td>{{data}}</td> </tr> </tbody> </li> </center> </ul> </body> </html>

修改views.py視圖文件

# views.py

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    if  request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        return render(request, 'index.html', {'data':username})

這個時候我們在此啟動Django服務,然后輸入對應數據點擊提交按鈕,發現數據已經全部接收了。

我們通過了登錄的小案例,大致的了解到了數據交互和數據接收應該如何進行,主要的一個知識,我理解到Django的一個跨站請求保護機制。多次使用,就能孰能生巧了

 

 

如果感覺安靜寫的對您有幫助,請點個關注,如果哪里有不懂的或者不明白的,可以下方留言,看到后第一時間回復


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM