模板语法

2024-05-19 10:53:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在django模板中使用{%url%}标记时遇到问题。在

<a href="{% url baza.views.thread category.last_post.thread.pk %}">LINK</a>

引发此错误:

^{pr2}$

奇怪的是,它是这样工作的:

{{ category.last_post.thread.pk }}

返回正确的值“8”,当我这样使用它时也不会抛出错误:

<a href="{% url baza.views.thread 8 %}">LINK</a>

上面的代码运行良好,并重定向到线程。在

我的网址.py公司名称:

...
(r"^temat/(\d+)/$", "thread"),
...

岗位型号:

class Post(models.Model):
    title = models.CharField(max_length=60)
    created = models.DateTimeField(auto_now_add=True)
    creator = models.ForeignKey(User, blank=True, null=True)
    thread = models.ForeignKey(Thread)
    body = models.CharField(max_length=10000)

线程视图:

def thread(request, pk):
    posts = Post.objects.filter(thread=pk).order_by("created")
    posts = mk_paginator(request, posts, 20) # ZMIEN TAKZE W get_thread_page
    t = Thread.objects.get(pk=pk)

    return render_to_response("baza/thread.html", add_csrf(request, posts=posts, pk=pk, title=t.title,
    element_pk=t.element.pk, media_url=MEDIA_URL, path = request.path))

分类模型有“last_post”方法,返回该类别中发布的最后一条消息。在

有人能帮我解决这个烦人的问题吗?在

敬上。在

我使用的是Django版本:1.3.1


Tags: trueurltitlemodelsrequestlinkpostthread
1条回答
网友
1楼 · 发布于 2024-05-19 10:53:45

问题是下一个表达式category.last_post.thread.pk的值是None或“”。也不必为'baza.views.thread'带参数'(',)'。在

参数('',)表示category.last_post.thread.pk为None或''

相关问题 更多 >

    热门问题