在 Django 中测试 url 重定向
Testing url redirection in Django
如果我转到 http://localhost:8000/login/
,将显示登录表单,您必须输入 username
和 password
。如果凭据正确,您将被重定向到 http://localhost:8000/dashboard/
。我在测试时面临的问题是,即使我的 username
和 password
是正确的,它也没有重定向到 http://localhost:8000/dashboard/
,而是转到 http://testserver/admin/login/?next=/dashboard/
。我正在使用以下代码在 django 中休息重定向功能:
class UrlTests(TestCase):
def test_client_cart(self):
response = self.client.get('/login/')
self.assertEqual(200, response.status_code)
def test_login_client(self):
User.objects.create_user('rakesh', 'rrs402@nyu.edu', 'ranjan')
self.client.get('/login/')
self.client.login(username='rakesh', password='ranjan')
print self.client.get('/dashboard/')
谁能告诉我为什么我被重定向到 http://testserver/admin/login/?next=/dashboard/
而不是 http://localhost:8000/dashboard/
。
在我的 settings.py
文件中:
LOGIN_URL = '/'
LOGOUT_URL = '/logout/'
LOGIN_REDIRECT_URL = '/dashboard/'
LOGOUT_REDIRECT_URL = '/'
我的应用程序运行良好,但我无法测试重定向。谁能给我一些我哪里出错的指示?
如果我打印出 print self.client.login(username='rakesh', password='ranjan')
它会变成 True
这意味着我可以登录但为什么不能重定向?
更新:我也尝试使用 selenium
def login_user(self):
# User opens his web browser, and goes to the website
self.browser.get(self.live_server_url + '/login/')
# User types in his username and passwords and hits return
username_field = self.browser.find_element_by_name('username')
username_field.send_keys('rakesh')
password_field = self.browser.find_element_by_name('password')
password_field.send_keys('ranjan')
#User submits the form
password_field.send_keys(Keys.RETURN)
body = self.browser.find_element_by_tag_name('body')
print body.text
这里我也得到 admin
页面的 body.text 而不是 dashboard
页面。
更新:
仪表板视图:
class PortalDashboardView(RequireStaffMixinView, TemplateView):
template_name = "portal/dashboard.html"
因为您没有登录。您发送给登录调用的密码与您在测试用例中创建用户时使用的密码不同,因此凭据无效。
请注意,无论如何您都不需要在那里调用 client.get('/login/')
。
如果我转到 http://localhost:8000/login/
,将显示登录表单,您必须输入 username
和 password
。如果凭据正确,您将被重定向到 http://localhost:8000/dashboard/
。我在测试时面临的问题是,即使我的 username
和 password
是正确的,它也没有重定向到 http://localhost:8000/dashboard/
,而是转到 http://testserver/admin/login/?next=/dashboard/
。我正在使用以下代码在 django 中休息重定向功能:
class UrlTests(TestCase):
def test_client_cart(self):
response = self.client.get('/login/')
self.assertEqual(200, response.status_code)
def test_login_client(self):
User.objects.create_user('rakesh', 'rrs402@nyu.edu', 'ranjan')
self.client.get('/login/')
self.client.login(username='rakesh', password='ranjan')
print self.client.get('/dashboard/')
谁能告诉我为什么我被重定向到 http://testserver/admin/login/?next=/dashboard/
而不是 http://localhost:8000/dashboard/
。
在我的 settings.py
文件中:
LOGIN_URL = '/'
LOGOUT_URL = '/logout/'
LOGIN_REDIRECT_URL = '/dashboard/'
LOGOUT_REDIRECT_URL = '/'
我的应用程序运行良好,但我无法测试重定向。谁能给我一些我哪里出错的指示?
如果我打印出 print self.client.login(username='rakesh', password='ranjan')
它会变成 True
这意味着我可以登录但为什么不能重定向?
更新:我也尝试使用 selenium
def login_user(self):
# User opens his web browser, and goes to the website
self.browser.get(self.live_server_url + '/login/')
# User types in his username and passwords and hits return
username_field = self.browser.find_element_by_name('username')
username_field.send_keys('rakesh')
password_field = self.browser.find_element_by_name('password')
password_field.send_keys('ranjan')
#User submits the form
password_field.send_keys(Keys.RETURN)
body = self.browser.find_element_by_tag_name('body')
print body.text
这里我也得到 admin
页面的 body.text 而不是 dashboard
页面。
更新:
仪表板视图:
class PortalDashboardView(RequireStaffMixinView, TemplateView):
template_name = "portal/dashboard.html"
因为您没有登录。您发送给登录调用的密码与您在测试用例中创建用户时使用的密码不同,因此凭据无效。
请注意,无论如何您都不需要在那里调用 client.get('/login/')
。