django 只能加载/article/1/,/article/2/显示404

lhcgjxsq  于 2023-01-14  发布在  Go
关注(0)|答案(1)|浏览(140)

articles/x/中的每个页面代表一篇文章,每篇文章都有自己的文章内容,这些内容位于ArticleContent模型中,结构如下
| 身份证|标签|css_标识|css类|内容|article_id(指向项目表的外键)|
| - ------|- ------|- ------|- ------|- ------|- ------|
| 1个|p|||布拉|1个|
| 第二章|p|||巴豪夫斯|第二章|
实际数据库具有多个article_id的更多记录
加载/article/1/完全按预期方式加载文章内容
文章内容模型

class ArticleContent(models.Model):

    article = models.ForeignKey('Article', on_delete=models.CASCADE)
    order = models.IntegerField(blank=True)
    tag = models.CharField(max_length=20)
    css_id = models.CharField(max_length=100, blank=True)
    css_class = models.CharField(max_length=100, blank=True)
    extra = models.TextField(max_length=200, blank=True)
    content = models.TextField(max_length=2000, blank=True)

供试品型号

class Article(models.Model):

    def __str__(self) -> str:
        return self.title

    title = models.TextField(max_length=200)
    slug = models.SlugField(max_length=100, blank=True)
    preview = models.TextField(max_length=500)
    hasimage = models.BooleanField(default=True)
    image = models.ImageField(upload_to='articles/static/articles/images', blank=True)
    date = models.DateTimeField(blank=True)
    url = models.TextField(blank=True)
    alt = models.TextField(max_length=1000, blank=True)

urls.py

app_name = 'article'

urlpatterns = [
    path('', views.index, name='index'),
    path('articles/<int:pk>/', views.DetailView.as_view(), name='article_detail'),
]

视图

class DetailView(generic.DetailView):
    
    model = ArticleContent
    template_name = 'articles/article_detail.html'

    def get_queryset(self, *args, **kwargs):
        article_content = ArticleContent.objects.filter(article_id=self.kwargs['pk'])
        return article_content

    def get_context_data(self, *args, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in settings
        context['ordered_article_content'] = self.get_queryset()
        context['kwargs'] = self.kwargs['pk']
        context['is_header_on'] = True
        context['is_footer_on'] = True
        context['header_links'] = HeaderLinks.objects.all()
        context['footer_links'] = FooterLinks.objects.all()
        return context

文章_详细信息. html模板

{% extends "./base.html" %}

{% block content %}

    {% for x in ordered_article_content %}
        <{{ x.tag }} id="{{ x.css_id }}" class="{{ x.css_class }}" {{ x.extra }} >{{ x.content }}</{{ x.tag }}>

    {% endfor %}

    {{ kwargs }}

{% endblock %}

页面未找到(404)页面未找到(404)
访问article_id = 2在articles/1/页面上有效,所以我认为问题与article/2 +/有关。我不知道为什么,因为我没有在任何地方特别引用一个article_id。我想知道这是否与django如何处理我不理解的视图有关。
我只是想让article/2 +/以article/1/的方式呈现

i34xakig

i34xakig1#

您应该实现get_objectget_querysetDetailView中的主键上被额外过滤,因此:
从django.快捷方式导入get_object_or_404

class DetailView(generic.DetailView):
    model = ArticleContent
    template_name = 'articles/article_detail.html'

    def get_object(self, *args, **kwargs):
        return get_object_or_404(ArticleContent, lemons_id=self.kwargs['pk'])

    def get_context_data(self, *args, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        context['kwargs'] = self.kwargs['pk']
        context['is_header_on'] = True
        context['is_footer_on'] = True
        context['header_links'] = HeaderLinks.objects.all()
        context['footer_links'] = FooterLinks.objects.all()
        return context

该项将作为object传递给模板,因此您可以 * 不 * 使用for循环来呈现它:

<{{ object.tag }} id="{{ object.css_id }}" class="{{ object.css_class }}" {{ object.extra }} >{{ object.content }}</{{ object.tag }}>

但您可能需要ListView,因此:

class MyListView(generic.ListView):
    model = ArticleContent
    template_name = 'articles/article_detail.html'
    context_object_name = 'ordered_article_content'

    def get_queryset(self, *args, **kwargs):
        article_content = ArticleContent.objects.filter(article_id=self.kwargs['pk'])
        return article_content

    def get_context_data(self, *args, **kwargs):
        # Call the base implementation first to get a context
        context = super().get_context_data(**kwargs)
        # Add in settings
        context['kwargs'] = self.kwargs['pk']
        context['is_header_on'] = True
        context['is_footer_on'] = True
        context['header_links'] = HeaderLinks.objects.all()
        context['footer_links'] = FooterLinks.objects.all()
        return context

相关问题