Python 我不断收到 'WSGIRequest' 对象在 django 上没有属性 'Get'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32808106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:18:09  来源:igfitidea点击:

I keep getting 'WSGIRequest' object has no attribute 'Get' on django

pythondjango

提问by Bossam

I'm trying to build a little "boards" app for practicing purposes. I'm currently stuck on a page, where a error occurs if I try to load a paginator template.

我正在尝试构建一个用于练习目的的小“板”应用程序。我目前卡在一个页面上,如果我尝试加载分页器模板,则会发生错误。

The error traceback looks like the following:

错误回溯如下所示:

AttributeError at /board/2/
'WSGIRequest' object has no attribute 'Get'
Request Method: GET
Request URL:    http://192.168.56.101:8000/board/2/
Django Version: 1.7.6
Exception Type: AttributeError
Exception Value:    
'WSGIRequest' object has no attribute 'Get'
Exception Location: /home/web/workspace/simpleboard/board/views.py in read_board, line 38
Python Executable:  /home/web/venv/bin/python
Python Version: 3.4.2
Python Path:    
['/home/web/workspace/simpleboard',
 '/home/web/venv/lib/python34.zip',
 '/home/web/venv/lib/python3.4',
 '/home/web/venv/lib/python3.4/plat-linux',
 '/home/web/venv/lib/python3.4/lib-dynload',
 '/home/web/.pyenv/versions/3.4.2/lib/python3.4',
 '/home/web/.pyenv/versions/3.4.2/lib/python3.4/plat-linux',
 '/home/web/venv/lib/python3.4/site-packages']


Traceback Switch to copy-and-paste view

/home/web/venv/lib/python3.4/site-packages/django/core/handlers/base.py in get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
? Local vars
/home/web/workspace/simpleboard/board/views.py in read_board
page = request.Get.get("page") ...
? Local vars

Views:

意见:

def read_board(request, board_id):
    board = get_object_or_404(Board, id=board_id)
    article_list = board.article_set.order_by("-written_date")
    paginator = Paginator(article_list, 5)
    page = request.Get.get("page")           <--error here, apparently.

    try:
        articles = paginator.page(page)    
    except PageNotAnInteger:
        articles = paginator.page(1)    
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)

    context = {
        "board" : board,
        "articles" : articles,
        "pages" : paginator.page_range
    }

    return render(request, "board.html", context)

Thanks.

谢谢。

PS: tab key is not working on stackoverflow, so I had to indent with spaces instead. When I press tab, it jumps to "tags" box underneath. Does anyone know what the problem is? As far as I can remember, I worked fine last time. (I'm on Mac OS by the way.)

PS:tab键在stackoverflow上不起作用,所以我不得不用空格缩进。当我按 Tab 键时,它会跳转到下面的“标签”框。有谁知道问题是什么?据我所知,上次我工作得很好。(顺便说一下,我使用的是 Mac OS。)

采纳答案by Daniil Ryzhkov

You have misprinted GET. Use:

你打错了GET。用:

page = request.GET.get("page")

Please read "Request and response objects" article on Django Docs.

请阅读关于 Django Docs 的“请求和响应对象”文章。