简体   繁体   中英

How to pass parameter values in Django?

My goal is to have the user enter their username and password which is then checked in a database to see if it is correct or not to allow them to login or be denied access. Currently, I have it setup so that they are asked for their information in an html page that then calls the url of the user authentication page which then goes to the views file where it then checks the database for a match. I have some code written up, but I keep running into errors and I can't seem to figure out how to fix it.

Is this even the right setup for checking user information even the correct way to go about it in the first place or is there another way that is more effective in doing so? Here's my code as of now.

HTML Page:

function getUserInfo() {
  username = document.getElementById("username").value;
  password = document.getElementById("password").value;

  "{% url 'user_authentication' %}"

}

urls.py:

urlpatterns = [
  url(r'^user_authentication/$', views.user_authentication, name='user_authentication'),
]

views.py:

def user_authentication(request):
  username = request.GET.get('username')
  password = request.GET.get('password')
  if (username == "Stack" and password == "Overflow"):
    return render(request, 'next url goes here')

I tried to use GET, but that doesn't work. I don't have the database setup yet so for now I'm just hardcoding in the username and password requirements to get to the next url.

You just need a simple HTML form.

 <form action="{% url 'user_authentication' %}" method="GET"> <div> <label for="username">Username</label> <input name="username" id="username"> </div> <div> <label for="password">Password</label> <input name="password" id="password"> </div> <div> <input type="submit"> </div> </form> 

This will send your username and password in the GET params (obviously in a real site you would use POST and get the data from request.POST in Django).

Note, this is all basic web development, and nothing specific to Django; you may benefit from doing an introductory HTML tutorial.

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