V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lovebeyondalways
V2EX  ›  Python

django 如何过滤某个分类下的文章

  •  
  •   lovebeyondalways ·
    piaokorg · 2016-11-03 11:50:35 +08:00 · 2549 次点击
    这是一个创建于 2702 天前的主题,其中的信息可能已经有所发展或是发生改变。

    models.py class Category(models.Model): name = models.CharField(verbose_name=u'文章分类', max_length=64)

        def __str__(self):
            return self.name
    
    class article(models.Model):
        title = models.CharField(u'标题', max_length=60)
        category = models.ForeignKey(Category, verbose_name=u'标签', max_length=10, null=True)
    

    在 url 中以分类名作为参数传递给 views 内函数进行过滤行不通

    def Category_list(request, category):
        try:
            category_post = article.objects.filter(category_iexact=category)
        except article.DoesNotExist:
            raise Http404
        return render(request, 'category.html', {"category_post": category_post})
    

    ValueError at /linux/ invalid literal for int() with base 10: 'linux'

    8 条回复    2016-11-21 12:01:58 +08:00
    Blunt1991
        1
    Blunt1991  
       2016-11-03 12:10:06 +08:00
    ```python
    def Category_list(request, category):
    try:
    category_post = article.objects.filter(category__name__iexact=category)
    except article.DoesNotExist:
    raise Http404
    return render(request, 'category.html', {"category_post": category_post})
    ```
    lovebeyondalways
        2
    lovebeyondalways  
    OP
       2016-11-03 12:52:38 +08:00
    @Blunt1991 谢谢大牛 此法可用
    lovebeyondalways
        3
    lovebeyondalways  
    OP
       2016-11-03 12:53:30 +08:00
    @Blunt1991
    敢问大神__name __iexact 这两个字段在哪篇文档里有详解
    Blunt1991
        5
    Blunt1991  
       2016-11-03 13:10:24 +08:00
    官方文档, https://docs.djangoproject.com/en/1.10/topics/db/queries/#lookups-that-span-relationships
    你原来的代码中 category_iexact 查询时其实是根据 id 去查询的,要根据 model 的其他属性去查询的话,直接双下划线就属性就可以了。
    lovebeyondalways
        6
    lovebeyondalways  
    OP
       2016-11-03 13:12:32 +08:00 via Android
    原来是这样,看来官方文档要过一遍
    zmrenwu
        7
    zmrenwu  
       2016-11-05 19:25:10 +08:00
    category_post = article.objects.filter(category=category)

    或者 category_post = category.article_set.all()
    Ellen
        8
    Ellen  
       2016-11-21 12:01:58 +08:00
    传过来的参数是字符串,这里 iexact 的需要是一个 category 对象。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3717 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 00:14 · PVG 08:14 · LAX 17:14 · JFK 20:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.