gpt4 book ai didi

python - Django Slug 生成的 URL 返回 : 'No Fund matches the given query.'

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:07 25 4
gpt4 key购买 nike

我正在尝试使用 slug 来生成网址,但是无论我在哪里访问该网址,我都会收到“在/funds/[slug_ generated_value] 处找不到页面”

Raised by:  apps.funds.views.details
No Fund matches the given query

这是我的 models.py,我使用 Fund.name 生成 Fund_id 来创建首字母缩略词,并添加截止日期的日期时间值减去小时、分钟、秒值:

class Fund(models.Model):
name = models.CharField(max_length=128, unique=True)
description = models.TextField()
duration = models.CharField(max_length=10)
slug = models.SlugField(unique=True)

def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Fund, self).save(*args, **kwargs)

def __str__(self):
return self.name

class OfferedFunds(models.Model):
fund_id = models.CharField(max_length=55, blank=True)
fund_name = models.ForeignKey(Fund)
deadline = models.DateTimeField('Deadline')
slug = models.SlugField(unique=True)

def save(self, *args, **kwargs):
output = ''
for i in self.fund_name.name.upper().split():
output += i[0]
deadline = str(self.deadline)[:-15]
deadline = deadline.replace('-', '')
random_number = random.randint(1,9)
self.fund_id = '%s%s%s' % (output, deadline, random_number)
self.slug = slugify(self.fund_id)
super(OfferedFunds, self).save(*args, **kwargs)

class Meta:
verbose_name_plural = "Offered funds"

def __str__(self):
return '%s' % (self.fund_id)

以及我对应的views.py:

def details(request, slug):
context_dict = {}

fund = get_object_or_404(Fund, slug=slug)
context_dict['fund'] = fund

offered_funds = OfferedFunds.objects.filter(fund_name=fund, deadline__gte=timezone.now()).order_by('deadline')
context_dict['offered_funds'] = offered_funds

return render(request, 'funds/details.html', context_dict)

def of_details(request, slug):
context_dict = {}

of = get_object_or_404(OfferedFunds, slug=slug)
context_dict['of'] = of

return render(request, 'funds/offered-funds-details.html', context_dict)

最后是我的 urls.py:

url(r'^(?P<slug>[\w-]+)/$', views.details, name='details'),
url(r'^(?P<slug>[\w-]+)/$', views.of_details, name='of details'),

我的details() View 工作正常,但它的of_details()不起作用。如果我取出details() View ,of_details()就可以工作。有人知道我该如何解决这个错误吗?谢谢

最佳答案

好的,正如用户lambo477评论中指出的那样我的网址太相似了,所以我改变了:

url(r'^(?P<slug>[\w-]+)/$', views.of_details, name='of details'),

至:

url(r'^det/(?P<slug>[\w-]+)/$', views.of_details, name='of details'),

这很有效。

关于python - Django Slug 生成的 URL 返回 : 'No Fund matches the given query.' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33444923/

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