Home > OS >  Compare Django-HTML form input with the elements in a list
Compare Django-HTML form input with the elements in a list

Time:11-22

I have a list of usernames and I want to compare the form input with the elements of the list.

Suppose I have a list, say, listA = ["abc", "def", "ghi"]

and the HTML form,

<form action="#" class="signin-form">
     <div class="form-group mb-3">
          <label class="label" for="name">Username</label>
          <input type="text" class="form-control" placeholder="Username required>
     </div>
     <div hljs-attr">form-group">
          <button type="submit" class="form-control btn btn-primary submit">Sign In</button>
     </div>

I want to compare the input from the elements in the list.

CodePudding user response:

Make a dedicated page to handle the comparing and add that page as the action in your form.

Then in the view for that page, take in the data entered by the user using either get or post then you can compare the data in any way you want.

CodePudding user response:

You have to add name argument and preferably method of POST to your input so then you can use something like this:

HTML:

<form action="/" method="POST" class="signin-form">
     <div class="form-group mb-3">
          <label class="label" for="name">Username</label>
          <input name="username" type="text" class="form-control" placeholder="Username required>
     </div>
     <div hljs-attr">form-group">
          <button type="submit" class="form-control btn btn-primary submit">Sign In</button>
     </div>
</form>

Python:

listA = ["abc", "def", "ghi"]

if 'username' in request.POST:
    if request.POST['username'] in listA:
        # do something
  • Related