從頁面
This page isn’t working. If the problem continues, contact the site owner.
HTTP ERROR 405
從終端
Method Not Allowed (POST): /
Method Not Allowed: /
[20/Dec/2021 22:00:27] "POST / HTTP/1.1" 405 0
單擊頁面上傳后如何重定向到同一頁面。
form.html->包含在 sidebar.html-> 包含在 home.html
<form method = "POST" action='.' enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload</button>
</form>
視圖.py
from django.shortcuts import render
from .forms import UserProfileForm
def index(request):
print(request.POST)
return render(request,'home.html')
網址.py
from django.conf import settings
from django.urls import path
from django.views.generic.base import TemplateView # new
urlpatterns = [
path('', TemplateView.as_view(template_name='home.html'), name='home'),
]
uj5u.com熱心網友回復:
在你的 urls.py
改成:
path(' ', index, name = 'home'),
而且您還必須在 urls.py 中匯入您的視圖
uj5u.com熱心網友回復:
由于您正在重定向到同一頁面,因此我假設您在網頁上提供表單時也會發出 get 請求。但是,當頁面用作對 GET 請求的回應時,它不應在 POST 屬性中包含空字典。因此,它提供了一個錯誤。據我
def index(request):
if request.method == "POST" :
print(request.POST)
return render(request,'home.html')
應該解決問題
Acc,到 Django 檔案
一個請求可能會通過帶有空 POST 字典的 POST 傳入——例如,如果表單是通過 POST HTTP 方法請求的,但不包含表單資料。因此,您不應該使用 if request.POST 來檢查 POST 方法的使用情況;相反,使用 if request.method == "POST"
進一步參考 - https://docs.djangoproject.com/en/3.2/ref/request-response/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/389616.html
