9

Django応答オブジェクトがあるとしましょう。

URL(場所)を探したい。ただし、応答ヘッダーには実際にはLocationまたはContent-Locationフィールドは含まれていません。

この応答オブジェクトから、表示されているURLを特定するにはどうすればよいですか?

4

2 に答える 2

10

これは古いですが、単体テストを行うときに同様の問題が発生しました。これが私が問題を解決した方法です。

response.redirect_chainおよび/またはを使用しresponse.request['PATH_INFO']てリダイレクトURLを取得できます。

ドキュメントもチェックしてください! Djangoテストツール:assertRedirects

from django.core.urlresolvers import reverse
from django.test import TestCase


class MyTest(TestCase)
    def test_foo(self):
        foo_path = reverse('foo')
        bar_path = reverse('bar')
        data = {'bar': 'baz'}
        response = self.client.post(foo_path, data, follow=True)
        # Get last redirect
        self.assertGreater(len(response.redirect_chain), 0)
        # last_url will be something like 'http://testserver/.../'
        last_url, status_code = response.redirect_chain[-1]
        self.assertIn(bar_path, last_url)
        self.assertEqual(status_code, 302)
        # Get the exact final path from the response,
        # excluding server and get params.
        last_path = response.request['PATH_INFO']
        self.assertEqual(bar_path, last_path)
        # Note that you can also assert for redirects directly.
        self.assertRedirects(response, bar_path)
于 2014-02-27T16:06:46.960 に答える
5

応答はURLを決定しませんが、要求は決定します。

応答は、応答のURLではなく、応答の内容を提供します。

于 2011-10-31T01:27:43.723 に答える