簡體   English   中英

django 表格沒有 go 內柱

[英]django form doesn't go inside post

我只是想能夠讀取用戶在服務器表單上輸入的值。 我注意到它永遠不會進入視圖 function 的這一部分

 if request.method == 'POST':

如果我錯過了什么,請告訴我。 我已經包含了視圖、url、表單、html 文件的代碼片段:

urls.py=>
urlpatterns = [
    path('create',views.create),
    path('create/',views.create),
    path('download', views.download),
]

checklistform.py=>
class ContactForm(forms.Form):
    name = forms.CharField()

create.html=>
<!DOCTYPE html>
<html lang="en>
<head>
</head>
<body>
 <form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="OK">
 </form>
</body>
</html>

views.py =>
from django.shortcuts import render, HttpResponseRedirect, HttpResponse, redirect
from .checklistform import ContactForm

def create(request):
    print("letssee")
    print(request.method)
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            print(name)

    form=ContactForm()
    return render(request, 'create.html', {'form': form})

Output 見於 pycharm => (我們可以看到它永遠不會進入 if)

[15/Aug/2020 23:47:03] "GET / HTTP/1.1" 404 2038
letssee
GET
[15/Aug/2020 23:47:08] "GET /imschecklist/create HTTP/1.1" 200 364

謝謝,尼克

<html lang="en>更改為<html lang="en"> 。你錯過了雙引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM