简体   繁体   中英

why request.GET.get() returns 'None' and value of html element is not visible in URL

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HELLO</title>
</head>
<body>
    <form action="/removepunc", method="get">
        <input type="text",name='text' value="Hello,Django" />
        <input type="submit">
    </form>
</body>
</html>

views.py

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


def index(request):
    return render(request,'index.html')

def removepunc(request):
    print("Text is :"+request.GET.get('text','default'))
    return HttpResponse("Hello")

urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.index,name='index'),
    path('removepunc',views.removepunc,name='rempunc')

]

This is first screen after run the code

When I click on submit in Url it did not show "hello django"

Also in terminal it print default not "hello django"

There is a comma in your <input> box between "text" and name .

The <input> tag should thus look like:

<input type="text"  value="Hello,Django" />

not:

<input type="text",name='text' value="Hello,Django" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM