是否可以访问views.py中的静态文件?

编程入门 行业动态 更新时间:2024-10-26 22:29:55
本文介绍了是否可以访问views.py中的静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以通过直接url( localhost:8000 / static / maps / foo.txt )访问有问题的静态文件,所以我想我拥有这一切工作得很好但是我不能这样做:我想在view.py中打开这个文本文件。这是因为我正在开发一个简单的网络浏览器冒险游戏,我想在静态/地图中存储地图,并使用 f = open('/ static / maps /'+ mapname +')加载这些地图。 txt','r')。我得到 IOError:没有这样的文件或目录。我真的不明白,因为当我在地址中搜索这样的目录

I am able to access the static file in question via direct url (localhost:8000/static/maps/foo.txt), so I guess I have it all working well. But I can't do the following: I want to open that text file in views.py. It's because I'm working on a simple web browser adventure game and I wanted to store maps in static/maps and load those maps using f=open('/static/maps/' + mapname + '.txt', 'r'). I get the IOError: no such file or directory. I really don't understand it, because there is such directory when I search for it in address.

可以以某种方式完成吗? / p>

Can it be done somehow?

推荐答案

您需要使用存储在磁盘上的位置,这可能位于 settings.STATIC_ROOT 或 settings.STATICFILES_DIRS ,而不是由网络应用程序提供的地方。

You need to use the place they are stored on disk, which is probably in settings.STATIC_ROOT or settings.STATICFILES_DIRS, not the place they are being served by the web app.

请注意,如果您以编程方式修改这些文件,则它们(根据定义)不是静态文件。您最好使用 MEDIA_ROOT 位置。还要注意,Django有帮助者做这样的事情 - 请参阅管理文件。

Note however that if you are modifying these files programmatically, they aren't (by definition) static files. You'd be better off using the MEDIA_ROOT location. Also note that Django has helpers to do this sort of thing - see the documentation on Managing files.

更多推荐

是否可以访问views.py中的静态文件?

本文发布于:2023-11-08 11:44:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569279.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   文件   views   py

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!