gpt4 book ai didi

python - 如何在 Django 模板中使用带有外键对象的 get_absolute_url()

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:26 24 4
gpt4 key购买 nike

我是 django 的新手,我已经搜索过这个问题,但找不到解决方案。如果解决方案很明显但我似乎无法正确解决,请原谅我。

所以,这就是问题所在。我有两个模型 ParishionerCommunity。 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 页面。它基本上会重新加载页面。

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

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

你可能应该这样做:

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

最好将 communities 字段更改为单数(即 community),因为它是一个外键字段,因此不会造成混淆;一个教区居民到一个社区(而不是多个社区)。

关于python - 如何在 Django 模板中使用带有外键对象的 get_absolute_url(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47182268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com