简体   繁体   English

Django 中正确的重定向是什么?

[英]What is the right redirection in Django?

I'm a beginner in Django and I want to display the username from the nav bar.我是 Django 的初学者,我想从导航栏中显示用户名。 I did not use User authentication.我没有使用用户身份验证。 I only created custom database table called Jobseeker:我只创建了名为 Jobseeker 的自定义数据库表:

class Jobseeker(models.Model):

    username = models.CharField(max_length=50)
    email = models.CharField(max_length=50)
    password = models.CharField(max_length=50)
    repassword = models.CharField(max_length=50)
    
    def __str__(self):
       return self.username 

Because the criteria is the user should be able to login using username or email then password.因为标准是用户应该能够使用用户名或电子邮件然后密码登录。 I was able to achieve that using this code,我能够使用这段代码实现这一点,

def login(request):

    jobseeker = None
    if request.method == 'POST':
        userinput = request.POST['username']
        try:
            username = Jobseeker.objects.get(email=userinput).username
        except Jobseeker.DoesNotExist:
            username = request.POST['username']
            password = request.POST['password']
            jobseeker = auth.authenticate(username=username, password=password)

        if Jobseeker is not None:
            auth.login(request, jobseeker)
            messages.success(request,"Login successfull")
            return redirect('home')
        else:
            messages.error(request,'Invalid credentials, Please check username/email or password. ')
    
    return render(request, 'login.html',)

Now my problem is using {{request.user}} in the nav bar it display the superuser's username, instead on the jobseeker username.现在我的问题是在导航栏中使用 {{request.user}} 它显示超级用户的用户名,而不是求职者用户名。 Superuser's username .超级用户的用户名 Code for the HTML HTML代码

I want to display the username that i inputed in the login form.我想显示我在登录表单中输入的用户名。 It should be one of these username that needs to be displayed sorry for the bad english but hopefully it makes sense.它应该是需要显示的这些用户名之一,抱歉英语不好,但希望它有意义。

You didn't give me enough informations.你没有给我足够的信息。

Up to now, I can only explain why the problem occurs when you login with email: it is because auth.authenticate function is only called when the code in try raise an error.到目前为止,我只能解释为什么使用电子邮件登录时会出现问题:因为auth.authenticate函数仅在try中的代码引发错误时被调用。

So you should fix like this:所以你应该这样修复:

jobseeker = None
if request.method == 'POST':
    userinput = request.POST['username']
    try:
        username = Jobseeker.objects.get(email=userinput).username
    except Jobseeker.DoesNotExist:
        username = request.POST['username']
    password = request.POST['password']
    jobseeker = auth.authenticate(username=username, password=password)

    if jobseeker is not None:
        auth.login(request, jobseeker)
        messages.success(request,"Login successfull")
        return redirect('home')
    else:
        messages.error(request,'Invalid credentials, Please check username/email or password. ')

return render(request, 'login.html',)

If it still works badly, please comment to my answer and I'll help you more.如果它仍然效果不佳,请评论我的答案,我会帮助你更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM