python - HttpRequest 的语法不正确

标签 python django

我正在从这里开始介绍 Django 教程:

https://docs.djangoproject.com/en/1.9/intro/tutorial03/

在其中,我在项目的 view.py 中创建此方法:

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

我从 urls.py 调用该方法:

detail(request=<HttpRequest object>, question_id='34')

因此,我完全按照教程中的步骤进行操作,但收到此错误:

    detail(request=<HttpRequest object>, question_id='34')
                   ^
SyntaxError: invalid syntax

为什么会这样?

最佳答案

detail(request=<HttpRequest object>, question_id='34')

是对幕后发生的事情的解释,而不是您应该使用的语法。

您应该按如下方式定义 detail 函数:

def detail(request, question_id):
    # your view logic here

然后,当您尝试访问引用 detail View 的 url 时,Django 将自动为您传递 HttpRequest 对象 以及指定的question_id

关于python - HttpRequest 的语法不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36384962/

相关文章:

python - 如何让 PyPy、Django 和 PostgreSQL 协同工作?

python - Pod错误-Fastparquet的建筑轮子失败

python - 在 Python 2.7 中打印 UTF-8 字符

django - Django 中的 LDAP 身份验证

python - 识别django中数据库对象是什么类型

django - 在 Django Admin 中,如何禁用删除链接

使用 Django 服务器主机名和监听端口作为 redirect_uri 的 Python-social-auth

python - 从谷歌表格中执行 python 脚本

python - python 中的字典列表作为 HTML 表格式

django - 即使设置中的 DEBUG=False,有没有办法向管理员用户显示 Django 调试堆栈跟踪页面?