對于一個專案,我想使用用戶通過兩個輸入加載的兩個檔案,以應用外部腳本。
我的問題是我收到一個錯誤,因為 Django 似乎無法找到這些輸入。所以我有一個空物件而不是檔案。
這是我的代碼:
主頁.html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>Home</h1>
<form action="/external/" method="post" enctype="multipart/form-data">
{% csrf_token %}
Input xlsx file :<br><br>
<input type="file" name="file1" accept=".xml" required><br>
<input type="file" name="file2" accept=".xml" required><br>
<input type="submit" value="Valider"><br>
</form>
</body>
</html>
視圖.py
from django.shortcuts import render
from .scripts.extScript import *
def home(request):
return render(request, 'home.html')
def external(request):
f1=request.FILES.get('file1')
f2=request.FILES.get('file2')
extScript(f1,f2)
return render(request,'home.html')
網址.py
from django.contrib import admin
from django.urls import include, path
from .views import *
urlpatterns=[
path('',home,name="home"),
path('external/',external, name="external")
]
建筑學:
DjangoProject
|
-views.py
-urls.py
-scripts
|
-extScript.py
templates
|
-home.html
并且錯誤指定 f1 型別為 < NoneType >
我想指出,我試圖將 f1=request.FILES['file1'] 發送給我,'file1' is not found :raise MultiValueDictKeyError(key)。
如果有人有想法,我無法解決這個問題,我覺得一切都很好。此外,我在另一個專案上執行了相同的功能,并且效果很好,所以我不知道。
謝謝!
uj5u.com熱心網友回復:
那是因為您正在發送帶有“POST”請求的檔案,您應該在視圖中收聽該請求,因為它首先從沒有檔案的 GET 視圖開始。所以代碼必須如下所示:
def external(request):
if request.POST:
f1=request.FILES.get('file1')
f2=request.FILES.get('file2')
extScript(f1,f2)
return render(request,'home.html')
uj5u.com熱心網友回復:
我真的不知道,但也許您需要將提交更改為按鈕<button type="submit">Upload</button>而不是<input>.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/389423.html
標籤:Python 姜戈 xml django-views 蟒蛇请求
