简体   繁体   English

如何在 django 的上下文处理器文件中检查登录用户

[英]how to check logged in user in context processors file in django

I am using context processors for showing total products in cart and other details in my project's header and footer.我正在使用上下文处理器来显示购物车中的总产品以及我项目的 header 和页脚中的其他详细信息。 This is my file for context processors file "custom_context.py"这是我的上下文处理器文件“custom_context.py”

from models import Event, Ticket_Cart_details

def get_base_content(request):
    music_events = Event.objects.all().filter(category='music', status = True).values('id','event_title').order_by('-id')[:4]
    sports_events = Event.objects.all().filter(category='sports', status = True).values('id','event_title').order_by('-id')[:4]
    experience_events = Event.objects.all().filter(category='experience', status = True).values('id','event_title').order_by('-id')[:4]
    lifestyle_events = Event.objects.all().filter(category='lifestyle', status = True).values('id','event_title').order_by('-id')[:4]

The above code does not contain any information about logged in user now i have to write a code in which i have to get data of logged in user.上面的代码不包含有关登录用户的任何信息,现在我必须编写一个代码,我必须在其中获取登录用户的数据。

from models import Event, Ticket_Cart_details

def get_base_content(request):
    music_events = Event.objects.all().filter(category='music', status = True).values('id','event_title').order_by('-id')[:4]
    sports_events = Event.objects.all().filter(category='sports', status = True).values('id','event_title').order_by('-id')[:4]
    experience_events = Event.objects.all().filter(category='experience', status = True).values('id','event_title').order_by('-id')[:4]
    lifestyle_events = Event.objects.all().filter(category='lifestyle', status = True).values('id','event_title').order_by('-id')[:4]

    if request.user.is_authenticated():
        current_cart_products = Ticket_Cart_details.objects.all().filter(user_id=request.user.id, session_id=request.session.session_key)
    current_cart_products = Ticket_Cart_details.objects.all().filter(session_id=request.session.session_key)

In the above code "request" will not work, so how can i get records of logged in user using this在上面的代码中,“请求”将不起作用,那么我怎样才能使用这个获取登录用户的记录

You are using request well, but when you work with context processors, you have to return all the data that you want.您很好地使用了请求,但是当您使用上下文处理器时,您必须返回所有您想要的数据。 For example:例如:

def get_base_content(request):
    music_events = Event.objects.all().filter(category='music', status=True).values('id','event_title').order_by('-id')[:4]
    sports_events = Event.objects.all().filter(category='sports', status=True).values('id','event_title').order_by('-id')[:4]
    return {"music_events": music_events,
            "sports_events": sports_events ,
            }

Additionally, for the problems with user not allowed or not logged in, you have to handle it with a anonymous data or something like this.此外,对于用户不允许或未登录的问题,您必须使用匿名数据或类似的方式处理它。

def get_base_content(request):
    if not request.user:
        music_events = 'none'
        sports_events = 'none'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM