2017-05-08 69 views
0

我的默认项目文件夹是启动登录应用程序在它。如何在登录后通过用户名或用户名登录表单提交django

登录应用urls.py: -

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

网址登录页面。

内登录应用views.py:提交并重定向到用户的个人资料页

LoginFormView(TemplateView): 
     .......... 
     ......... 
     if user is not None: 
     if user.is_active: 
      login(request, user) 
      # messages.add_message(request, messages.INFO, 'Login Successfull.') 
      return redirect('indexClient') 

登录表单。

启动urls.py: -

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

启动views.py: -

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

我需要的URL更换客户用户名在登录时输入形成。

+0

也许会返回一个HTTP响应而不是重定向,并处理正在执行登录的前端的重定向? – Zohair

回答

0

urls.py:-

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

views.py:-

class IndexClientView(TemplateView): 
    model=User 
    slug_field = "username" 
    template_name = 'startup/index-client.html' 

现在,您可以通过访问: -

client/Space/

嵌塞参数正在使用DetailView(或任何其他基于SingleObjectMixin的视图)通过使用在User.username上查找对象和slug_field = "username".

0

您可以导入HttpResponseRedirect并使用反转函数。

然后你的views.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})) 

更改相应的urls.py,

url(r'^login/$', views.LoginFormView.as_view(), name='login'), 
url(r'^(?P<username>[\w]+)/$', views.IndexClientView.as_view(), name='indexClient') 

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

希望这是有用的。