Django AttributeError:“ tuple”对象没有属性“ get”

时间:2018-08-03 17:09:17

标签: python django http get views

在按照教程进行操作时,我很难获得正确的答复a link 每当我运行服务器时,我都无法进入视图。请帮助我检查代码。我不确定我使用的django版本和本教程使用的版本是否存在问题,因为我注意到本教程中的那个使用django.urls导入路径,但是当我启动一个应用程序时,它来自django.conf.urls。有不同,我很困惑。

Request Method: GET
Request URL: http://127.0.0.1:8000/polls/

Django Version: 1.11.6
Python Version: 3.6.3
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "c:\users\CaddyKKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner 41.             response = get_response(request)
File "c:\users\Caddy KKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py" in __call__ 142.             response = 
self.process_response(request, response)
File "c:\users\Caddy KKhaw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\middleware\clickjacking.py" in process_response 32.         if response.get('X-Frame-Options') is not 
None:
Exception Type: AttributeError at /polls/
Exception Value: 'tuple' object has no attribute 'get'

这是我的民意调查/views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("You did it this time!,"),

这是我的民意调查/urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url('', views.index, name="index")

]

这是我的mysite / urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include("polls.urls")),
    url(r'^admin/', admin.site.urls),
]

1 个答案:

答案 0 :(得分:1)

在视图的return语句中还有尾随逗号。删除它,你会好起来的。

确切地说,请从以下行开始修改

return HttpResponse("You did it this time!,"),

return HttpResponse("You did it this time!,")