Django |自动退出开/关|单引号| JSON

时间:2016-02-02 15:17:17

标签: python django django-templates escaping

我在表单中有地址字段。我将该表单转换为json后将其发送到Web服务器。出乎意料的是,当用户在地址字段中输入文本'X' Road时,它正在使用\转义。我将该信息保存在数据库中,然后发送回客户端。

jresp = '{% autoescape on%}{{jresp}}{% endautoescape %}';

但不幸的是,这里没有出现转义字符。遇到文本'X Road'

时,代码正在关闭

这是片段......

jresp = 
'{"data":  
    {"alt_mobile": "7396623933", "father": "Janaiah", "gender": true, "surname": "Boddu", 
     "mobile": "9010101046", "religion": "Hindu", "mother": "Parvathi", 
     "address": "Khammam 'X' Road, Road No: 5", "dob": "14 July, 1995", 
     "cast": "Bc - B", "lastname": "Sai ", "temp_address": "Hyderabad", 
     "firstname": "Gowtham", "mother_maiden_name": "Mothukuri"}, 
     "rollno": "12261A0109", "academics": 
                                     {"mtech_pref": 2, "abckl_total": 0, 
                                      "aieeerank": 44978, "engg_t_agg": 69.32, 
                                      "job_pref": 1, "sem4_total": 750, "sem1_pbckl": 0, 
                                      "verify": false, "sem7_pbckl": 0, 
                                      "x2class_year_of_pass": 2012, "sem2_abckl": 0, 
                                      "x2class_board": "BIEAP", "sem8_marks": 0, 
                                      "xclass_total": 700, "sem3_total": 750,  
                                      "x2class_t_agg": 89.8, "ms_pref": 3, "sem5_abckl": null,  
                                      "sem4_marks": 515, "x2class_total": 1000, "sem2_marks": 545,  
                                      "sem6_pbckl": 0, "sem3_pbckl": 0, "evaluation_test": false,  
                                      "sem5_pbckl": null, "pbckl_history": true, "sem6_total": 0,  
                                      "is_icse": false, "is_inter": true,  
                                      "abckl_history": false, "sem8_pbckl": 0, "comp_xclass_t_agg": 3,  
                                      "is_ssc": false, "sem1_total": 1000, "x2class_name": "CVR",  
                                      "x2class_marks": 898, "branch": 1, "sem6_abckl": 0,  
                                      "xclass_t_agg": 89.71, "sem1_abckl": 0, "pbckl_total": 0,  
                                      "xclass_name": "SVVN", "sem8_total": 0, "xclass_marks": 628,  
                                      "sem4_abckl": 0, "sem2_pbckl": 0, "mba_pref": 4,  
                                      "sem7_marks": 0, "sem6_marks": 0, "xclass_year_of_pass": 2010,  
                                      "sem7_abckl": 0, "sem2_total": 750, "sem7_total": 0,  
                                      "comp_x2class_t_agg": 3, "sem4_pbckl": 0,  
                                      "comp_engg_t_agg": 2, "sem1_marks": 693, "is_isce": null,  
                                      "sem3_marks": 500, "sem3_abckl": 0, "sem5_marks": null,  
                                      "xclass_board": "SSC", "task_reg": false, "cetrank": 23592,  
                                      "sem8_abckl": 0, "sem5_total": null}}';

它不应该引发错误,但我在控制台中出错!

enter image description here

的问题:
如何解决这个问题?
究竟是什么导致了这个问题?

1 个答案:

答案 0 :(得分:2)

autoescape代码会转义特殊的HTML字符(<>),而非单引号。

如果要转义单引号,可以写一个custom template filter

例如:

from django import template


register = template.Library()


@register.filter
def escape_single_quotes(string):
    # The two backslashes are interpreted as a single one
    # because the backslash is the escaping character.
    return string.replace("'", "\\'")

如果您不想使用模板过滤器,可以这样做:

{# Note the type is not "text/javascript" so the browser does not try to interpret the content. #}
<script id="jresp" type="application/json">{{ jresp }}</script>
<script>
  var jsresp = document.getElementById('jsreps').innerHTML;
</script>

第二种解决方案是更好的练习,因为你没有使用Django渲染JS,这意味着它可以移动到外部文件。