Python 有没有办法提到django中的两个登录URL?

Python 有没有办法提到django中的两个登录URL?,python,django,django-views,Python,Django,Django Views,在我的应用程序中,如果用户第一次登录,他将被重定向到配置文件页面,从第二次开始,他将被重定向到主页(在我的应用程序中为VChome)。 所以我决定在url.py和views.py中编写这个 url.py from django.urls import path from . import views urlpatterns = [ path('VC/',views.VChome, name='VChome'), path('profile/',views.update_prof

在我的应用程序中,如果用户第一次登录,他将被重定向到配置文件页面,从第二次开始,他将被重定向到主页(在我的应用程序中为VChome)。 所以我决定在url.py和views.py中编写这个

url.py

from django.urls import path
from . import views

urlpatterns = [
    path('VC/',views.VChome, name='VChome'),
    path('profile/',views.update_profile,name='profile'),
    path('users/login/', views.login_user, name='login'),
    path('login/done/', LoginRedirectView.as_view(), name='login_redirect'),       
]
views.py

class LoginRedirectView(generic.View):
def get(self, request, *args, **kwargs):
    logout(request)
    username = password = ''
    if request.POST:
        username = request.POST['username']
        password = request.POST['password']
        userLL = CustomUser.objects.get(username=username)
        # print(userLL)
        last_login = userLL.last_login
        # print(last_login)
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                if last_login == None:
                    return HttpResponseRedirect(reverse("profile"))
                else:
                    return HttpResponseRedirect(reverse("VChome"))
    return render(request, 'login.html')
login.html

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
{% block content %}
  <form method="post">
        <strong><p>Sign in</p></strong>
        <p>Username</p>
        {% csrf_token %}
        <input type="text" id="username" name="username"  placeholder="Username">
        <p>Password</p>
        {% csrf_token %}
        <input type="password" name="password" id="password" placeholder="Password">
        <input type="submit" value="Login">      
  </form>
{% endblock %}
</body>
</html>

{%block content%}
登录

用户名

{%csrf_令牌%} 密码

{%csrf_令牌%} {%endblock%}
我有profile.html和VChome.html模板,我确信它们在views.py中呈现正确

这里的问题是,如果我提到LOGIN\u REDIRECT\u URL='VChome',登录页面将重定向到VChome

若我并没有提到它,那个么登录页面将被重定向到/accounts/profile


我希望根据login\u user in views.py中提到的最后一个登录约束重定向登录页面。我会保持简单,创建一个视图,执行逻辑并返回重定向到
VChome
或其他任何位置

views.py

class LoginDirectView(视图):
def get(自我、请求、*args、**kwargs):
如果请求.user.blahblah:
返回HttpResponseRedirect(反向('CAhome'))
其他:
返回HttpResponseRedirect(反向('profile'))
url.py

。。。
路径('login/done/',loginDirectView.as_view(),name='login_redirect'),
...
settings.py

LOGIN\u REDIRECT\u URL='LOGIN\u REDIRECT'


从技术上讲,您可以从同一请求调用不同的视图,从而为用户节省一个HTTP重定向。但我认为这可能不值得为这个特定的用例付出努力…

据我所知,它应该可以按照您的意愿工作。当您点击登录页面时,是否会发送一个名为
next
的GET querystring参数?@schillingt Nope。我的代码中没有使用任何GET。