Python Django Tastypie 0.9.13-beta:资源始终返回状态\u代码200

Python Django Tastypie 0.9.13-beta:资源始终返回状态\u代码200,python,django,tastypie,Python,Django,Tastypie,我一直在使用Django和Tastypie版本Django-Tastypie==0.9.13-beta的Django和Tastypie开发API,在测试用例中,当我预期bad request响应为400或401时,它总是返回httpok200响应。什么会导致这个问题 用户资源 小部分测试 测试失败回溯 谢谢。我认为最好的办法是先做 创建异常类: class HTTPError(Exception): def __init__(self, code, msg): Excep

我一直在使用
Django
Tastypie
版本
Django-Tastypie==0.9.13-beta
Django
Tastypie
开发API,在测试用例中,当我预期
bad request
响应为400或401时,它总是返回
httpok
200响应。什么会导致这个问题

用户资源 小部分测试 测试失败回溯
谢谢。

我认为最好的办法是先做

创建异常类:

class HTTPError(Exception):
    def __init__(self, code, msg):
        Exception.__init__(self, msg)
        self.code = code
        self.msg = msg
创建一个自定义页面“error.html”,只显示err_代码err_msg和request或reason(如果需要)。 “{{err_code}}{{err_msg}}{{request}”

不要错过在设置中添加模板。在模板中添加py

创建一个中间件 例如:

例如: 在代码中,当需要引发错误时

Raise HTTPError(404,'未找到。该页对用户%s'无效% 请求(用户)


我想如果你的请求实际上无效,你只能得到400英镑。由于您的请求是有效的,并且只有与该请求一起提供的信息是错误的(错误!=无效),因此您仍然得到200。您需要调整服务器端以检查电子邮件的有效性,并在必要时返回400代码。修复语法错误:
'wrong@email.com
应该是
'wrong@email.com“
@karthikr在代码中没问题:)我想补充一点,那就是在unittest with assertRaise(404,method,code='code')中,您应该在后面进行测试
def test_login_unsuccessful_wrong_email(self):
    post_data = {
        "user": {
            "email": 'wrong@email.com',  # so email is wrong
            "password": self.user_password
        }
    }

    response = self.api_client.post('/api/v1/login/', data=post_data)

    self.assertHttpBadRequest(response)    # At this place I expect 400 status code
FAIL: test_login_unsuccessful_wrong_email (api.v1.tests.UsersLoginResourceTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/sultan/.virtualenvs/myenv/myproject/api/v1/tests.py", line 458, in test_login_unsuccessful_wrong_email
    self.assertHttpBadRequest(response)
  File "/Users/sultan/.virtualenvs/myenv/lib/python2.7/site-packages/django_tastypie-0.9.13_beta-py2.7.egg/tastypie/test.py", line 346, in assertHttpBadRequest
    return self.assertEqual(resp.status_code, 400)
AssertionError: 200 != 400
class HTTPError(Exception):
    def __init__(self, code, msg):
        Exception.__init__(self, msg)
        self.code = code
        self.msg = msg
class JobMiddlewareException:
    def process_exception(self, request, e):
       if isinstance(e, HTTPError)
            render_to_response('error.html', {'err_code': e.code, 'err_msg': e.msg, 'request':request}, requestcontext(request))