将Json文件用作站点信息和设置(Django)是不是正确? [关闭]

Posted

技术标签:

【中文标题】将Json文件用作站点信息和设置(Django)是不是正确? [关闭]【英文标题】:Is it right to use the Json file as a site information and settings (Django)? [closed]将Json文件用作站点信息和设置(Django)是否正确? [关闭] 【发布时间】:2021-08-02 13:06:59 【问题描述】:

我有一个 django 项目。我想使用 JSON 文件获取站点信息和设置(例如电话号码 | 关于文本 | 联系我们信息)。

像这样--->


   "about_text": "Hi this is about text",
   "phone": "0912------984",
   "email": "text@text.com"

并在视图中使用这些...

import json
from django.shortcuts import render

def about(request):
    with open("data_file.json") as f:
        data = json.load(f)
    context = 
        "about_us_text": data['about_text']  # Using *marked* lib to render to html (In template)
    
    return render(request, 'about.html', context)

正确吗?解决方案不是最好的吗?它不会导致 有问题吗?

我认为有更好的解决方案,因为这可能需要时间和 放慢程序... .

【问题讨论】:

【参考方案1】:

据我所知,到django3.2为止,还没有官方选择告诉哪个最好,json、yaml、ini、环境变量等。

但它不应该减慢程序,因为 django 只读取一次数据。

【讨论】:

【参考方案2】:

在用户端获取此请求以接收和使用 Json 文件是否是更好的解决方案?

像这样...(返回)

from django.shortcuts import render

def about(request):
    return render(request, 'about.html', context)

然后使用这个 JS/jQuery 脚本?

$.getJSON('http://example.com/info.json', function(data) 
        var text = `$data.about_text`;
        $(".about").html(text);
);

认为这样更好,因为它发送来自用户的请求,而获取操作是在用户端完成的。

【讨论】:

以上是关于将Json文件用作站点信息和设置(Django)是不是正确? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 django 设置文件中添加相同站点无选项

如何将 manifest.json 的 start_url 设置为站点的根目录?

从 django 模板中转义 JSON 数据的正确方法

使用 django 测试客户端发送 JSON

在django里如何设置响应头的头信息

使用 XML 或 JSON 文件作为数据库后端的 Django