繁体   English   中英

如何使用django将字段数据渲染到模板文件中?

[英]How to render field data into a template file using django?

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

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解析错误(如果我错了请纠正我)。 现在我被困在这个位置。 请帮忙。

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

您的HTML格式不正确,无论是什么类型,html和html5。

而且要挑剔,你的python代码也应该重构。

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

变量Details, Name, Author, Status应该是details, name, author, status 更进一步,你的班级名称bookInfo拼写是这样的吗? 在Python类应该以大写字母开头这样bookInfoBookInfo

正确的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。 您不能在输入字段中使用占位符。

输入需要用/>而不是</input>来关闭。

您在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 Form。

我认为你得到这个错误是因为你的模板不是'格式良好'。 我的猜测是输出的xml中存在错误。

您已将http响应的内容类型声明为application/xhtml+xml xhtml要求您在所有属性周围加上引号。 您可能在某处丢失/添加引号。 我无法从你的模板中看到它的位置。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM