在djang中提交登录表单后,如何在url中传递用户id或用户名

2024-10-02 00:32:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我的默认项目文件夹是启动,其中包含登录应用程序。在

登录应用程序网址.py:-

url(r'^$', views.LoginFormView.as_view(), name='index'),

登录页面的url。在

内部登录应用程序视图.py

^{pr2}$

已提交登录表单并重定向到用户配置文件页面。在

启动网址.py:-

url(r'^client/', views.IndexClientView.as_view(), name='indexClient'),

启动视图.py:-

class IndexClientView(TemplateView):
    template_name = 'startup/index-client.html'

我需要url来用登录表单中输入的用户名替换客户端。在


Tags: 项目namepyclientview视图应用程序url
2条回答

您可以导入HttpResponseRedirect并使用reverse函数。在

那你呢视图.py看起来像这样

from django.http import HttpResponseRedirect

class LoginFormView(TemplateView):
    ..,...................
    if user is not None:
            if user.is_active:
                login(request, user)
                # messages.add_message(request, messages.INFO, 'Login Successfull.')
                return HttpResponseRedirect(reverse('indexClient', kwargs={'username':user.username}))

改变你的网址.py因此

^{pr2}$

您将有一个登录视图和一个单独的用户配置文件视图。这里indexClient显示为用户配置文件视图。登录后,django重定向到indexClient视图,用户名=用户名,即当前用户的用户名,该用户名应在您需要的url上。在

希望这是有用的。在

在网址.py:-

url(r'^client/(?P<slug>[\w.@+-]+)/$', views.IndexClientView.as_view(), name='indexClient')

在视图.py:-

^{pr2}$

现在您可以通过以下方式访问它:

client/Space/

slug参数被DetailView(或任何其他基于单个objectmixin的视图)使用,通过使用model = User和{}来查找{}上的对象

相关问题 更多 >

    热门问题