我写了一个视图,将我的文件保存在存储中并保存到数据库中,我想传递文件路径进行下载,但是我遇到了问题。 我的查看功能:
def xmlFile(request):
all = request.POST
data = dict(all)
username = User.objects.get(username=request.user)
xml = xmlFirst.dataToXml(data)
with open('web/data/Fuzzy'+ str(username.username)+'1.xml','w') as myfile:
myfile.write(xml)
xmlFile = XmlFile(username= username, upload='Fuzzy'+ str(username.username)+'1.xml',filename='Fuzzy'+ str(username.username)+'1.xml')
xmlFile.save()
xmlLink = XmlFile.objects.first()
a = xmlLink.upload.path
context = {'data':a}
return render(request, 'xmlFile.html', context)
我的设置
MEDIA_URL = '/data/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'data')
我的xmlFile.html模板
<!DOCTYPE html>
<html>
<body>
<a href="{{MEDIA_URL}}{{data}}" download="FuzzyMirab1.xml">{{data}}</a>
</body>
</html>
和我的模型:
class XmlFile(models.Model):
id = models.AutoField(primary_key=True)
username = models.ForeignKey(User)
filename = models.CharField(max_length=1000, default="")
upload = models.FileField(upload_to="data", default="")
def __unicode__(self):
return "{}, {}".format(self.username, self.filename)
当我单击它时,它会转到http://localhost:8000/home/yelesee/Desktop/Mirab/Mirab/data/FuzzyMirab1.xml
我该如何解决。
我已经编辑了MEDIA_ROOT ='/ data /' 并在localhost:8000 / data / FuzzyMirab1.xml中发送正确的根目录 却说找不到文件