使用 Django 实现用户无效消息

发布于 2024-10-27 20:29:39 字数 2117 浏览 1 评论 0原文

我创建了一个 Django 应用程序。它在同一个registrationForm.html 上有用户登录/注册。登录功能现在工作正常。一旦用户提供了正确的用户名(此处为电子邮件 ID)和密码,用户将被重定向到另一个页面(logedIn.html),显示一条消息“用户已登录”,并且当用户无效(用户名或密码错误)时,该页面再次呈现到该页面(registrationForm.html)。现在我想在该页面上显示一条消息“检查您的用户名或密码”。作为一个 Django 新手,我无法做到这一点。有人可以帮忙做这件事吗?我将在这里粘贴我的代码。

views.py

def registrationForm(request):
    if request.method == "POST":  
        firstName = request.POST.get("firstName")
        lastName = request.POST.get("lastName")
        email = request.POST.get("email")
        password = request.POST.get("password")
        sex = request.POST.get("sex")
        birthday = request.POST.get("birthday")
        print request.POST.get("sex")
        UniversityDetails(firstName=firstName,lastName=lastName,email=email,password=password,sex=sex,birthday=birthday).save()
        return render_to_response('registrationForm.html')
    return render_to_response("registrationForm.html")

def login(request):
    if request.POST:            
        email=request.POST.get("username")
        password = request.POST.get("password")                     
        user = UniversityDetails.objects.filter(email=email,password=password)          
        if(not user):
            return render_to_response("registrationForm.html")
        else:
            return render_to_response("logedIn.html")

html

<div align="center">
<form name="userInputForm" method="POST" id="myFormid" action="http://10.1.0.90:8080/login/">
<div style="float:left;width:100%;">
  <p style="float:left;margin-right:10px;width:auto;"><label style="float:left;">Email id</label><br/> <input type="text" name="username" size="25" /></p>
  <p style="float:left;margin-right:10px;width:auto;"><label style="float:left;">Password</label><br/><input type="password" name="password" size="25" /></p>
 </div> 
    <p style="clear:both;float:left;"><input type="submit" value="Log in" /></p>
</div>
</form>

I have created a django app. It has got a user login/registration on the same registrationForm.html. The login functionality is working fine now. Once the user gives the correct username (here email id) and password the user is redirected to another page(logedIn.html) showing a message 'User logged in', and when the user is invalid (wrong username or password) , the page is again rendered to that page(registrationForm.html). Now i want to show a message on that page saying "Check your username or password" . Being a django newbie i am not able to do it. Can somebody help to do this. I will paste my code here.

views.py

def registrationForm(request):
    if request.method == "POST":  
        firstName = request.POST.get("firstName")
        lastName = request.POST.get("lastName")
        email = request.POST.get("email")
        password = request.POST.get("password")
        sex = request.POST.get("sex")
        birthday = request.POST.get("birthday")
        print request.POST.get("sex")
        UniversityDetails(firstName=firstName,lastName=lastName,email=email,password=password,sex=sex,birthday=birthday).save()
        return render_to_response('registrationForm.html')
    return render_to_response("registrationForm.html")

def login(request):
    if request.POST:            
        email=request.POST.get("username")
        password = request.POST.get("password")                     
        user = UniversityDetails.objects.filter(email=email,password=password)          
        if(not user):
            return render_to_response("registrationForm.html")
        else:
            return render_to_response("logedIn.html")

html

<div align="center">
<form name="userInputForm" method="POST" id="myFormid" action="http://10.1.0.90:8080/login/">
<div style="float:left;width:100%;">
  <p style="float:left;margin-right:10px;width:auto;"><label style="float:left;">Email id</label><br/> <input type="text" name="username" size="25" /></p>
  <p style="float:left;margin-right:10px;width:auto;"><label style="float:left;">Password</label><br/><input type="password" name="password" size="25" /></p>
 </div> 
    <p style="clear:both;float:left;"><input type="submit" value="Log in" /></p>
</div>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2024-11-03 20:29:39

你做事的方式存在很多问题 - 例如存储明文密码 - 查看 django 可用的内置身份验证后端以了解如何正确登录用户。额外的好处包括用户在 request.user 中自动可用
http://docs.djangoproject.com/en/dev/topics/auth/

但是,我确实理解一步一步学习 django。给定您的代码,只需将一个变量添加到失败时呈现的模板的上下文中即可。

def login(request):
    if request.POST:            
        email=request.POST.get("username")
        password = request.POST.get("password")                     
        user = UniversityDetails.objects.filter(email=email,password=password)          
        if(not user):
            return render_to_response("registrationForm.html", 
                       {'invalid': True }) # our template can detect this variable
        else:
            return render_to_response("logedIn.html")

模板

{% if invalid %}
    Your email/password combo doesn't exist. 
{% endif %}

There are massive problems with the way you are doing things - storing plaintext passwords for example -- look into the built in authentication backends available to django for how to properly log a user in. Added benefits include the user being available automatically in request.user
http://docs.djangoproject.com/en/dev/topics/auth/

BUT, I do understand learning django one step a time. Given your code, just add a variable to the context of your template being rendered on failure.

def login(request):
    if request.POST:            
        email=request.POST.get("username")
        password = request.POST.get("password")                     
        user = UniversityDetails.objects.filter(email=email,password=password)          
        if(not user):
            return render_to_response("registrationForm.html", 
                       {'invalid': True }) # our template can detect this variable
        else:
            return render_to_response("logedIn.html")

template

{% if invalid %}
    Your email/password combo doesn't exist. 
{% endif %}
生生不灭 2024-11-03 20:29:39

我强烈建议您使用 Django 的内置用户身份验证系统。您可以存储其他用户信息对于每个用户,它将为您完成大部分工作(包括加密密码和用户权限)。

如果不可能,请考虑使用表单来收集和验证用户数据——它比你自己做的更干净、更容易。

最后,我建议您仔细阅读 教程 以了解如何使用模板中的变量。

我知道需要一段时间才能熟悉 Django 的工作原理(天知道我花了足够长的时间),但请坚持下去:)。

I would highly recommend you use Django's built-in user authentication system. You can store additional user information for each user, and it will do most of the work for you (including encrypted passwords and user permissions).

If that is not possible, then look into using forms to collect and validate the user data - it is much cleaner and easier than doing it yourself.

Finally, I'd recommend you go over the tutorial to see how to use variables in your templates.

I know it takes a while to become familiar with how Django does things (god knows it took me long enough), but stick in there :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文