django创建上下文

 在app中创建context_processes.py(可以是别的名字),然后加载到settings里,这样所有的网页都可以传入变量

复制代码
from .models import User
def app01_context(request):
    context = {}
    user_id = request.session.get('user_id')
    ip = request.META['REMOTE_ADDR']
    print('上下文内容:',user_id, ip)
    try:
        login_user = User.objects.get(pk=user_id)
        context['login_user'] = login_user
        context['ip'] = ip
    except:
        pass
    return context
复制代码

 

posted @   力王7314  阅读(208)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示