向消息添加 django 格式

adding a django format to messages

我正在尝试将用户名添加到我的 messages.success 中,这样当他登录时就会出现消息并拥有用户名 views.py:

def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        if username and password:

            user = authenticate(username=username, password=password)
            
            if user is not None:
                l_login(request, user)
                success_url = '/home'
                messages.success(request, 'You logged in to ')
                return redirect('home') 
            else:
                messages.error(request, 'Username or Password is Incorrect')
        else:
            messages.error(request, 'Fill out all the Fields Please')  
    
    return render(request, 'register/login.html',{})

您可以从用户对象中获取:

messages.success(request, <b>f</b>'You logged in to <b>{user.username}</b>')

f'…'literal string interpolation [pep-498]