아래 코드의 응답에서 ID를 어떻게 인쇄 할 수 있습니까? 사용자가 DB에 존재합니다. 또한이 오류가 발생합니다.Django 테스트 클라이언트가 쿼리에서 행 ID를 얻습니다.
from django.test import Client
c = Client(enforce_csrf_checks=False)
response = c.post('/reg/_user/', {'firstname': 'test', 'lastname' : '_test'})
보기 나 get_user
def _user(request):
try:
response_dict = {}
qd = request.POST
firstname = qd.__getitem__('firstname')
lastname = qd.__getitem__('lastname')
up = UserProfile.objects.get(first_name=firstname,last_name=lastname)
print up.id
return up.id
except:
pass
오류 :
response = c.post('/reg/_user/', {'firstname': 'test', 'lastname' : '_test'})
File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 483, in post
response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 302, in post
return self.request(**r)
File "/usr/local/lib/python2.7/dist-packages/django/test/client.py", line 444, in request
six.reraise(*exc_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 201, in get_response
response = middleware_method(request, response)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/clickjacking.py", line 30, in process_response
if response.get('X-Frame-Options', None) is not None:
AttributeError: 'UserProfile' object has no attribute 'get'
작은 것들 :'qd .__ getitem __ ('firstname')'은'qd [ 'firstname']'와 동의어입니다. 그러나 이전의 것은 훨씬 짧고 명확합니다 :) –
'qd.get ('firstname' ')', 아이템이 존재하지 않으면 예외를 던지지 않을 것입니다 (대신'None'을 리턴합니다). –
불행히도 귀하의 질문을 이해할 수 없습니다. 다시 말해 줄 수 있니? –