django 页面未找到(404)页面未找到

y4ekin9u  于 2023-08-08  发布在  Go
关注(0)|答案(2)|浏览(149)

我试图使用context_processors.py文件激活导航栏链接。在这里,我使用了一个下拉菜单和一些链接来加载同一个html页面中的内容。下拉菜单用于选择“类别”,链接用于选择“流派”。“类别”应用程序用于通过管理面板添加类别,“流派”应用程序用于添加流派。“评论”应用程序用于显示详细信息。在“评论”应用程序模型中创建“详细信息”,并在“类别”应用程序中创建“类别”模型,并在“流派”应用程序中使用ForeignKey在“详细信息”模型中定义“流派”模型。我想在单击链接时在同一页面(index.html)中显示项目。下拉菜单中给出的链接工作正常,但当我单击下拉菜单中提到的链接以外的链接不工作时,它显示如下的404错误:

Page not found (404)

No category matches the given query.

Request Method:     GET
Request URL:    http://127.0.0.1:8000/drama/
Raised by:  review.views.index

Using the URLconf defined in movie.urls, Django tried these URL patterns, in this order:

admin/
[name='index']
<slug:category_slug>/ [name='index_by_category']

The current path, drama/, matched the last one.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

字符串
谁能提出一个解决方案。
HTML页面:

{% extends 'base.html' %}
{% block content %}
<nav class="navbar navbar-main navbar-expand-lg navbar-light border-bottom">
  <div class="container">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#main_nav" aria-controls="main_nav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="main_nav">
      <ul class="navbar-nav">
        <li class="nav-item dropdown">
          <a class="nav-link pl-0" data-toggle="dropdown" href="#"><strong> <i class="fa fa-bars"></i> &nbsp  Category</strong></a>
          <div class="dropdown-menu">
              <a class="dropdown-item" href="{% url 'index' %}">All Category</a>
              <div class="dropdown-divider"></div>
              {% for i in links %}
                <a class="dropdown-item" href="{{i.get_url}}">{{i.category_name}}</a>
              {% endfor %}
          </div>
        </li>
           {% for x in genre_links %}
        <li class="nav-item">
          <a class="nav-link" href="{{x.get_url}}">{{x.genre_name}}</a>
        </li>
            {% endfor %}
      </ul>
    </div> 
  </div> 
</nav>


urls.py

from django.urls import path
from . import views
urlpatterns=[
    path('',views.index,name="index"),
    path('<slug:category_slug>/',views.index,name="index_by_category"),
    path('<slug:genre_slug>/',views.index_by_genre,name="index_by_genre"),
    ]


views.py

from django.shortcuts import render,redirect,get_object_or_404
from review.models import details
from category.models import category
from genre.models import genre

def index(request,category_slug=None):
    if category_slug!=None:
        categories = get_object_or_404(category,slug=category_slug)
        movies = details.objects.filter(Category=categories)
    else:
        movies = details.objects.all()
    return render(request,"index.html",{'movies':movies})

def index_by_genre(request,genre_slug=None):
    if genre_slug != None:
        genres = get_object_or_404(genre, slug=genre_slug)
        movies = details.objects.filter(Genre=genres)
    else:
        movies = details.objects.all()
    return render(request,"index.html",{'movies':movies})


context_processors.py(category)

from .models import category
def menu_links(request):
    links = category.objects.all()
    return dict(links=links)


context_processors.py(genre)

from .models import genre

def menu_links(request):
    genre_links = genre.objects.all()
    return dict(genre_links=genre_links)


models.py(genre)

from django.db import models
from django.urls import reverse

class genre(models.Model):
    genre_name = models.CharField(max_length=50,unique=True)
    slug = models.SlugField(max_length=100,unique=True)
    description = models.TextField(max_length=255,blank=True)
    genre_image = models.ImageField(upload_to='categories',blank=True)

class Meta:
    verbose_name = 'genre'
    verbose_name_plural = 'genres'

def get_url(self):
    return reverse('index_by_genre', args=[self.slug])

def __str__(self):
    return self.genre_name


models.py(genre)

from django.db import models
from django.urls import reverse

class category(models.Model):
    category_name = models.CharField(max_length=50,unique=True)
    slug = models.SlugField(max_length=100,unique=True)
    description = models.TextField(max_length=255,blank=True)
    cat_image = models.ImageField(upload_to='categories',blank=True)

class Meta:
    verbose_name = 'category'
    verbose_name_plural = 'categories'

def get_url(self):
    return reverse('index_by_category',args=[self.slug])

def __str__(self):
    return self.category_name


models.py(评论应用程序)

from django.db import models
from category.models import category
from genre.models import genre

class details(models.Model):
    movie_name = models.CharField(max_length=250)
    description = models.CharField(max_length=5000)
    director = models.CharField(max_length=100)
    writer = models.CharField(max_length=100)
    cast = models.CharField(max_length=1000)
    year_release = models.CharField(max_length=20)
    time = models.CharField(max_length=30)
    image = models.ImageField(upload_to='images')
    Category = models.ForeignKey(category,on_delete=models.CASCADE)
    Genre = models.ForeignKey(genre,on_delete=models.CASCADE)

def __str__(self):
    return str(self.movie_name)

hwamh0ep

hwamh0ep1#

修改urls.py文件,这样我们就可以解决这个问题。从urls.py文件中删除最后两个路径。将path('<slug:category_slug>/',views.index,name="index_by_category"),代码放在类别应用的www.example.com文件中,并将path('<slug:genre_slug>/',views.index_by_genre,name="index_by_genre"),放在类型应用的urls.py文件中。同时从views.py中删除'index_by_genre'函数,并将其放在类型应用的views.py文件中。同样对于'category'。不要忘记在应用URL和项目URL之间给予连接。urls.py

n8ghc7c1

n8ghc7c12#

在我的情况下,我交换了路径,它得到了。我的意思不是:

path('<slug:category_slug>/',views.index,name="index_by_category"),
path('<slug:genre_slug>/',views.index_by_genre,name="index_by_genre"),

字符串
执行:

path('<slug:genre_slug>/',views.index_by_genre,name="index_by_genre"),
path('<slug:category_slug>/',views.index,name="index_by_category"),

相关问题