已有184人关注
《Python Web开发从入门到实践》这本书里的5.5.7.2案例老是报错。
发表在Python图书答疑 2022-03-08 《Python Web开发从入门到实践》第5章 企业级开发框架Django 156页-157页
是否精华
版块置顶:

class PublishYearFilter(admin.SimpleListFilter):


    

  title = _('发布年份')


    parameter_name = 'year'

    def lookups(self, request, model_admin):

        return (
            ('2020', _('2020年')),
            ('2019', _('2019年')),
        )

    def queryset(self, request, queryset):

        if self.value() == '2019':
            return queryset.filter(publish_date__get=date(2019, 1, 1),
                                    publish_date__lte=date(2019, 12, 31))

        if self.value() == '2020':
            return queryset.filter(publish_date__get=date(2020, 1, 1),
                                    publish_date__lte=date(2020, 12, 31))


class ArticleAdmin(admin.ModelAdmin):
    """
    创建UserAdmin类,继承于admin.ModelAdmin
    """
    #  配置展示列表,在User版块下的列表展示

    # 配置过滤查询字段,在User版块下右侧过滤框
    list_filter = ('title', 'user__username', PublishYearFilter)  #

分享到:
精彩评论 5
李伟杰
学分:7 LV1
2022-03-08
沙发

运行结果


  File "F:\python\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception

    raise _exception[1]

  File "F:\python\lib\site-packages\django\core\management\__init__.py", line 381, in execute

    autoreload.check_errors(django.setup)()

  File "F:\python\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper

    fn(*args, **kwargs)

  File "F:\python\lib\site-packages\django\__init__.py", line 24, in setup

    apps.populate(settings.INSTALLED_APPS)

  File "F:\python\lib\site-packages\django\apps\registry.py", line 122, in populate

    app_config.ready()

  File "F:\python\lib\site-packages\django\contrib\admin\apps.py", line 27, in ready

    self.module.autodiscover()

  File "F:\python\lib\site-packages\django\contrib\admin\__init__.py", line 24, in autodiscover

    autodiscover_modules('admin', register_to=site)

  File "F:\python\lib\site-packages\django\utils\module_loading.py", line 57, in autodiscover_modules

    import_module('%s.%s' % (app_config.name, module_to_search))

  File "F:\python\lib\importlib\__init__.py", line 127, in import_module

    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked

  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked

  File "<frozen importlib._bootstrap_external>", line 850, in exec_module

  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed

  File "F:\blog\article\admin.py", line 20, in <module>

    class PublishYearFilter(admin.SimpleListFilter):

  File "F:\blog\article\admin.py", line 22, in PublishYearFilter

    title = _('发布年份')

NameError: name '_' is not defined


李伟杰
学分:7 LV1
2022-03-08
板凳
title = _('发布年份')


def lookups(self, request, model_admin):

        return (
            ('2020', _('2020年')),
            ('2019', _('2019年')),

------------------------------------------------------

这几个地方都有一个下划线_,我把下划线去掉,可以运行代码案例。但是,在实例网站中输入年号,查找不了对应的文章。

李伟杰
学分:7 LV1
2022-03-08
地板

在网站实例中点击By发布年份下2019年的链接,CMD命令行屏幕显示错误代码:


[08/Mar/2022 17:13:47] "GET /admin/article/article/ HTTP/1.1" 200 10201

Internal Server Error: /admin/article/article/

Traceback (most recent call last):

  File "F:\python\lib\site-packages\django\core\handlers\exception.py", line 47, in inner

    response = get_response(request)

  File "F:\python\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response

    response = wrapped_callback(request, *callback_args, **callback_kwargs)

  File "F:\python\lib\site-packages\django\contrib\admin\options.py", line 622, in wrapper

    return self.admin_site.admin_view(view)(*args, **kwargs)

  File "F:\python\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view

    response = view_func(request, *args, **kwargs)

  File "F:\python\lib\site-packages\django\views\decorators\cache.py", line 57, in _wrapped_view_func

    response = view_func(request, *args, **kwargs)

  File "F:\python\lib\site-packages\django\contrib\admin\sites.py", line 236, in inner

    return view(request, *args, **kwargs)

  File "F:\python\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper

    return bound_method(*args, **kwargs)

  File "F:\python\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view

    response = view_func(request, *args, **kwargs)

  File "F:\python\lib\site-packages\django\contrib\admin\options.py", line 1710, in changelist_view

    cl = self.get_changelist_instance(request)

  File "F:\python\lib\site-packages\django\contrib\admin\options.py", line 742, in get_changelist_instance

    return ChangeList(

  File "F:\python\lib\site-packages\django\contrib\admin\views\main.py", line 100, in __init__

    self.queryset = self.get_queryset(request)

  File "F:\python\lib\site-packages\django\contrib\admin\views\main.py", line 464, in get_queryset

    new_qs = filter_spec.queryset(request, qs)

  File "F:\blog\article\admin.py", line 36, in queryset

    return queryset.filter(publish_date__gte=date(2019, 1, 1),

NameError: name 'date' is not defined


李伟杰
学分:7 LV1
2022-03-08
4L

NameError at /admin/article/article/

name 'date' is not defined
Request Method:GET
Request URL:http://127.0.0.1:8000/admin/article/article/?year=2019
Django Version:4.0.2
Exception Type:NameError
Exception Value:
name 'date' is not defined
Exception Location:F:\blog\article\admin.py, line 36, in queryset
Python Executable:F:\python\python.exe
Python Version:3.9.10
Python Path:
['F:\\blog',
 'F:\\python\\python39.zip',
 'F:\\python\\DLLs',
 'F:\\python\\lib',
 'F:\\python',
 'F:\\python\\lib\\site-packages']
Server time:Tue, 08 Mar 2022 09:18:25 +0000


game99258
学分:2266 LV10
2022-07-18
5L

下划线问题吧?

首页上一页 1 下一页尾页 5 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照