在django模板中处理u'\x00'

时间:2022-05-27 11:42:27

Some of the fields in my model (through a mysql db) returns a u'\x00' when accessed, and I want to detect this in a django template. Here is what I see:

我的模型中的一些字段(通过mysql db)在被访问时返回u'\x00',我希望在django模板中检测到这一点。以下是我所看到的:

(Pdb) patient.address.fax
u'\x00'

I am trying to do the following in the template ...

我正在尝试在模板中做以下事情……

{% if person.address.fax != u"\x00"  %}
   <abbr  class="icon-print" title = "Phone"> </abbr>  {{person.address.fax}} </br> 
{% endif %}

I get the following error with the above template code:

上面模板代码有以下错误:

Could not parse the remainder: '"\x00"' from 'u"\x00"'

Essentially, I do not want to print the fax number if person.address.fax = u'\x00'. How do I do this?

实质上,如果是person.address,我不想打印传真号。传真= u ' \ x00”。我该怎么做呢?

Background:

背景:

I export a MSSQL DB using bcp and import the data into MySQL. When I look at the exported data from MSSQL in emacs, I see a bunch of "^@" (without quotes) in them. After the import this file, I suspect this ^@ shows up as \x00 (a NULL character?). There is probably some way to prevent the MSSQL from exporting this differently, but it is going to complicate things for me, and I would rather have the answer to the question above.

我使用bcp导出MSSQL DB并将数据导入MySQL。当我看着导出的数据从该软件在emacs中,我看到一群“^ @”(不含引号)。导入这个文件后,我怀疑这^ @显示为\ x00(NULL字符?)。可能有某种方法可以防止MSSQL以不同的方式导出这些内容,但这对我来说会使事情变得复杂,我更希望得到上面问题的答案。

2 个解决方案

#1


0  

You can pass in u'\x00' as a context variable to compare against, as in

您可以将u'\x00'作为上下文变量进行比较,如in

#!/usr/bin/env python2.7

from __future__ import print_function

from django.template import Template, Context
from django.conf import settings

settings.configure()

c = Context({'null': u'\x00', 'dbresult': u'\x00'})

t = Template(r"""
{% if dbresult == null %}
result is null
{% else %}
result is {{ dbresult }}
{% endif %}
""")

print(t.render(c))

which correctly prints result is null.

正确地输出结果为null。

#2


1  

  1. Django's templating considers unicodes to be strings; you don't need the u prefix.

    Django模板将unicodes视为字符串;你不需要u前缀。

  2. You used the wrong charset on import; you need to set the charset to UTF-16LE first.

    你在进口上使用了错误的字符集;您需要首先将字符集设置为UTF-16LE。

  3. bcp "helpfully" outputs NUL rather than an empty string in CSV where the original field is NULL; either tell bcp to knock it off, or perform an UPDATE query that sets each field to NULL where it's equal to '\0'.

    bcp“有用”输出NUL,而不是CSV中原始字段为空的空字符串;要么告诉bcp关闭它,要么执行一个更新查询,将每个字段设置为NULL,其值为'\0'。

#1


0  

You can pass in u'\x00' as a context variable to compare against, as in

您可以将u'\x00'作为上下文变量进行比较,如in

#!/usr/bin/env python2.7

from __future__ import print_function

from django.template import Template, Context
from django.conf import settings

settings.configure()

c = Context({'null': u'\x00', 'dbresult': u'\x00'})

t = Template(r"""
{% if dbresult == null %}
result is null
{% else %}
result is {{ dbresult }}
{% endif %}
""")

print(t.render(c))

which correctly prints result is null.

正确地输出结果为null。

#2


1  

  1. Django's templating considers unicodes to be strings; you don't need the u prefix.

    Django模板将unicodes视为字符串;你不需要u前缀。

  2. You used the wrong charset on import; you need to set the charset to UTF-16LE first.

    你在进口上使用了错误的字符集;您需要首先将字符集设置为UTF-16LE。

  3. bcp "helpfully" outputs NUL rather than an empty string in CSV where the original field is NULL; either tell bcp to knock it off, or perform an UPDATE query that sets each field to NULL where it's equal to '\0'.

    bcp“有用”输出NUL,而不是CSV中原始字段为空的空字符串;要么告诉bcp关闭它,要么执行一个更新查询,将每个字段设置为NULL,其值为'\0'。