Django:类别模型中的URL错误

时间:2022-02-07 07:33:37

I created a simple category model. The

我创建了一个简单的类别模型。该

models

I created are as follows. Where do I make mistakes in these model files? Thanks for your help.

我创建如下。我在哪里可以在这些模型文件中出错?谢谢你的帮助。

models.py The code I added to the file is as follows.

models.py我添加到文件的代码如下。

class Category(models.Model):
    category_name = models.CharField(max_length=250)
    category_desc = models.TextField()
    slug = models.SlugField(max_length=250, unique=True)

class Meta:
    ordering = ('category_name',)
    verbose_name = 'category'
    verbose_name_plural = 'Categories'

def get_absolute_url(self):
    return reverse('article:categories', args=[self.slug])

def __str__(self):
    return self.category_name

views.py The code I added to the file is as follows.

views.py我添加到文件中的代码如下。

def article_category(request, category_slug):
    categories = Category.objects.all()
    article = Article.objects.filter( article_status='published')
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        article = article.filter(category=category)
    template = 'article/category.html'
    context = {'article': article}
return render(request, template, context)

url.py

url(r'^category/(?P<category_slug>[-\w]+)/$', article_category, name='article_category'),

index.html The code I added to the file is as follows.

index.html我添加到文件中的代码如下。

<a href="{{ articles.article_category.get_absolute_url }}">{{ article.article_category }}</a>

1 个解决方案

#1


0  

You should remove the non-sens categories variable in your view if you don't use it.

如果不使用,则应删除视图中的非sens类别变量。

You can't call the .filter() method on your instance. Change:

您无法在实例上调用.filter()方法。更改:

if category_slug:
    category = get_object_or_404(Category, slug=category_slug)
    article = article.filter(category=category)

to:

if category_slug:
    category = get_object_or_404(Category, slug=category_slug)
    article = Article.objects.filter(article_status='published', category=category)

You have to provide a url slug, that condition is useless anyway.

你必须提供一个url slug,无论如何这种情况都是无用的。

The article variable is a list with all of the entries that pass those filters. It must be articles.

article变量是一个列表,其中包含通过这些过滤器的所有条目。它必须是文章。

What index.html has to do with your view, if you render the article/category.html ?

如果您渲染article / category.html,index.html与您的视图有什么关系?

Why you render the article/category.html, if you want to display all articles filtered by that category?

如果要显示按该类别过滤的所有文章,为什么要呈现article / category.html?

Why aren't you using ForeignKey for this kind of relation.

为什么不使用ForeignKey来实现这种关系。

If it's a list of articles, as I said, in your template you should have something like this (supposing you renamed article with articles):

如果它是文章列表,正如我所说,在你的模板中你应该有这样的东西(假设你用文章重命名文章):

{% for article in articles %}
    <a href="{{ article.get_absolute_url }}">{{ article.name }}</a>
{% endfor %}

You haven't showed the Article model, so I supposed that there's .get_absolute_url and .name.

你没有展示文章模型,所以我认为有.get_absolute_url和.name。

Ask me if you haven't understood something.

问你,如果你不明白的话。

#1


0  

You should remove the non-sens categories variable in your view if you don't use it.

如果不使用,则应删除视图中的非sens类别变量。

You can't call the .filter() method on your instance. Change:

您无法在实例上调用.filter()方法。更改:

if category_slug:
    category = get_object_or_404(Category, slug=category_slug)
    article = article.filter(category=category)

to:

if category_slug:
    category = get_object_or_404(Category, slug=category_slug)
    article = Article.objects.filter(article_status='published', category=category)

You have to provide a url slug, that condition is useless anyway.

你必须提供一个url slug,无论如何这种情况都是无用的。

The article variable is a list with all of the entries that pass those filters. It must be articles.

article变量是一个列表,其中包含通过这些过滤器的所有条目。它必须是文章。

What index.html has to do with your view, if you render the article/category.html ?

如果您渲染article / category.html,index.html与您的视图有什么关系?

Why you render the article/category.html, if you want to display all articles filtered by that category?

如果要显示按该类别过滤的所有文章,为什么要呈现article / category.html?

Why aren't you using ForeignKey for this kind of relation.

为什么不使用ForeignKey来实现这种关系。

If it's a list of articles, as I said, in your template you should have something like this (supposing you renamed article with articles):

如果它是文章列表,正如我所说,在你的模板中你应该有这样的东西(假设你用文章重命名文章):

{% for article in articles %}
    <a href="{{ article.get_absolute_url }}">{{ article.name }}</a>
{% endfor %}

You haven't showed the Article model, so I supposed that there's .get_absolute_url and .name.

你没有展示文章模型,所以我认为有.get_absolute_url和.name。

Ask me if you haven't understood something.

问你,如果你不明白的话。