简体   繁体   中英

Request.POST.get not working for me in django, returning default value

I am trying to get input from a html form in django , python code below:

def add(request):
   n = request.POST.get('Username', 'Did not work')
   i = Item(name=n,price=0)
   i.save()
   return render(request,'tarkovdb/test.html')

Second pic is my html code:

<html> 
    <head> 
        <meta charset="UTF-8"› 
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384—Vkoo8x4CGs0 OPaXtkKtu6ug5T0eNV6gBiFeWPGFN9Muh0f23Q9Ifjh" crossorigin="anonymous"> 
        <title>Tarkov Database Web App</title> 
    </head> 
    <body> 
        <h1>This is the page to add items</h1> 
            <li><a href="{% url 'tarkovdb:index' s'6}">List of Items in DataBase</a></li> 

<form> 
    <div class="form—group"> 
        <label for&username'> Username: </lable> 
        <input type&text' name='Username' id.'username'> <br><br> 
    </div> 
        <button type="submit" class="btn btn—primary">Submit</button> 
</form> 

You need to set your method attribute to " post " on your HTML form tag . Like this:

<form method="post">
    <!-- your fields here -->
</form>

Otherwise you'll be sending a GET request, which is the default value of the method attribute.

PD.: Please paste your code, make it easy for the community to help you. Otherwise you'll get down voted.

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