我想使用
get_absolute_url
但它在 combined queryset 上什么也没显示
它返回一个错误: 找不到页面 以下是我的代码
#Action/models.py
class Action(models.Model):
......
def get_absolute_url(self):
return reverse('act',args=[self.slug])
#adventure/models.py
class Adventure(models.Model):
......
def get_absolute_url(self):
return reverse('adves',args=[self.slug])
#racing/models.py
class Racing(models.Model):
......
def get_absolute_url(self):
return reverse('act',args=[self.slug])
#puzzle/models.py
class Puzzle(models.Model):
......
def get_absolute_url(self):
return reverse('puz',args=[self.slug])
然后查看我的组合查询集
#Others/views.py
from itertools import chain
def games(request):
... .....
act_games=Action.objects.all()
adv_games=Adventure.objects.all()
puz_games=Puzzle.objects.all()
rac_games=Racing.objects.all()
games= list(chain(act_games,adv_games,puz_games,rac_games))
context={
'games':games,
}
return render(request,'combined.html', context)
#combined.html
.......
{% for game in games %}
<div class="col">
<a href="{{ game.get_absolute_url }}"> {{ game.game_name }} </a>
{% endfor %}
.....
我的期望
当我刷新页面时,我没有发现任何错误。但是当我尝试点击 link 时,
它返回了一个错误
找不到页面
现在我想知道如何让它成功通过并带我到创建对象时添加的slug链接。 例如
当在动作模型中创建组合页面上的动作游戏时,它必须将我重定向到slug链接页面但只是返回我404找不到页面
有人要求提供网址,所以我决定也添加这些部分
#Action/views.py
from django.shortcuts import render, get_object_or_404
def act(request, description):
description=get_object_or_404(Action, slug=description)
Context={'description': description}
return render(request, 'description.html', Context)
#action/url.py
path('action', views.action, name='action'),
path('description/<slug:description>/', views.act, name='act' )
#Adventure/views.py
from django.shortcuts import render, get_object_or_404
def adv(request, description):
description=get_object_or_404(Adventure, slug=description)
Context={'description': description}
return render(request, 'description.html', Context)
#adventure/url.py
path('adventure', views.adventure, name='adventure'),
path('description/<slug:description>/', views.adv, name='adv' )
#puzzle/views.py
from django.shortcuts import render, get_object_or_404
def puz(request, description):
description=get_object_or_404(Puzzle, slug=description)
Context={'description': description}
return render(request, 'description.html', Context)
#puzzle/url.py
path('puzzle', views.puzzle, name='puzzle'),
path('description/<slug:description>/', views.puz, name='puz')
#Racing/views.py
from django.shortcuts import render, get_object_or_404
def rac(request, description):
description=get_object_or_404(Action, slug=description)
Context={'description': description}
return render(request, 'description.html', Context)
#Racing/url.py
path('racing', views.racing, name='racing'),
path('description/<slug:description>/', views.rac, name='rac')
就是这样,请寻求帮助