Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

python - 我正在使用django框架的网站上工作,我得到这个AttributeError告诉我'str'对象没有属性'get'[重复](Am working on a site using django framework and i got this AttributeError telling me 'str' object has no attribute 'get' [duplicate])

`AttributeError at /polls/

(/ polls /的AttributeError)

'str' object has no attribute 'get'

('str'对象没有属性'get')

Request Method: GET

(请求方法:GET)

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

(要求网址: http : //127.0.0.1 : 8000/polls/)

Django Version: 2.0.2

(Django版本:2.0.2)

Exception Type: AttributeError

(异常类型:AttributeError)

Exception Value: 'str' object has no attribute 'get'

(异常值:“ str”对象没有属性“ get”)

Exception Location: C:\Python34\lib\site-packages\django-2.0.2-py3.4.egg\django\middleware\clickjacking.py in process_response, line 26

(异常位置:process_response,第26行中的C:\ Python34 \ lib \ site-packages \ django-2.0.2-py3.4.egg \ django \ middleware \ clickjacking.py)

Python Executable: C:\Python34\python.exe

(Python可执行文件:C:\ Python34 \ python.exe)

Python Version: 3.4.1

(的Python版本:3.4.1)

Python Path: ['C:\Users\WUESE PHILIP\Desktop\vibezt',

(Python路径:['C:\ Users \ WUESE PHILIP \ Desktop \ vibezt',)

'C:\Python34\python34.zip',

('C:\ Python34 \ python34.zip',)

'C:\Python34\DLLs',

('C:\ Python34 \ DLLs',)

'C:\Python34\lib',

('C:\ Python34 \ lib',)

'C:\Python34',

('C:\ Python34',)

'C:\Python34\lib\site-packages',

('C:\ Python34 \ lib \ site-packages',)

'C:\Python34\lib\site-packages\setuptools-33.1.1-py3.4.egg',

('C:\ Python34 \ lib \ site-packages \ setuptools-33.1.1-py3.4.egg',)

'C:\Python34\lib\site-packages\django-2.0.2-py3.4.egg',

('C:\ Python34 \ lib \ site-packages \ django-2.0.2-py3.4.egg',)

'C:\Python34\lib\site-packages\pytz-2019.3-py3.4.egg']`

('C:\ Python34 \ lib \ site-packages \ pytz-2019.3-py3.4.egg']`)

polls urls.py

from django.urls import path

from . import views

`urlpatterns = [

(`urlpatterns = [)

path('', views.index, name='index'),

(path('',views.index,name ='index'),)

]`

(]`)

views.py

from django.shortcuts import render

`from django.http import HttpResponse

(从django.http导入HttpResponse)

def index(request):

(def索引(请求):)

return("<h2>Welcome to my First Python Django Project</h2>")`

vibezt urls.py

`from django.contrib import admin

(来自django.contrib的导入管理员)

from django.urls import include, path`

(从django.urls导入包括路径)

`urlpatterns = [

(`urlpatterns = [)

path('polls/', include('polls.urls')),

(path('polls /',include('polls.urls')),)

path('admin/', admin.site.urls),

(路径(“ admin /”,admin.site.urls),)

]`

(]`)

  ask by Wuese Philip translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should return HttpResponse (or subclass) in your views, not raw python strings.

(您应该在视图中返回HttpResponse (或子类),而不是原始的 python字符串。)

So you should have:

(因此,您应该具有:)

from django.http import HttpResponse

def index(request):
    return HttpResponse("<h2>Welcome to my First Python Django Project</h2>")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...