- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Django 新手,现在正在开发一个 Django 应用程序来备份和恢复 MySQL 数据库。该应用程序的工作流程包含 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/
如何启用 Mercurial(中央/主)存储库的自动(增量)备份? 我来自颠覆,在那里我能够制作一个提交 Hook ,将更改上传到 S3。 编辑: 如果这种策略对 Mercurial 没有意义,那么什
我想定期创建我的github存储库的备份。是否有一种快速的方法可以在不知道整个列表的情况下将它们全部拉出? 沃尔特 最佳答案 您可以通过GitHub的API获取整个列表: curl http://gi
我正在使用 flatpages在我在本地服务器上开发的站点中。我需要备份平面页面的数据以在最终服务器中使用它。有谁知道怎么做? 最佳答案 在您的本地服务器上运行: python manage.py d
我们已将应用服务升级到 S1,并希望设置自动备份。这是一个带有 SQL 数据库的 .net core 应用程序。对于数据库部分,我得到以下内容: No supported connection str
我们使用一些 阻止 blob 来存储一些持久资源,然后 用于存储事件数据的页面 blob 我们需要备份 blob,因此我尝试使用 AzCopy。它在我的开发机器上工作正常,但在其他慢速机器上失败,并出
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 5年前关闭。 Improve this questi
我正在看这个例子 docker run --rm --volumes-from myredis -v $(pwd)/backup:/backup debian cp /data/dump.rdb /b
我们使用一些 阻止 blob 来存储一些持久资源,然后 用于存储事件数据的页面 blob 我们需要备份 blob,因此我尝试使用 AzCopy。它在我的开发机器上工作正常,但在其他慢速机器上失败,并出
我目前正在使用 Java,并且正在寻找一个程序,该程序可以在每次编译时保存我正在执行的操作的新版本。我不介意它是否不运行,我可以进去编辑类名,使其与 .java 名称匹配。由于我是一个初学者,我总是因
在 RabbitMQ 管理控制台中,出于导入和导出目的,我在概览页面底部看到了导入/导出定义链接。但是有了这个,我能够导出整套队列、交换等。 我有一个 MQ 服务器,其中包含多个应用程序的 MQ 设置
我在 Eclipse IDE 中做了一个 Maven 项目,但在某些时候,我决定以其他名称复制并粘贴工作区位置中的文件夹,以避免在迄今为止工作正常的原始项目中进行一些错误配置,但是,我在执行中遇到了问
有没有办法备份在任何给定元素上设置的所有事件? 背景故事:我有一些代码,当用户将鼠标悬停在某个图标上时(想想悬停帮助文本),会显示绝对定位的“弹出窗口”。如果用户将鼠标悬停在弹出窗口上,它会停留在周围
我需要将 MySQL 数据库移动到新位置。根据workbench,数据库大小只有200MB,但有些表有数百万行,报告索引和数GB的数据长度。结果,EXPORT 的大小为 15GB,导出 DB 并将其复
我有一个工作数据库 - 我想复制它的所有数据(甚至只是其中的 1 个表)并在磁盘上创建备份,并在另一个位置(不是同一台计算机)上创建一个“备份”数据库。另一台计算机未连接到互联网 我该怎么做? 最佳答
我有一个实时数据库,我想要做的是制作一份副本。我可以通过 SSH 和 phpMyAdmin 访问 MySQl。是否有一个命令可以在单个命令/操作中复制/备份数据库,而无需使用导出/导入? 谢谢 最佳答
我是 PostgreSQL 的新手,我正在寻找备份数据库的方法。我知道有 3 种方法 pg_dump,快照和复制以及使用 WAL。您建议使用哪一个来完整备份数据库?如果可能,请提供代码片段。 最佳答案
如何快速备份/恢复mysql数据库中的数据? 最佳答案 mysqldump命令行备份最快, (使用mysql命令行恢复,mysql -u #username# -p #database# < #dum
从管理的角度来看,我是 MySQL 的新手。 在过去的几个小时里,我一直在谷歌上搜索,但没有成功,我想知道是否有人可以为我指明正确的方向,让我知道要用谷歌搜索什么或提出建议。 基本上,我正在寻找有关如
我需要备份创建模式附带的数据库(转储)。使用 putty(通过 SSH)完成这项工作的命令行是什么?我研究并思考了这个命令,但它只执行表备份和数据。 mysqldump -h HOST -u LOGI
我正在尝试创建一个将整个/dev/sda1 备份到/mnt/Backup 的 bash 脚本 /dev/sda1 457G 3.5G 431G 1% / /dev/sdb1
我是一名优秀的程序员,十分优秀!