如何在Django模板中使用get_absolute_url()和外键对象

时间:2021-10-01 19:54:23

Am relatively new to django and I have searched for this problem but couldn't find a solution. Forgive me if the solution is obvious but I just can't seem to get it right.

对django来说相对较新,我已经搜索过这个问题,但找不到解决方案。请原谅我,如果解决方案是显而易见的,但我似乎无法做到正确。

So, this is the issue. I have two models Parishioner and Community. Parishioner has a many-to-one relationship with Community. On the parishioner_detail page, I am trying to display the community name as a link to the community_detail page. I feel I am not using the get_absolute_url() method correctly. Any help would be appreciated.

所以,这就是问题所在。我有两个模型Parishioner和社区。 Parishioner与社区有多对一的关系。在parishioner_detail页面上,我试图将社区名称显示为community_detail页面的链接。我觉得我没有正确使用get_absolute_url()方法。任何帮助,将不胜感激。

Models:

from django.db import models
from django.core.urlresolvers import reverse

class Community(models.Model):
    name = models.CharField(max_length=41)
    description = models.TextField()
    leader = models.CharField(max_length=41)
    email = models.EmailField()
    phone_number = models.CharField(max_length=20)
    slug = models.SlugField(max_length=31, unique=True)

    def __str__(self):
        return self.name

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

class Parishioner(models.Model):
    name = models.CharField(max_length=41)
    date_of_birth = models.DateField('date of birth', blank=True)
    email = models.EmailField()
    phone_number = models.CharField(max_length=20)
    start_date = models.DateField('date posted')
    societies = models.ManyToManyField(Society, blank=True, related_name='parishoners')
    communities = models.ForeignKey(Community, blank=True, related_name='parishoners')
    sacraments = models.ManyToManyField(Sacrament, blank=True, related_name='parishoners')
    slug = models.SlugField(max_length=31, unique=True)

    def __str__(self):
        return self.name

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

    class meta:
        ordering = ['name']
        verbose_name_plural = "parishioners"

Views:

from django.shortcuts import get_object_or_404, render, redirect
from .models import Society, Community, Sacrament, Festival, Parishioner

def community_list(request):
    return render(request, 'people/community_list.html', {'community_list': Community.objects.all()})

def community_detail(request, slug):
    community = get_object_or_404(Community, slug__iexact=slug)
    return render(request, 'people/community_detail.html', {'community': community})

def parishioner_list(request):
    return render(request, 'people/parishioner_list.html', {'parishioner_list': Parishioner.objects.all()})

def parishioner_detail(request, slug):
    parishioner = get_object_or_404(Parishioner, slug__iexact=slug)
    return render(request, 'people/parishioner_detail.html', {'parishioner': parishioner})

parishioner_detail.html:

<dt>Community</dt>
  <dd><a href="{{ community.get_absolute_url }}">{{ parishioner.communities|title }}</a></dd>
<dt>Societies</dt>
  {% for society in parishioner.societies.all %}
    <dd><a href="{{ society.get_absolute_url }}">{{ society.name|title }}</a></dd>
  {% endfor %}

The society name links correctly to the society_detail page but the community name links to the parishioner_detail page instead of the community_detail page. It basically reloads the page.

社会名称正确链接到social_detail页面,但社区名称链接到parishioner_detail页面而不是community_detail页面。它基本上重新加载页面。

Any help would be appreciated. Thanks.

任何帮助,将不胜感激。谢谢。

1 个解决方案

#1


3  

The community name does not exist in your context, the href link is therefore empty and you get redirects to the same page.

社区名称在您的上下文中不存在,因此href链接为空,您将重定向到同一页面。

You should probably be doing:

你可能应该这样做:

 <dt>Community</dt>
 <dd><a href="{{ parishioner.communities.get_absolute_url }}">{{ parishioner.communities|title }}</a></dd>

Better to change the communities field to singular (i.e. community) since it's a foreign key field, so it's less confusing; one parishioner to one community (not multiple communities).

最好将社区字段改为单一(即社区),因为它是一个外键字段,所以它不那么令人困惑;一个教区居民到一个社区(不是多个社区)。

#1


3  

The community name does not exist in your context, the href link is therefore empty and you get redirects to the same page.

社区名称在您的上下文中不存在,因此href链接为空,您将重定向到同一页面。

You should probably be doing:

你可能应该这样做:

 <dt>Community</dt>
 <dd><a href="{{ parishioner.communities.get_absolute_url }}">{{ parishioner.communities|title }}</a></dd>

Better to change the communities field to singular (i.e. community) since it's a foreign key field, so it's less confusing; one parishioner to one community (not multiple communities).

最好将社区字段改为单一(即社区),因为它是一个外键字段,所以它不那么令人困惑;一个教区居民到一个社区(不是多个社区)。