【问题标题】: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)
  • 正确吗?解决方案不是最好的吗?它不会导致 有问题吗?

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

【问题讨论】:

    标签: python json django django-views django-templates


    【解决方案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);
      });
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-05
        • 2012-07-16
        • 1970-01-01
        相关资源
        最近更新 更多