如何将登录用户的登录url重定向到新闻提要页面



我有一个应用程序,它使用django用户帐户包登录网站。

我相信我必须通过settings.py文件来做到这一点:

LOGIN_URL = 'accounts/login'
LOGIN_REDIRECT_URL = 'news-feed/'
LOGOUT_URL = ''
LOGOUT_REDIRECT_URL = '' # new
def index(request):    
return render(request, 'feed/index.html',{'page_title': 'HomePage' })


class PostListView(ListView):
model = PostForNewsFeed
template_name = 'feed/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 5 # add this
count_hit = True
slug_field = 'slug'
....        return context

path('', index, name='index'),
path('news-feed/', PostListView.as_view(), name='home2'),

如何让登录用户重定向到主页url?

只需检查您的用户是否通过了

from django.shortcuts import redirect
def index(request):
if request.user.is_authenticated:
return redirect("news-feed")
return render(request, 'feed/index.html',{'page_title': 'HomePage' })

您可以在django视图中获取设置变量。除了@kiran chauhan回答:

from django.conf import settings as conf_settings
from django.shortcuts import redirect
def index(request):
if request.user.is_authenticated:
return redirect(conf_settings.LOGIN_REDIRECT_URL)
return render(request, 'feed/index.html',{'page_title': 'HomePage' })

最新更新