【问题标题】:How to render field data into a template file using django?如何使用 django 将字段数据渲染到模板文件中?
【发布时间】:2013-03-06 21:29:12
【问题描述】:

我正在尝试使用以下代码将数据呈现到模板文件中。我遇到的错误是这样的:

This page contains the following errors:
error on line 13 at column 16: AttValue: " or ' expected

Below is a rendering of the page up to the first error.
Name,Author,Status

代码

def editbook(request):
    if request.method == 'GET':
        name = request.GET.get('name',False)
        Details = bookInfo.objects.all().filter(Name=name)
        id = Details.values_list('id',flat=True)
        Name = Details.values_list('Name',flat=True)
        Author = Details.values_list('Author',flat=True)
        Status = Details.values_list('Status',flat=True)
        return render(request, 'app/add.html', {'Name' : Name, 'Author' : Author, 'Status' : Status}, content_type="application/xhtml+xml")

模板代码

<html>
<head>
    <title>Add</title>
</head>
<body>
    <form action="add/" method="post">
        {% csrf_token %}
        <p style="font-family:Courier New;color:teal">Name <input type="text" placeholder="Name of the book" name="name"></input></p>

        <p style="font-family:Courier New;color:teal">Author <input type="text" placeholder="Author of the book" name="author"></input></p>
        <p style="font-family:Courier New; color:teal"> Status
        <select name="status">
                <option value=1>Read</option>
                <option value=1>Unread</option>
        </select>
        </p>
        <input type="submit" id="booksubmit" value="Add/Edit Book"></input>
</form>
</body>
</html>

我通过谷歌搜索,我发现这有点像 XML 解析错误(如果我错了,请纠正我)。现在我被困在这个位置上。请帮忙。

编辑这里添加书籍的表单有不同的方法将字段数据保存到数据库中。

【问题讨论】:

  • 你能显示app/add.html模板吗?
  • @Ander2 我已经添加了模板代码。

标签: python xml django


【解决方案1】:

无论是 html 还是 html5,您的 HTML 格式都是错误的。

为了挑剔,你的 python 代码也应该重构。

通常我们用小写字母而不是大写字母来定义变量

变量Details, Name, Author, Status 应该是details, name, author, status。 此外,您的班级名称bookInfo 是这样拼写的吗? python中的类应该以大写字母开头,所以bookInfo应该是BookInfo

正确的 HTML5 是这样的:

<html>
<head>
    <title>Add</title>
</head>
    <body>
        <form action="add/" method="post">
        {% csrf_token %}
        <p style="font-family:Courier New;color:teal;">Name <input type="text" placeholder="Name of the book" name="name" /></p>

        <p style="font-family:Courier New;color:teal;">Author <input type="text" placeholder="Author of the book" name="author" /></p>
        <p style="font-family:Courier New; color:teal;"> Status
            <select name="status">
                <option value=1>Read</option>
                <option value=1>Unread</option>
            </select>
            </p>
            <input type="submit" id="booksubmit" value="Add/Edit Book" />
        </form>
    </body>
</html>

如果您不使用 HTML5,这取决于您定义的 Doctype。 您不能在输入字段中使用占位符。

输入需要用/&gt; 而不是&lt;/input&gt; 关闭。

您在 html 中提供的内联样式不完整:

style="font-family:Courier New; color:teal"

应该是

style="font-family:Courier New; color:teal;"

您使用的回报不需要content_type,您可以放弃它。 你没有在任何地方使用你的模板变量,所以不是这样,但如果你想开始使用它们,模板语言的语法是 {{ variable_name }} 并且在你的情况下是(直到你重构) 以{{ Name }}, {{ Status }} 为例。

您还看到此错误是因为 content_type,因为您正在积极地告诉浏览器将文档解析为 xhtml+xml,这是带有规则的 XHTML,您实际上是在破坏它。

要将 Django 应用程序中的值添加到输入字段,请执行此操作,(无需 Django 表单)

<input type="text" value="{{ Name }}" />

但我建议改用 Django 表单。

【讨论】:

  • 我已经更改了代码,但我仍然面临同样的问题。是不是因为我试图将内容填充到text-fields而不是直接渲染到模板?
  • 你在用javascript吗?
  • 好吧,我能想象到您遇到问题的唯一地方是&lt;option value=1&gt; 尝试将 1 包含在 "" 中。还有你的 HTML 是什么文档类型?
  • 好吧,我希望 Status 的值为 integers。我仍然尝试插入引号但没有成功。我的文档中没有使用doctype
  • 好吧,HTML 在任何地方都没有格式错误。这是你所有的代码吗?
【解决方案2】:

我认为您收到此错误是因为您的模板不是“格式正确”。我的猜测是正在输出的 xml 中有错误。

您已将 http 响应的内容类型声明为 application/xhtml+xml。 xhtml 要求您在所有属性周围加上引号。您可能在某处缺少/添加了引号。我从你的模板中看不到那在哪里。

要么检查您的模板变量({{ Name }}{{ Author }}{{ Status }})输出的内容,以查看它们是否添加了杂散引号,或者尝试更改内容类型(也许只需删除 content_type 参数您正在传递给render 函数)。

【讨论】:

  • 我的render 语句(语法)是否正确?好吧,我尝试删除content_type,它没有用。但后来我尝试将{'Name' : Name, 'Author' : Author, 'Status' : Status} 更改为({{'Name' : Name}},{{ 'Author' : Author}},{{ 'Status' : Status}}),它显示:unhashable dict。怎么办?
  • 你有一个格式错误的字典,你不能去{{}}一个字典是一对{}
  • @limelights 那么这是否意味着,我之前的语法是正确的?
猜你喜欢
  • 2012-01-08
  • 1970-01-01
  • 2015-10-17
  • 2012-10-27
  • 2018-01-17
  • 2013-08-03
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
相关资源
最近更新 更多