我似乎遇到了问题。当管理员用户点击按钮时,我想编写一些可以转储mysql数据库的代码。我尝试在下面的views.py中编写一些代码。
Views.py
@login_required
def dbbackup(request):
if not (request.user.is_authenticated() and request.user.is_staff):
raise http.Http404
os.chdir('/usr/local/src/djcode/c2duo_mms') # This is important as apache by default goes to the user's home directory.
os.popen3("mysqldump --add-drop-table -u " + settings.DATABASE_USER + " -p" + settings.DATABASE_PASSWORD + " " + settings.DATABASE_NAME + " > backup.sql")
os.popen3("gzip -c backup.sql > backup.gz")
现在给我一个错误The view c2duo_mms.mmc.views.dbbackup didn't return an HttpResponse object.
如果我在最后添加返回HttpResponse。它会说unbound method has_header() must be called with HttpResponse instance as first argument (got str instance instead)
。
我不太确定问题是什么。但我无法解决问题。我现在有点困惑。
答案 0 :(得分:1)
您需要从方法的末尾返回有效的HttpResponse
对象。例如,如果要返回gzip文件:
@login_required
def dbbackup(request):
if not (request.user.is_authenticated() and request.user.is_staff):
raise http.Http404
os.chdir('/usr/local/src/djcode/c2duo_mms') # This is important as apache by default goes to the user's home directory.
os.popen3("mysqldump --add-drop-table -u " + settings.DATABASE_USER + " -p" + settings.DATABASE_PASSWORD + " " + settings.DATABASE_NAME + " > backup.sql")
os.popen3("gzip -c backup.sql > backup.gz")
dataf = open('/usr/local/src/djcode/c2duo_mms/backup.gz', 'r')
return HttpResponse(dataf.read(), mimetype='application/x-gzip')
这应该开始下载gzip文件。