简体   繁体   English

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

[英]Django | Auto Escape On / Off | Single Quotes | JSON

I have address field in a form. 我在表格中有地址栏。 I'm sending that form to web server after converting it to json. 我将表单转换为json后发送到Web服务器。 Unexpectedly when a user enters the text 'X' Road in the address field, it is escaping using \\ . 出乎意料的是,当用户在地址字段中输入文本'X' Road ,会使用\\转义。 I saved that info in database and then sending back to client. 我将该信息保存在数据库中,然后发送回客户端。

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

But unfortunately here escape characters are not presented. 但是不幸的是,这里没有显示转义字符。 The tag is closing when encountering the text 'X Road' 遇到文字'X Road'时,该标签正在关闭

Here is the snippet... 这是代码段...

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}}';

It is not supposed to raise the error, but I get an error in the console! 它不应该引发错误,但是控制台中出现错误!

在此处输入图片说明

Questions: 问题:
How to resolve the issue? 该如何解决?
What exactly is causing the issue? 到底是什么引起了该问题?

The autoescape tag escapes special HTML characters ( < and > ), not single quotes. autoescape标记转义特殊的HTML字符( <> ),而不是单引号。

If you want to escape single quotes, you can write a custom template filter . 如果要转义单引号,可以编写自定义模板过滤器

For example: 例如:

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("'", "\\'")

If you do not wish to use a template filter, what you can do is this: 如果您不希望使用模板过滤器,则可以执行以下操作:

{# 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>

This second solution is better practice because you are not rendering the JS with Django which means it can be moved to an external file. 第二个解决方案是更好的做法,因为您没有使用Django渲染JS,这意味着可以将其移动到外部文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM