编程语言
首页 > 编程语言> > python-如何在Django模板中将get_absolute_url()与外键对象一起使用

python-如何在Django模板中将get_absolute_url()与外键对象一起使用

作者:互联网

对于django来说,它相对较新,我已经搜索了此问题,但找不到解决方案.如果解决方案显而易见,请原谅我,但似乎无法正确解决.

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

楷模:

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"

观看次数:

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 %}

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

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

解决方法:

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

您可能应该这样做:

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

最好将社区字段更改为单数(即社区),因为它是外键字段,因此不那么令人困惑;一个社区的教区居民(不是多个社区).

标签:django-templates,django-urls,python,django,django-models
来源: https://codeday.me/bug/20191110/2015119.html