简体   繁体   English

如何在Django中传递参数值?

[英]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.

相关问题 如何将var值传递给javascript中的参数? - How to pass var values to a parameter in javascript? 如何将多个值传递给一个参数 - How to pass multiple values to one parameter 如何将参数传递给自治函数并获取值? - How to pass the parameter to autonomous function and get the values? 如何将 django 上下文值传递给 javascript 变量? - How to pass django context values to the javascript variable? 使用for循环时如何从Django模板将参数传递给javascript - How to pass parameter to javascript from Django template while using for loop 如何仅将键及其值作为javascript对象的参数传递 - how to pass only keys and its values as parameter of javascript object 如何创建一个从数组中收集值以作为参数传递的计数器? - how to create a counter that collects values from an array to pass as a parameter? 如何在Ajax Actionlink的路由值中将javascript函数作为参数传递 - How to pass javascript function as a parameter in the route values of Ajax Actionlink 如何传递参数并从angular js中的自定义指令获取值? - how to pass parameter and get values from custom directive in angular js? 如何在url中传递参数? - how to pass parameter in a url?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM