django - Django CBV:在同一个类中创建函数
问题描述
我正在开发一个基于的新项目,Django Class Based View
并且我想获得建议以分解重要的post function
.
我对这个概念很陌生。
我的帖子功能如下所示:
def post(self, request, *args, **kwargs):
form = self.form_class()
query_document = None
query_document_updated = None
query_app = None
query_document_count = None
if "UpdateDocument" in request.POST:
checkbox_id = request.POST['DocumentChoice']
checkbox_id_minus_1 = int(checkbox_id) - 1
query_document_updated = Document.objects.get(id=checkbox_id)
APP_CODE = query_document_updated.app.code
SRC_FILENAME = query_document_updated.src_filename
FILENAME, file_extension = os.path.splitext(SRC_FILENAME)
CATEGORY = query_document_updated.category
if CATEGORY == "ANNUAL":
CATEGORY = "ANNUAL_REPORT"
# Get the new year selected by user
year = self.request.POST.get('q1year')
# Create the new document title updated by the new year
new_document_title = f"{year}_{CATEGORY}_{APP_CODE}" + " - " + f"{SRC_FILENAME}"
# Create the new document file updated by the new year
new_document_file = "app_docs/" + f"{APP_CODE}" + "/" + \
f"{year}_{CATEGORY}_{APP_CODE}_{checkbox_id_minus_1}{file_extension}"
context = {
'form': form,
'query_app' : query_app,
'query_document': query_document,
'query_document_updated': query_document_updated,
'query_document_count' : query_document_count
}
return render(request, self.template_name, context)
我想创建两个新函数:new_document_title
并new_document_file
调用这两个函数post
我怎么能这样做?在发布后创建两个函数并在参数中传递变量?
解决方案
我认为您可以通过多种方式做到这一点!我在这里写几个,
方法一:使用class methods
from rest_framework.views import APIView
class MyView(APIView):
def _new_document_title(self, *args, **kwargs):
# do something
return something
def _new_document_file(self, *args, **kwargs):
# do something
return something
def post(self, request, *args, **kwargs):
self._new_document_title() # calling "_new_document_title"
self._new_document_file() # _new_document_file
# do something
return some_response
方法2:使用Mixin class
class MyMixin(object):
def new_document_title(self, *args, **kwargs):
# do something
return something
def new_document_file(self, *args, **kwargs):
# do something
return something
class MyView(APIView, MyMixin):
def post(self, request, *args, **kwargs):
self.new_document_title() # calling "new_document_title"
self.new_document_file() # new_document_file
# do something
return some_response
方法3:调用外部函数
from rest_framework.views import APIView
def new_document_title(self, *args, **kwargs):
# do something
return something
def new_document_file(self, *args, **kwargs):
# do something
return something
class MyView(APIView):
def post(self, request, *args, **kwargs):
new_document_title() # calling "new_document_title"
new_document_file() # new_document_file
# do something
return some_response
推荐阅读
- java - 骆驼路线是如何手动启动的?
- windows - 为什么安装程序无法在 Windows Server 2016 中打开?
- c# - UWP 如何与 .NET Core Web 应用程序建立通信?
- ios - 图像选择器 ViewController 应用程序卡住 - NSXPCConnection
- php - Laravel 5.5 - 无法更新对象
- ios - 如何知道 UIPasteboard.general.URL 中的 url 是否有效?
- python - 使用sqlalchemy python查询表中的sum 2列
- python - 使用 Django Allauth 跳过初始登录页面
- mysql - MySQL布尔搜索包括内部连接内的字段
- bitbucket - 使用 Bitbucket AWS CodeDeploy 插件部署时生成“.env”