2017-05-08 70 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上。

希望這是有用的。