Django应用程序中的版本号
我正在开发一个Django应用程序,我想显示该应用程序的版本(这样,发现bug的人就知道该应用程序的版本,并且可以提供更好的bug报告)Django应用程序中的版本号,django,Django,我正在开发一个Django应用程序,我想显示该应用程序的版本(这样,发现bug的人就知道该应用程序的版本,并且可以提供更好的bug报告) 在Django中是否有一种普遍接受的存储版本号的方法(我指的是我的应用程序的版本,而不是Django)?似乎设置文件是存储版本号的合理位置。我不相信有任何Django接受的方式来存储个人应用程序的版本号。它似乎是您应该定义的特定于应用程序的变量 有关从svn获取版本号的更多信息:我正在寻找完全相同的问题,并找到了您的问题。你接受的答案我不太满意 我正在使用dj
在Django中是否有一种普遍接受的存储版本号的方法(我指的是我的应用程序的版本,而不是Django)?似乎设置文件是存储版本号的合理位置。我不相信有任何Django接受的方式来存储个人应用程序的版本号。它似乎是您应该定义的特定于应用程序的变量
有关从svn获取版本号的更多信息:我正在寻找完全相同的问题,并找到了您的问题。你接受的答案我不太满意 我正在使用django debugtoolbar,在那里您还可以显示所用应用程序的所有版本。我想知道如何让我的自定义应用程序的版本也显示在那里 再进一步看,我发现了这个问题和答案: 然而,这个答案并没有告诉我把这个
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
所以我查看了一个打开的应用程序,它确实显示在django工具栏中。
我查看了django restframework代码,发现:
该版本被放入\uuuu init\uuuuuuuuy.py
文件中
(见附件)
这里是这样写的:
__version__ = '2.2.7'
VERSION = __version__ # synonym
在这之后,在他的setup.py中,他从这个\uuuu init\uuuuu.py
获取这个版本:
见:
像这样:
import re
def get_version(package):
"""
Return package version as listed in `__version__` in `init.py`.
"""
init_py = open(os.path.join(package, '__init__.py')).read()
return re.match("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1)
version = get_version('rest_framework')
import sys
sys.path.append('..')
from content_services import __version__
def get_app_version(request):
"""
Get the app version
:param request:
:return:
"""
return {'APP_VERSION': __version__}
使用buildout和zestreleaser时:
顺便说一下,我正在使用buildout和zest.releaser进行构建和版本控制
在这种情况下,上面有点不同(但基本上是相同的想法):
看
setup.py中的版本由setup.py自动编号,因此在\uuuu init\uuuuu.py
中,您可以:
import pkg_resources
__version__ = pkg_resources.get_distribution("fill in yourpackage name").version
VERSION = __version__ # synonym
不是针对Django应用程序本身,而是针对Python模块,是的。请参阅和库(简易安装verlib)
(我想详细说明一下,但我刚刚自己发现了这一点。)我通过在django项目中添加templatetag解决了这个问题:
在proj/templatetags中,添加了version.py:
from django import template
import time
import os
register = template.Library()
@register.simple_tag
def version_date():
return time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime('../.git')))
然后,在my base.html(或任何模板)中,添加:
{%load version%}
上次更新:{%version\u date%}
对我来说,最好的结果/方法是在项目文件夹上使用\uuuu init\uuuuu.py
,例如
.
├── project_name
│ ├── __init__.py
然后使用standar方法进行检查,如()
您可以在许多地方存储应用程序版本号,并使用一些方法在django模板中显示应用程序版本号。这在很大程度上取决于您使用的发布工具和您自己的偏好
下面是我在当前项目中使用的方法
将版本号放入version.txt
我正在version.txt文件中存储应用程序版本号。这是发布工具(我正在使用)在发布时考虑的位置之一
version.txt的全部内容只是应用程序版本号,例如:1.0.1.dev0
将数字读取到settings.py中的变量
。。。
打开(版本文件路径)作为v文件:
APP_VERSION_NUMBER=v_file.read()
...
创建自定义上下文处理器
本段和以下内容都是基于
自定义上下文处理器将允许您将应用程序版本号添加到每个渲染模板的上下文中。您不必每次呈现模板时都手动添加它(通常您希望在每页的页脚某处都有版本号)
在应用程序目录中创建context_processors.py文件:
from django.conf import settings
def selected_settings(request):
# return the version value as a dictionary
# you may add other values here as well
return {'APP_VERSION_NUMBER': settings.APP_VERSION_NUMBER}
将上下文处理器添加到settings.py
模板=[{
...
“选项”:{
“上下文处理器”:[
...
'您的应用程序。上下文\u处理器。选定的\u设置'
],
},
}]
在视图中使用RequestContext
或render
和render
使用在settings.py中设置的context\u处理器提供的变量填充上下文
例如:
def some_view(request):
return render(request, 'content.html')
在模板中使用它
。。。
{%trans'应用程序版本'%}:{{App\ u版本\编号}
....
如果使用GIT进行源代码版本控制,您可能需要手动升级稳定版本
发布,并为开发提交自动编号
在Django项目中获得此信息的一个原因是:
在“PROJECT/u init_uu.py”中定义:
然后在setting.py中执行以下操作:
import os
import subprocess
import PROJECT
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
try:
PROJECT.__build__ = subprocess.check_output(["git", "describe", "--tags", "--always"], cwd=BASE_DIR).decode('utf-8').strip()
except:
PROJECT.__build__ = PROJECT.__version__ + " ?"
因此,PROJECT.uu build_uu将显示:
v1.0.1 in stable releases
及
当最近的标记没有指向最后一次提交时(其中N计算标记后的额外提交数,后跟提交签名)我使用了上下文处理器,它看起来如下所示:
import re
def get_version(package):
"""
Return package version as listed in `__version__` in `init.py`.
"""
init_py = open(os.path.join(package, '__init__.py')).read()
return re.match("__version__ = ['\"]([^'\"]+)['\"]", init_py).group(1)
version = get_version('rest_framework')
import sys
sys.path.append('..')
from content_services import __version__
def get_app_version(request):
"""
Get the app version
:param request:
:return:
"""
return {'APP_VERSION': __version__}
由于项目名称为content\u services
我必须将系统路径向上更改1级,以便导入它。如果使用Git和版本标记,则可以在管理站点标题中显示应用程序版本
在项目或任何应用程序模块中创建version.py
文件:
import os
import subprocess
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
def get_version_from_git():
try:
return subprocess.check_output(['git', 'describe', '--tags'],
cwd=FILE_DIR).decode('utf-8').strip()
except:
return '?'
VERSION = get_version_from_git()
将版本添加到url.py中的管理站点标题中:
from django.contrib import admin
from django.utils.safestring import mark_safe
from utils import version
...
admin.site.site_header = mark_safe('MyApp admin <span style="font-size: x-small">'
f'({version.VERSION})</span>')
import os
import time
import subprocess
import json
class VersionViewSet(ViewSet):
def list(self, request):
# ['git', 'describe', '--tags'] # use this for named tags (version etc)
# ['git', 'describe', '--all', '--long'] # use this for any commit
# git log -1 --pretty=format:"Last commit %h by %an, %ar ("%s")"
# {"commit_hash": "%h", "full_commit_hash": "%H", "author_name": "%an", "commit_date": "%aD", "comment": "%s"}
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
git_command = ['git', 'log', '-1', '--pretty={"commit_hash": "%h", "full_commit_hash": "%H", "author_name": "%an", "commit_date": "%aD", "comment": "%s"}']
git_identifier = subprocess.check_output(git_command, cwd=FILE_DIR).decode('utf-8').strip()
git_identifier = json.loads(git_identifier)
last_updated = time.strftime('%a, %-e %b %Y, %I:%M:%S %p (%Z)', time.localtime(os.path.getmtime('.git'))).strip()
return Response({
"last_updated": last_updated,
"git_commit": git_identifier
}, status=200)
from myapp.views import VersionViewSet
router = routers.DefaultRouter()
...
router.register(r'version', VersionViewSet, base_name='version')
版本信息通常保存在git提交标记中。否则,即使是git提交和上次更新时间也可以很好地指示哪个版本正在运行以及何时部署
对于那些使用django rest框架并且只有API的用户,您可以同时返回这两个API;使用/api/version
端点执行“上次更新”和“上次git提交”:
在视图.py中
:
from django.contrib import admin
from django.utils.safestring import mark_safe
from utils import version
...
admin.site.site_header = mark_safe('MyApp admin <span style="font-size: x-small">'
f'({version.VERSION})</span>')
import os
import time
import subprocess
import json
class VersionViewSet(ViewSet):
def list(self, request):
# ['git', 'describe', '--tags'] # use this for named tags (version etc)
# ['git', 'describe', '--all', '--long'] # use this for any commit
# git log -1 --pretty=format:"Last commit %h by %an, %ar ("%s")"
# {"commit_hash": "%h", "full_commit_hash": "%H", "author_name": "%an", "commit_date": "%aD", "comment": "%s"}
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
git_command = ['git', 'log', '-1', '--pretty={"commit_hash": "%h", "full_commit_hash": "%H", "author_name": "%an", "commit_date": "%aD", "comment": "%s"}']
git_identifier = subprocess.check_output(git_command, cwd=FILE_DIR).decode('utf-8').strip()
git_identifier = json.loads(git_identifier)
last_updated = time.strftime('%a, %-e %b %Y, %I:%M:%S %p (%Z)', time.localtime(os.path.getmtime('.git'))).strip()
return Response({
"last_updated": last_updated,
"git_commit": git_identifier
}, status=200)
from myapp.views import VersionViewSet
router = routers.DefaultRouter()
...
router.register(r'version', VersionViewSet, base_name='version')
在url.py
中:
from django.contrib import admin
from django.utils.safestring import mark_safe
from utils import version
...
admin.site.site_header = mark_safe('MyApp admin <span style="font-size: x-small">'
f'({version.VERSION})</span>')
import os
import time
import subprocess
import json
class VersionViewSet(ViewSet):
def list(self, request):
# ['git', 'describe', '--tags'] # use this for named tags (version etc)
# ['git', 'describe', '--all', '--long'] # use this for any commit
# git log -1 --pretty=format:"Last commit %h by %an, %ar ("%s")"
# {"commit_hash": "%h", "full_commit_hash": "%H", "author_name": "%an", "commit_date": "%aD", "comment": "%s"}
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
git_command = ['git', 'log', '-1', '--pretty={"commit_hash": "%h", "full_commit_hash": "%H", "author_name": "%an", "commit_date": "%aD", "comment": "%s"}']
git_identifier = subprocess.check_output(git_command, cwd=FILE_DIR).decode('utf-8').strip()
git_identifier = json.loads(git_identifier)
last_updated = time.strftime('%a, %-e %b %Y, %I:%M:%S %p (%Z)', time.localtime(os.path.getmtime('.git'))).strip()
return Response({
"last_updated": last_updated,
"git_commit": git_identifier
}, status=200)
from myapp.views import VersionViewSet
router = routers.DefaultRouter()
...
router.register(r'version', VersionViewSet, base_name='version')
这将创建与API中其他端点一致的端点
在http://www.example.com/api/version/
:
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"last_updated": "Mon, 6 May 2019, 11:19:58 PM (IST)",
"git_commit": {
"commit_hash": "e265270",
"full_commit_hash": "e265270dda196f4878f4fa194187a3748609dde0",
"author_name": "Authorname",
"commit_date": "Mon, 6 May 2019 23:19:51 +0530",
"comment": "The git commit message or subject or title here"
}
}
我使用此选项\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。正如大家所说,该版本被放入\uuuu init\uuuu.py
文件中,例如:
proyect_name
| __init__.py
# __init__.py file
VERSION = '1.0.0' # or __version__ = '1.0.0'
现在来自ev