Django模板中使用args的get_absolute_url()不起作用。

huangapple go评论88阅读模式
英文:

Django template with get_absolute_url() using args isn't working

问题

I keep getting an error that says person_id is not defined

我一直得到一个错误,说person_id未定义

英文:

Having trouble using get_absolute_url() with args to work in my template (perhaps I'm doing this incorrectly on the template side)

I keep getting an error that says person_id is not defined

I've got a simple model that looks like this:

class Course(models.Model):
    year = models.ForeignKey(Year, on_delete=models.PROTECT)
    name = models.CharField(max_length=50)
    short_name = models.CharField(max_length=50)
    created = models.DateTimeField(auto_now=False, auto_now_add=True)
    last_updated = models.DateTimeField(auto_now=True, auto_now_add=False)

It has get_absolute_url that looks like this:

    def get_absolute_url(self):
        """Return absolute url for Course."""
        return reverse(
            "view_course", args=[
                person_id, 
                location_id, 
                str(self.id)
            ]
        )

and I've got my URL which looks like this:

    path(
        "<person_id>/locations/<location_id>/course/<pk>/",
        views.CourseDetailView.as_view(),
        name="view_course",
    ),

In my template I've tried a few different ways to get this to display, currently it looks like this:

    {% for section in sections %}
        <a id="course{{section.course.id}}" href="{{person.id}}/locations/{{location.id}}/course/{{ section.course.get_absolute_url }}" class="card-link location-course-link">{{ location.person }}</a>{{section.course.short_name}}
    {% endfor %}

I have also tried: href="{{ section.course.get_absolute_url }}"

Also tried: href="{{ section.course.get_absolute_url person.id location.id }}"

While all the data is correct (the url comes out to 1/locations/3/course/5/ as expected in the template - it still gives that error when clicked on.

答案1

得分: 2

不能使用 get_absolute_url 来实现这个get_absolute_url 方法 [Django文档] 的目的是:

> 定义一个 get_absolute_url() 方法,告诉Django如何计算对象的规范URL。对于调用者来说,这个方法应该返回一个可以用于通过HTTP引用对象的字符串。

这意味着URL完全由对象确定(通过它的字段或相关对象的字段)。但是就我所见,这里没有办法派生出person_id

你可以使用 **{% url ... } 模板标签 [Django文档] 来计算反向URL:

<pre><code>&lt;a id=&quot;course{{section.course.id}}&quot; href=&quot;&lt;b&gt;{% url 'view_course' person.id location.id section.course.id %}&lt;/b&gt;&quot; class=&quot;card-link location-course-link&quot;&gt;{{ location.person }}&lt;/a&gt;</code></pre>
英文:

You can not use get_absolute_url for that. The get_absolute_url method [Django-doc] has as purpose:

> Define a get_absolute_url() method to tell Django how to calculate the canonical URL for an object. To callers, this method should appear to return a string that can be used to refer to the object over HTTP.

So that means the url is entirely determined by the object (through it fields, or through fields of related objects). But here there is, as far as I can see, no way to derive the person_id.

What you use the <code>{% url &hellip; }</code> template tag [Django-doc] to calculate the reverse url:

<pre><code>&lt;a id=&quot;course{{section.course.id}}&quot; href=&quot;<b>{% url 'view_course' person.id location.id section.course.id %}</b>&quot; class=&quot;card-link location-course-link&quot;&gt;{{ location.person }}&lt;/a&gt;</code></pre>

huangapple
  • 本文由 发表于 2020年1月6日 21:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定