为什么在函数外导入变量不能正常工作视图.py?(毫秒)_字段.py是同一文件夹中的文件)
=====这起作用:变量“MS\u 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\u 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))
为什么?import函数不应该在整个文件中提供一个变量吗?你知道吗
非常感谢!你知道吗
不是导入不起作用,而是任务。通过给函数中的MS_字段赋值,可以告诉Python这是一个局部变量,它覆盖了导入中的全局名称。你知道吗
我不明白你为什么要这么做。只需显式地将msu字段传递给上下文。使用
locals()
是一种技巧,但不是很好。你知道吗相关问题 更多 >
编程相关推荐