请帮忙写测试。你知道吗
你知道吗视图.py地址:
def ajax_username_check(request):
"""
ajax check username for registration form
return true - no matched
return false - matched
"""
result = True
if request.method == 'POST' and request.is_ajax():
username = request.POST.get('username', '')
try:
check_username_match = User.objects.get(username=username)
except:
pass
else:
result = False
data = {
'result': result,
}
return HttpResponse(json.dumps(data), content_type='application/json')
你知道吗测试.py地址:
class TestAjaxCheckUsername(TestCase):
def setUp(self):
self.client = Client()
self.record = User.objects.create(
user_id=2,
username='qqqqqq',
password='pbkdf2_sha256$12000$Bm1GmmeGtnyU$v4E1UUcXWjk7pmQEkIWXvY2Hsw2ycG783R/bVpoVEWk=',
is_active=1,
is_staff=0,
is_superuser=0,
email='asasas@mail.ru'
)
def test_diary(self):
json_string = json.dumps({'username':'qqqqqq'})
self.response = self.client.post('/ajax_username_check/', json_string, "text/json", HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertContains(self.response, true)
错误消息:
ERROR: test_diary (app_accounts.tests.TestAjaxCheckUsername)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/kalinins/.virtualenvs/kinopom_project/kinopom/app_accounts/tests.py", line 17, in setUp
email='asasas@mail.ru'
File "/home/kalinins/.virtualenvs/kinopom_project/kinopom_env/local/lib/python2.7/site-packages/django/db/models/manager.py", line 157, in create
return self.get_queryset().create(**kwargs)
File "/home/kalinins/.virtualenvs/kinopom_project/kinopom_env/local/lib/python2.7/site-packages/django/db/models/query.py", line 320, in create
obj = self.model(**kwargs)
File "/home/kalinins/.virtualenvs/kinopom_project/kinopom_env/local/lib/python2.7/site-packages/django/db/models/base.py", line 417, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'user_id' is an invalid keyword argument for this function
我正在尝试使用Ajax请求验证名为qqq的用户的存在。我需要检查函数ajax\u username\u check()的返回值
我读过this post并试图使用它,但没有用
用户没有字段
user_id
,它有一个字段id
。但是,我也不会设置它,因为ID可能已经存在,并且通常Django选择模型ID。所以我要去掉user_id=2
行。你知道吗另外两个注释:首先,从不像那样使用一个空的
except:
,显式地使用except User.DoesNotExist:
,否则您可能会意外地发现谁知道什么异常。你知道吗第二,我根本不会在这里使用
get()
:您要查找的是用户exists
与否。所以:应该这样做。你知道吗
您似乎没有阅读错误消息。你知道吗
首先,这不是检查用户或注册的问题:错误消息显然来自测试的设置方法。代码的其余部分无关紧要。你知道吗
第二,这个消息确切地告诉您什么是错误的:user\ id不是有效的参数。这是因为没有这样的字段“user\u id”。也许你是说
id
?你知道吗相关问题 更多 >
编程相关推荐