Django:在视图中访问时,settings.py中的重要字符被破坏 [英] Django: Accented characters in settings.py are broken when accessed in a view

查看:91
本文介绍了Django:在视图中访问时,settings.py中的重要字符被破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的settings.py中使用getattr(设置,'MY_CONSTANT_NAME',[])访问的字符,但getattr()调用返回破损的字符(例如ô成为: \\ xc3 \xb4)。



这里是view.py中的代码:

  from django .conf导入设置

def getValueFromSetting(request):
mimetype ='application / json'
charset = utf-8'datasources = getattr(settings,'MY_CONSTANT_NAME',[ ])
config ='{'
config + ='datasources:'+ str(datasources).replace(','')
config + ='}'

return HttpResponse(config,mimetype)

解决问题:




  • 我把# - - 编码:utf-8 - 我的settings.py和我的views.py

  • 我在设置中的特殊字符前加了u'ô'或unicode('ô')。

  • 我尝试了所有可能的.decode('utf-8'),.encode('utf-8'),在settings.py中或在views.py ...
  • $ b中的特殊字符上的'8'),.decode('iso-8859-1'),.encode('iso-8859-1' $ b


没有解决问题的方法。



有任何建议可以解决这个问题吗?



谢谢



Etienne

解决方案

我假设您在浏览器中看到这些 \xc3\xb4 字符串。您是否尝试编辑模板文件以在HTML标题中定义正确的字符集?

 < head> 
< meta name =descriptioncontent =example/>
< meta name =keywordscontent =something/>
< meta name =authorcontent =Etienne/>
< meta charset =UTF-8/> <! - < ----这一行 - >
< / head>

在此答案中的第一则留言之后编辑



我怀疑 getattr 不能用于 ascii 编码。

 从django.conf导入设置

def getValueFromSetting(request):
myConstantValue = settings.MY_CONSTANT_NAME
#检查myConstantValue here


$ b b

在最后一条评论后修改:



我想现在我明白你的问题了。你不喜欢视图返回的JSON只有ASCII的事实。我建议您使用与Python捆绑的 json 模块提供的 dumps 函数。下面是一个例子:

 # -  *  -  coding:utf-8  -  *  -  
# b $ b import json

def dumpjson(request):
response = HttpResponse(json.dumps(settings.CONSTANT_TUPLE,encoding ='utf-8',ensure_ascii = False),content_type = 'application / json')

返回响应

CONSTANT_TUPLE 在示例中只是我的 settings.py 中的 DATABASES



这里的重要一点是 ensure_ascii = False 。你能试试吗?这是你想要的吗?


I have accented characters in my settings.py that I access in a view using getattr(settings, 'MY_CONSTANT_NAME', []) but the getattr() call return broken characters (for example, "ô" become: "\xc3\xb4").

here is the code in view.py:

    from django.conf import settings

    def getValueFromSetting(request):
        mimetype = 'application/json' 
        charset=utf-8' datasources = getattr(settings, 'MY_CONSTANT_NAME', []) 
        config= '{' 
        config+= '"datasources": ' + str(datasources).replace("'", '"') 
        config+= '}'

        return HttpResponse(config,mimetype)                      

What I have done so far to try to solve the problem:

  • I put # -- coding: utf-8 -- as the first line of my settings.py and my views.py
  • I put u'ô' or unicode('ô') in front of special characters in settings.py
  • I put DEFAULT_CHARSET = 'utf-8' in settings.py
  • I try all possible combination of .decode('utf-8'), .encode('utf-8'), .decode('iso-8859-1'), .encode('iso-8859-1') on the special characters in settings.py or in the views.py...

Nothing solve the problem.

Any suggestion to solve this problem?

Thank you

Etienne

解决方案

I assume you're seeing these \xc3\xb4 strings in your browser.. Have you tried editing your template file to define the proper charset in the HTML header?

<head>
  <meta name="description" content="example" />
  <meta name="keywords" content="something" />
  <meta name="author" content="Etienne" />
  <meta charset="UTF-8" />      <!--  <---- This line -->
</head>

Edit after your first comment in this answer:

I suspect getattr will not work with other than ascii encoding. Do you think something like the following will not do what you want?

from django.conf import settings

def getValueFromSetting(request):
    myConstantValue = settings.MY_CONSTANT_NAME
    # check myConstantValue here

Edit after last comments:

I think now I understand your problem. You don't like the fact that the JSON returned by the view is ASCII-only. I recommend you to use dumps function provided by the json module bundled with Python. Here's an example:

# -*- coding: utf-8 -*-
# other required imports here
import json

def dumpjson(request):
   response = HttpResponse(json.dumps(settings.CONSTANT_TUPLE, encoding='utf-8', ensure_ascii=False), content_type='application/json')

   return response

The CONSTANT_TUPLE in the example is just a copy of DATABASES in my settings.py.

The important bit here is ensure_ascii=False. Could you try it? Is that what you want?

这篇关于Django:在视图中访问时,settings.py中的重要字符被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆