相关疑难解决方法(0)

如何将文件发送到 Django 中的响应?

我有这样的 php 函数,我尝试在我的 Django 项目中重写它。对于像header()和这样的php方法,python中的类似物应该是什么show_error()?另外如何发送文件以响应?

php:

function waprfile($date=false) {
    if(!isset($date) || $date==false) $date = date("d.m.y");

    $timestmp = date2timestamp($date);

    $filepath = "https://www.example.com/files/".$this->lang_code."/";

    if(file_get_contents($filepath.date("dmy",$timestmp).".xls"))
    {
        header("Location: ".$filepath."wapr".date("dmy",$timestmp).".xls");
    }
    else
    {
        show_error(_langWrite("No file for specified date", "???? ?? ????????? ???? ???????????"));
    }
}
Run Code Online (Sandbox Code Playgroud)

Python:

import urllib.request
import datatime
import time
from django.utils import translation

def isset(variable):
    return variable in locals() or variable in globals()

def waprfile(request, date):
    if(not isset(date) or date==False):
        date = datetime.datetime.now().strftime('%d.%m.%Y')

    timestmp = time.mktime(datatime.datetime.strptime(date, "%d.%m.%Y").timetuple())

    filepath …
Run Code Online (Sandbox Code Playgroud)

php python django python-2.7 django-1.11

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

django ×1

django-1.11 ×1

php ×1

python ×1

python-2.7 ×1