日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python属性错误怎么改_属性错误:无法设置属性

發布時間:2024/9/3 python 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python属性错误怎么改_属性错误:无法设置属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在做一個遺留的django項目,其中有一個類定義如下from django.http import HttpResponse

class Response(HttpResponse):

def __init__(self, template='', calling_context='' status=None):

self.template = template

self.calling_context = calling_context

HttpResponse.__init__(self, get_template(template).render(calling_context), status)

這個類在視圖中使用如下def some_view(request):

#do some stuff

return Response('some_template.html', RequestContext(request, {'some keys': 'some values'}))

這個類的創建主要是為了在單元測試中使用它來執行斷言,也就是說,它們不是使用django.test.Client來測試視圖,而是創建一個模擬請求,并將其傳遞給測試中的視圖(將視圖稱為可調用的),如下所示def test_for_some_view(self):

mock_request = create_a_mock_request()

#call the view, as a function

response = some_view(mock_request) #returns an instance of the response class above

self.assertEquals('some_template.html', response.template)

self.assertEquals({}, response.context)

問題是,在測試套件(相當大的測試套件)的中途,一些測試在執行return Response('some_template.html', RequestContext(request, {'some keys': 'some values'}))

堆棧跟蹤是self.template = template

AttributeError: can't set attribute

整個堆棧跟蹤看起來像======================================================================

ERROR: test_should_list_all_users_for_that_specific_sales_office

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

Traceback (most recent call last):

File "/Users/austiine/Projects/mped/console/metrics/tests/unit/views/sales_office_views_test.py", line 106, in test_should_list_all_users_for_that_specific_sales_office

response = show(request, sales_office_id=sales_office.id)

File "/Users/austiine/Projects/mped/console/metrics/views/sales_office_views.py", line 63, in show

"sales_office_users": sales_office_users}))

File "/Users/austiine/Projects/mped/console/metrics/utils/response.py", line 9, in __init__

self.template = template

AttributeError: can't set attribute

實際的失敗測試是def test_should_list_all_users_for_that_specific_sales_office(self):

user_company = CompanyFactory.create()

request = self.mock_request(user_company)

#some other stuff

#calling the view

response = show(request, sales_office_id=sales_office.id)

self.assertIn(user, response.calling_context["sales_office_users"])

self.assertNotIn(user2, response.calling_context["sales_office_users"])

顯示視圖的代碼def show(request, sales_office_id):

user = request.user

sales_office = []

sales_office_users = []

associated_market_names = []

try:

sales_office = SalesOffice.objects.get(id=sales_office_id)

sales_office_users = User.objects.filter(userprofile__sales_office=sales_office)

associated_market_names = Market.objects.filter(id__in= (sales_office.associated_markets.all())).values_list("name", flat=True)

if user.groups.all()[0].name == UserProfile.COMPANY_AO:

associated_market_names = [market.name for market in sales_office.get_sales_office_user_specific_markets(user)]

except:

pass

return Response("sales_office/show.html", RequestContext(request, {'keys': 'values'}))

總結

以上是生活随笔為你收集整理的python属性错误怎么改_属性错误:无法设置属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。