Django 新手
此代码将用户提交的项目添加到表中。我能得到关于 return 语句在做什么的完整解释吗?它基本上是返回一个列表项并说替换%s
为item.text
?
def add(request):
item = LineItem(text=request.POST["text"])
item.save()
return HttpResponse("<li>%s</li>" % item.text)
Django 新手
此代码将用户提交的项目添加到表中。我能得到关于 return 语句在做什么的完整解释吗?它基本上是返回一个列表项并说替换%s
为item.text
?
def add(request):
item = LineItem(text=request.POST["text"])
item.save()
return HttpResponse("<li>%s</li>" % item.text)
文本"<li>%s</li>" % item.text
是一个 python字符串格式化表达式。
字符串的%s
一部分是占位符字符串,为了填充它,python 将用str(item.text)
.
结果作为 HTTP 响应返回,可能会被 AJAX 调用使用(它不是完整的 HTML 页面)。
是的 - "string %s" % string 构造只是一种编写带有变量的字符串的方法。%s 是插入到字符串中的变量 item.text 的占位符。
这是一个字符串格式化操作。在此处查看详细信息:
http://docs.python.org/library/stdtypes.html#string-formatting
它基本上是返回一个列表项并说用 item.text 替换 %s
是的。