gpt4 book ai didi

python - 模板渲染 Django 1.6 期间的 Dijan 错误

转载 作者:行者123 更新时间:2023-11-28 19:54:52 26 4
gpt4 key购买 nike

我正在尝试使用 django 1.6 进行项目。但在代码第 5 行下方传递错误。没有该行,一切正常。我是新程序员。期待一些专家的帮助。

1   {% extends "base.html" %}
2
3 {% block content %}
4 {% for post in posts_list %}
5 <h2><a href="{% url post slug=post.slug %}">{{ post.title }}</a></h2>
6 <p>{{ post.body|truncatewords:20 }}</p>
7 <p>
8 {{ post.created_at }} |
9 {% with total=post.comments|length %}
10 {{ total }} comment{{ total|pluralize }}
11 {% endwith %}
12 </p>
13 {% endfor %}
14 {% endblock %}

网址.py

from django.conf.urls import patterns, include, url
from django.views.generic import ListView, DetailView
from .models import Post

urlpatterns = patterns('',
url(r'^$', ListView.as_view(
queryset=Post.objects.all(),
context_object_name="posts_list"),
name="home"
),
url(r'^post/(?P<slug>[a-zA-Z0-9-]+)/$', DetailView.as_view(
queryset=Post.objects.all(),
context_object_name="post"),
name="post"
),
)

模型.py

from django.db import models

# Create your models here.

from django.core.urlresolvers import reverse

from djangotoolbox.fields import ListField, EmbeddedModelField


class Post(models.Model):
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
title = models.CharField(max_length=255)
slug = models.SlugField()
body = models.TextField()
comments = ListField(EmbeddedModelField('Comment'), editable=False)

def get_absolute_url(self):
return reverse('post', kwargs={"slug": self.slug})

def __unicode__(self):
return self.title

class Meta:
ordering = ["-created_at"]


class Comment(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
body = models.TextField(verbose_name="Comment")
author = models.CharField(verbose_name="Name", max_length=255)

如果您需要,我会提供更多详细信息。

===============完整回溯============================ ===========

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.6.11
Python Version: 2.7.6
Installed Applications:
('django_mongodb_engine',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'marcador')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')


Template error:
In template /home/umayanga/Desktop/mongoProject/test2/marcador/templates/marcador/post_list.html, error at line 5
coercing to Unicode: need string or buffer, Post found
1 : {% extends "base.html" %}


2 :


3 : {% block content %}


4 : {% for post in posts_list %}


5 : <h2><a href=" {% url post slug=post.slug %} ">{{ post.title }}</a></h2>


6 : <p>{{ post.body|truncatewords:20 }}</p>


7 : <p>


8 : {{ post.created_at }} |


9 : {% with total=post.comments|length %}


10 : {{ total }} comment{{ total|pluralize }}


11 : {% endwith %}


12 : </p>


13 : {% endfor %}


14 : {% endblock %}

Traceback:
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
137. response = response.render()
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/response.py" in render
105. self.content = self.rendered_content
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
82. content = template.render(context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/base.py" in render
140. return self._render(context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
196. nodelist.append(node.render(context))
File "/home/umayanga/Desktop/mongoProject/myprojec/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
439. url = reverse(project_name + '.' + view_name,

Exception Type: TypeError at /
Exception Value: coercing to Unicode: need string or buffer, Post found

最佳答案

显然,您没有在 post 周围加上引号{% url post slug=post.slug %} .

当你做 {% url post slug=post.slug %} post 的值通过的是Post来自 for 的对象在模板中循环,而不是名称为 post 的 url .这可以通过错误 coercing to Unicode: need string or buffer, Post found 找到。其中提到一个 Post找到对象。

要解决您的问题,您需要在 post 周围使用引号在你的url标记,以便它使用反向名称为 'post' 的 url .

{% url 'post' slug=post.slug %}

关于python - 模板渲染 Django 1.6 期间的 Dijan 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33055391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com