Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 1.7和1.9版本中,单元测试时csrf_token自动生成 #2

Open
faketooth opened this issue Jan 9, 2016 · 0 comments
Open

Comments

@faketooth
Copy link
Owner

根据书中示例代码,在Django 1.7版本中,做单元测试时不需要关心csrf_token的生成问题。但在1.9版本中,执行单元测试代码则csrf_token没有被自动生成,render_to_string根本没有对其进行绘制。

根据http://s1n4.github.io/2013/05/12/django-csrf_token-when-using-render_to_string/ ,解决代码如下:

    c = {}
    c.update(csrf(request))
    expected_html = render_to_string(
        'home.html',
        c
    )
    self.assertEqual(response.content.decode(), expected_html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant