英文:
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><a id="course{{section.course.id}}" href="<b>{% url 'view_course' person.id location.id section.course.id %}</b>" class="card-link location-course-link">{{ location.person }}</a></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 … }</code> template tag [Django-doc] to calculate the reverse url:
<pre><code><a id="course{{section.course.id}}" href="<b>{% url 'view_course' person.id location.id section.course.id %}</b>" class="card-link location-course-link">{{ location.person }}</a></code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论