gpt4 book ai didi

python - (Django) 如何在单个 view.py 文件中遵循数据库备份和恢复工作流程?

转载 作者:行者123 更新时间:2023-11-29 16:06:50 25 4
gpt4 key购买 nike

我是 Django 新手,现在正在开发一个 Django 应用程序来备份和恢复 MySQL 数据库。该应用程序的工作流程包含 4 个步骤:

  1. 从html页面获取目标数据库参数;
  2. 获取原始数据库参数,或者从html页面上传sql文件;
  3. 选择数据表;
  4. 检查并确认要上传的数据。

我在html页面中使用了几个按钮来提交参数,每个按钮都链接到post函数中的ifelse。代码如下。似乎一旦我返回了渲染,参数、数据库连接和光标就无法在整个函数中“共享”。我该如何处理?

Python3.6 django2.1

# views.py
#-*-coding:utf-8 -*-
import os
import os.path
import sys
import datetime
from builtins import int

from django.db import models
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse

import pymysql
# Create your views here.

class IndexView(View):
template_name = 'add_struc/index.html'


def get(self, request):
return render(request, 'add_struc/index.html',)

def post(self, request):
if request.method == 'POST' and 'Tgt_link' in request.POST:
# pressed confirm in step 1. 在步骤1按下了设置目标数据库的链接

Tgt_link = {}
Tgt_ip = request.POST.get('Tgt_ip')
Tgt_port = int(request.POST.get('Tgt_port'))
Tgt_db = request.POST.get('Tgt_db')
Tgt_name = request.POST.get('Tgt_name')
Tgt_passwd = request.POST.get('Tgt_passwd')
Tgt_char = request.POST.get('Tgt_char')

# # connect to target db and get the cursor. 连接目标数据库,获取游标。
# self.Tgt_database = pymysql.connect(
# host=Tgt_ip,
# port=Tgt_port,
# db=Tgt_db,
# user=Tgt_name,
# passwd=Tgt_passwd,
# charset='utf8'
# )
# self.Tgt_cursor = database.cursor()

Tgt_link = {'Tgt_ip':Tgt_ip, 'Tgt_port':Tgt_port,'Tgt_db':Tgt_db,'Tgt_name':Tgt_name,'Tgt_passwd':'******', 'Tgt_char':Tgt_char,}
self.Tgt = Tgt_link
context = Tgt_link
# yield self.Tgt
return render(request, 'add_struc/index.html', context=context)



elif request.method == 'POST' and 'Ori_link' in request.POST:
# pressed confirm in step 2.在步骤2按下了设置源数据库的链接
Ori_link = {}
# 数据源链接
Ori_ip = request.POST.get('Ori_ip')
Ori_port = int(request.POST.get('Ori_port'))
Ori_db = request.POST.get('Ori_db')
Ori_name = request.POST.get('Ori_name')
Ori_passwd = request.POST.get('Ori_passwd')
Ori_char = request.POST.get('Ori_char')

# connect to origin db (data source) and get the cursor. 连接源数据库,获取游标。
self.Ori_database = pymysql.connect(
host=Ori_ip,
port=Ori_port,
db=Ori_db,
user=Ori_name,
passwd=Ori_passwd,
charset='utf8'
)

self.Ori_cursor = self.Ori_database.cursor()
# back up origin db to csv or sql file 备份数据库为sql文件或csv
self.Ori_cursor.close()
self.Ori_database.close()

Ori_link = {'Ori_ip':Ori_ip, 'Ori_port':Ori_port, 'Ori_db':Ori_db, 'Ori_name':Ori_name, 'Ori_passwd':'******', 'Ori_char':Ori_char,}
# context = self.Tgt.update(Ori_link)
context = Ori_link
return render(request, 'add_struc/index.html', context=context)

elif request.method == 'POST' and 'upload_file' in request.POST:
# uploaded a sql file or csv file in step 2. 在步骤2上传了数据包文件
return render(request, 'add_struc/index.html')

elif request.method == 'POST' and 'sheet_selected' in request.POST:
# confirmed the data sheets in step3. 在步骤3选好了数据表
return render(request, 'add_struc/index.html')


elif request.method == 'POST' and 'start_import' in request.POST:
# confirm alter and close db connections.在步骤4开始执行导入
return render(request, 'add_struc/index.html')

最佳答案

将此 View 类分成几个具有不同 url 路径的 View 类,如下所示:

class Step1View(TemplateView):
template_name = 'xxx'
def post(self, request):
if 'button1' is clicked:
return redirect('/step2')
elif 'button2' is clicked:
return redirect('/step3')

class Step2View(Step1View):
def post(self, request):
# do something

关于python - (Django) 如何在单个 view.py 文件中遵循数据库备份和恢复工作流程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com