在以下模板代码中,我设置value="{{ form.title.value }}"并按我的意图显示,
<div class="form-group"> <label for="title" class="col-sm-1 control-label">Title</label> <div class="col-sm-11"> <input type="text" class="form-control" id="title" name="title" placeholder="Write Title Later" value="{{ form.title.value }}"> </div>
但是,在其他模板中,如果我设置value="{{ form.username.value }}" ,则显示None
<div class="form-group"> <label for="username" class="col-sm-1 control-label">Username</label> <div class="col-sm-11"> <input type="text" class="form-control" id="username" value="{{ form.username.value }}" name="username" placeholder="Email"> </div> </div>
我必须设置value="{% if form.username.value %}{{ form.username.value }}{%else%}{%endif%}"
<div class="form-group"> <label for="username" class="col-sm-1 control-label">Username</label> <div class="col-sm-11"> <input type="text" class="form-control" id="username" value="{% if form.username.value %}{{ form.username.value }}{%else%}{%endif%}" name="username" placeholder="Email"> </div> </div>我无法弄清楚两个表单模板之间的区别。
In the following template code, I set value="{{ form.title.value }}" and it displays as I intended,
<div class="form-group"> <label for="title" class="col-sm-1 control-label">Title</label> <div class="col-sm-11"> <input type="text" class="form-control" id="title" name="title" placeholder="Write Title Later" value="{{ form.title.value }}"> </div>However, in other template, if I set value="{{ form.username.value }}", it display None
<div class="form-group"> <label for="username" class="col-sm-1 control-label">Username</label> <div class="col-sm-11"> <input type="text" class="form-control" id="username" value="{{ form.username.value }}" name="username" placeholder="Email"> </div> </div>I have to set value="{% if form.username.value %}{{ form.username.value }}{%else%}{%endif%}"
<div class="form-group"> <label for="username" class="col-sm-1 control-label">Username</label> <div class="col-sm-11"> <input type="text" class="form-control" id="username" value="{% if form.username.value %}{{ form.username.value }}{%else%}{%endif%}" name="username" placeholder="Email"> </div> </div>I cannot figure out what's the difference between the two form templates.
最满意答案
在Python中, None和空字符串''之间存在差异。 空字符串是一个没有字符的字符串,另一方面, None根本不是字符串,但你可以看到它就像许多编程语言中的字符串可能是null一样。
默认情况下,Django将None为字符串'None' ,而不是空字符串。 但是,您可以使用default_if_none过滤器:
value="{{ form.title.value | default_if_none: '' }}"因此,如果管道符号( | )左侧的值为None我们将使用空字符串'' 。
In Python there is a difference between None, and the empty string ''. The empty string is a string with no characters, None on the other hand is not a string at all, but you can see it like what in many programming languages would probably be something like null.
By default, Django typesets None as the string 'None', and not as an empty string. You can however use the default_if_none filter:
value="{{ form.title.value | default_if_none: '' }}"Here we thus will use the empty string '' in case the value at the left of the pipe character (|) is None.
更多推荐
发布评论