[英]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.
目前,我已经对其进行了设置,以便在html页面中要求他们提供信息,然后该页面调用用户身份验证页面的url,然后转到视图文件,然后在该文件中检查数据库是否匹配。 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: HTML页面:
function getUserInfo() {
username = document.getElementById("username").value;
password = document.getElementById("password").value;
"{% url 'user_authentication' %}"
}
urls.py: urls.py:
urlpatterns = [
url(r'^user_authentication/$', views.user_authentication, name='user_authentication'),
]
views.py: 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. 我尝试使用GET,但这不起作用。 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.
我还没有数据库设置,所以现在我只是对用户名和密码要求进行硬编码以获取下一个URL。
You just need a simple HTML form. 您只需要一个简单的HTML表单。
<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). 这将在GET参数中发送您的用户名和密码(显然,在实际站点中,您将使用POST并从Django中的
request.POST
获取数据)。
Note, this is all basic web development, and nothing specific to Django; 注意,这是所有基本的Web开发,并且没有特定于Django的内容。 you may benefit from doing an introductory HTML tutorial.
您可能会从入门HTML教程中受益。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.