设置值属性的错误条件(Set if error condition for value attribute)

编程入门 行业动态 更新时间:2024-10-15 08:21:45
设置值属性的错误条件(Set if error condition for value attribute)

在以下模板代码中,我设置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.

更多推荐

本文发布于:2023-08-07 16:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465383.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   条件   错误   Set   condition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!