我正在开发需要全局变量的应用程序。幸运的是,尽管eclipse抱怨我使用全局变量的方式,但它仍在工作。以这个问题为例,我创建了一个使用全局变量作为页面计数器的应用程序(有效!)。这是代码:
我的__init__.py文件:
counter = 0
我的views.py
from accessCounter import counter
from django.shortcuts import render
def conterf(request):
global counter
counter +=1
context = {
'counter' : counter,}
return render(request,'accessCounter/index.html',context)
并且eclipse抱怨我在“来自accessCounter导入计数器”的行上有一个“未导入的导入:计数器”,但是如果我删除此行,则该计数器不适用于此错误:
name 'counter' is not defined
我认为以下信息无关,但在这里……
我的index.html文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
access counter: {{ counter }}
</body>
</html>
和我的url.py文件:
from django.contrib import admin
from django.urls import path
from accessCounter import views
urlpatterns = [
path('admin/',admin.site.urls),path('',views.conterf,name='counter'),]