django_webtest 中的用户身份验证

发布于 2025-01-04 19:44:36 字数 1284 浏览 0 评论 0 原文

我的模板中有以下代码:

<div class="account">
{% if request.user.is_authenticated %}
    <a href="{% url settings %}"
       class="iconed username">{{ request.user.username }}</a>
    &nbsp;|&nbsp;
    <a href="{% url logout %}?next={{ request.path }}"
       class="iconed logout">{% trans "Logout" %}</a>
{% else %}
    <a href="{% url login %}?next={{ request.path }}" class="iconed login">{% trans "Login" %}</a>
    &nbsp;|&nbsp;
    <a href="{% url sign_up %}?next={{ request.path }}"
       class="iconed sign-up">{% trans "Sign up" %}</a>
{% endif %}
</div>

如您所见,它显示不同的链接取决于用户是否登录。如果我手动测试它,它工作正常,但是当我尝试使用以下代码测试它时:

def test_home_logged_in(self):
    if self.client.login(username='Test', password='secret'):
        home = self.app.get('/')
        self.assertOK(home)
        self.assertContains(home, '/settings/')
        self.assertContains(home, '/logout/')
    else:
        self.fail("Couldn't log in.")

login() 返回 True,但测试失败。我为 home 对象调用了 showbrowser() ,看到返回的页面看起来像匿名用户的页面 - 除了设置和注销的链接之外,它还包含注册和登录的链接。

在模板中使用 *request.user.is_authenticated* 来检查用户是否经过身份验证是否正确?为什么模板看不到该用户是从 test 注册的?

谢谢!

I have following code in my template:

<div class="account">
{% if request.user.is_authenticated %}
    <a href="{% url settings %}"
       class="iconed username">{{ request.user.username }}</a>
     | 
    <a href="{% url logout %}?next={{ request.path }}"
       class="iconed logout">{% trans "Logout" %}</a>
{% else %}
    <a href="{% url login %}?next={{ request.path }}" class="iconed login">{% trans "Login" %}</a>
     | 
    <a href="{% url sign_up %}?next={{ request.path }}"
       class="iconed sign-up">{% trans "Sign up" %}</a>
{% endif %}
</div>

As you can see, it shows different links depends on user logged in or not. It works fine if I test it by hands, but when I try to test it with following code:

def test_home_logged_in(self):
    if self.client.login(username='Test', password='secret'):
        home = self.app.get('/')
        self.assertOK(home)
        self.assertContains(home, '/settings/')
        self.assertContains(home, '/logout/')
    else:
        self.fail("Couldn't log in.")

login() returns True, but test fails. I called showbrowser() for home object and see, that page that was returned, looks like page for anonymous user - it contains links to sign up and login besides links to settings and logout.

Is it correct to use *request.user.is_authenticated* in template to check if user is authenticated? Why template doesn't see that user was signed up from test?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习ぎ惯性依靠 2025-01-11 19:44:36

根据您的其他问题,我猜测您正在使用 django_webtest。如果是这样,您可以向请求指定您想要以哪个用户身份登录。因此,要以用户“测试”身份访问主页,您可以执行以下操作:

home = self.app.get('/', user='Test')

Based on your other questions, I'm guessing you're using django_webtest. If so, you can specify to the request which user you want to be logged in as. So to access the homepage as user 'Test' you would do:

home = self.app.get('/', user='Test')
囚我心虐我身 2025-01-11 19:44:36

这是正确的,但您需要有 django.core.context_processors.request href="https://docs.djangoproject.com/en/dev/ref/settings/#std%3asetting-TEMPLATE_CONTEXT_PROCESSORS" rel="nofollow">settings.TEMPLATE_CONTEXT_PROCESSORS 使可从模板访问请求

It is correct, but you need to have django.core.context_processors.request in settings.TEMPLATE_CONTEXT_PROCESSORS to make request accessible from templates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文