为什么在views.py中导入函数之外的变量不起作用?(ms_fields.py 是同一文件夹中的文件)
==== 这有效:正确导入变量“MS_FIELDS”=============
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, RequestContext, get_object_or_404
def current_quote(request):
from .ms_fields import MS_FIELDS #import within the function
return render_to_response('mis/current_quote.html', locals(), context_instance=RequestContext(request))
===这不起作用:“分配前引用了局部变量'MS_FIELDS'” =====
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, RequestContext, get_object_or_404
from .ms_fields import MS_FIELDS # import at the beginning of the file
def current_quote(request):
MS_FIELDS = MS_FIELDS
return render_to_response('mis/current_quote.html', locals(), context_instance=RequestContext(request))
这是为什么?导入函数不应该在整个文件中提供一个变量吗?
非常感谢!