找不到参数为'(','...')'的'...'的Django反转,尝试了1个模式:

odopli94  于 2023-03-09  发布在  Go
关注(0)|答案(2)|浏览(146)

需要帮助,我试过很多解决方案,但都不起作用,总是一个错误,请帮助我解决问题。
http://127.0.0.1:8000/seasons/season-1/episode-2未找到带参数“(',”episode-1')“的”episode-detail“的反向NoReverseMatch。尝试了1个模式:['季节/(?P<seasonpost_slug>[-a-zA-Z 0 -9_]+)/(?P<episodepost_slug>[-a-zA-Z 0 -9_]+)\Z']
模板呈现期间出错,以前
返回render(请求,'content/episode.html',{...本地变量当前_一集〈一集:第2集〉第2集〈查询集[〈第2集:第2集〉]〉episodepost_slug '第2集'下一集〈第1集:发作-3〉上一发作〈发作:第1集〉请求〈WSGI请求:GET '/季节/第一季/第二集'〉季节后_鼻涕虫'第一季'

class season(models.Model):
            slug = models.SlugField(max_length=266, unique=True, db_index=True, verbose_name=('SLUG'))
            ...

            def __str__(self):
                return self.slug

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


        class episode(models.Model):
            slug = models.SlugField(max_length=266, unique=True, db_index=True, verbose_name=('SLUG'))
            ...
            
            cat = models.ForeignKey('season', on_delete=models.PROTECT, null=True)

            def __str__(self):
                return self.slug
            
            
            def get_absolute_url(self):
                return reverse('episodepost', kwargs={'seasonpost_slug': self.slug, 'episodepost_slug': self.slug})
                

        Views
                
        def ContentHome(request):
            homepages=home.objects.all()
            season_menu=season.objects.all()
            seasonpages=season.objects.all()
            return render(request, 'content/home.html',{
                'season_menu':season_menu, 
                'home':homepages, 
                'season':seasonpages})

        def ContentSeasons(request):
            seasonspages=seasons.objects.all()
            homepages=home.objects.all()
            season_menu=season.objects.all()
            seasonpages=season.objects.all()
            return render(request, 'content/seasons.html',{
                'season_menu':season_menu, 
                'home':homepages, 
                'seasons':seasonspages, 
                'season':seasonpages})

        def ContentSeason(request, seasonpost_slug):
            seasonpages = get_list_or_404(season, slug=seasonpost_slug)
            homepages=home.objects.all()
            seasonspages=seasons.objects.all()
            season_menu=season.objects.all()
            episodepost = episode.objects.filter()
            seasonpages=season.objects.filter(slug=seasonpost_slug)
            return render(request, 'content/season.html',{
                'home':homepages, 
                'seasons':seasonspages, 
                'season':seasonpages, 
                'season_menu':season_menu, 
                'episode': episodepost,})


        def ContentEpisode(request, seasonpost_slug, episodepost_slug):
            episodepost = episode.objects.filter(slug=episodepost_slug, cat__slug=seasonpost_slug)
            current_episode = get_object_or_404(episodepost, slug=episodepost_slug, cat__slug=seasonpost_slug)
            previous_episode = episode.objects.filter(cat__slug=seasonpost_slug, pk__lt=current_episode.pk).first()
            next_episode = episode.objects.filter(cat__slug=seasonpost_slug, pk__gt=current_episode.pk).first()
            
            return render(request, 'content/episode.html', {
                'episode': episodepost,
                'current_episode': current_episode,
                'previous_episode': previous_episode,
                'next_episode': next_episode,
        })

            path('', views.ContentHome, name='homepage'),
            path('seasons', views.ContentSeasons),
            path('seasons/<slug:seasonpost_slug>', views.ContentSeason, name='episode-detail'),
            path('seasons/<slug:seasonpost_slug>/<slug:episodepost_slug>', views.ContentEpisode, name='episode-detail'),
            
        Template
            
        {% if previous_episode %}
        <span class="span__bootons-navigatiom"><a href="{% url 'episode-detail' previous_episode.seasonpost.slug previous_episode.slug, %}" title="previous">previous</a></span>
        {% endif %}
        {% if next_episode %}
        <span class="span__bootons-navigatiom"><a href="{% url 'episode-detail' next_episode.seasonpost.slug next_episode.slug, %}" title="next">next</a></span>
        {% endif %}
        </div>
eoigrqb6

eoigrqb61#

不能有两个同名url路径。
您必须正确重命名URL:

path('', views.ContentHome, name='homepage'),
path('seasons', views.ContentSeasons),
path('seasons/<slug:seasonpost_slug>', views.ContentSeason, name='season-detail'),
path('seasons/<slug:seasonpost_slug>/<slug:episodepost_slug>', views.ContentEpisode, name='episode-detail'),

在HTML模板中,不需要使用逗号分隔参数:

{% if previous_episode %}
        <span class="span__bootons-navigatiom"><a href="{% url 'episode-detail' previous_episode.seasonpost.slug previous_episode.slug %}" title="previous">previous</a></span>
{% endif %}
{% if next_episode %}
        <span class="span__bootons-navigatiom"><a href="{% url 'episode-detail' next_episode.seasonpost.slug next_episode.slug %}" title="next">next</a></span>
{% endif %}
rqqzpn5f

rqqzpn5f2#

根据错误消息,href中的next_episode.seasonpost.slug似乎为空:

{% if previous_episode %}
        <span class="span__bootons-navigatiom"><a href="{% url 'episode-detail' previous_episode.seasonpost.slug previous_episode.slug, %}" title="previous">previous</a></span>
        {% endif %}
        {% if next_episode %}
        <span class="span__bootons-navigatiom"><a href="{% url 'episode-detail' next_episode.seasonpost.slug next_episode.slug, %}" title="next">next</a></span>
        {% endif %}

从你的帖子看,如果模特剧集有一个外键字段,那么它是不可见的。

  • 字段seasonpost为空或不存在
  • 字段seasonpost.slug为空或不存在
    在以下注解后编辑:

该错误消息

"....with arguments '('', 'episode-1')' not found ..."

指示您使用url标记

{% url 'name' arg1 arg2 %}

有两个参数,第一个是''(空),第二个是'episode-1'。Django url标签不允许空参数...所以请看看为什么第一个参数(previous_episode.seasonpost.slugnext_episode.seasonpost.slug)是空的。在上面的原始答案中,我在结尾给出了一些提示,为什么它们可以是空的。

相关问题