在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