【问题标题】:Django Allauth and urlsDjango Allauth 和网址
【发布时间】:2017-02-23 08:42:18
【问题描述】:

我有一个用于测试 Django 的开发环境。我从“本地”pyenv 安装运行 Python 3.5.2。我有 Django 1.10.2。我昨天发现了allauth 注册插件,并且一直在使用它,但遇到了问题。

我的网站是“dev.my.domain.com”。目的是在本网站的生产版本上不会有任何“公开”信息。生产版本将被称为:“members.my.domain.com”。所以,我想知道“allauth”插件是否有可能让所有非/adomn入站请求检查身份验证?

所以,请求:

  • dev.my.domain.com
  • dev.my.domain.com/foo
  • dev.my.domain.com/foo/../bar/...

都应该检查身份验证。如果不在那里,那么我假设“allauth”将重定向到登录/注册页面。

我尝试将Members/urls.py 文件设置为:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^$', include('allauth.urls')),
    url(r'^admin/', admin.site.urls),
]

但该炸弹会出现页面未找到错误和DEBUG 消息:

    Using the URLconf defined in Members.urls, Django tried these URL patterns, in this order:
    ^$ ^ ^signup/$ [name='account_signup']
    ^$ ^ ^login/$ [name='account_login']
    ^$ ^ ^logout/$ [name='account_logout']
    ^$ ^ ^password/change/$ [name='account_change_password']
    ^$ ^ ^password/set/$ [name='account_set_password']
    ^$ ^ ^inactive/$ [name='account_inactive']
    ^$ ^ ^email/$ [name='account_email']
    ^$ ^ ^confirm-email/$ [name='account_email_verification_sent']
    ^$ ^ ^confirm-email/(?P<key>[-:\w]+)/$ [name='account_confirm_email']
    ^$ ^ ^password/reset/$ [name='account_reset_password']
    ^$ ^ ^password/reset/done/$ [name='account_reset_password_done']
    ^$ ^ ^password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$ [name='account_reset_password_from_key']
    ^$ ^ ^password/reset/key/done/$ [name='account_reset_password_from_key_done']
    ^$ ^social/
    ^$ ^google/
    ^$ ^facebook/
    ^$ ^facebook/login/token/$ [name='facebook_login_by_token']
    ^admin/
    The current URL, , didn't match any of these.

我向自己的无知低头,回到 allauth 文档并使用他们默认的 urls 设置:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^accounts/', include('allauth.urls')),
    url(r'^admin/', admin.site.urls),
]

但这也会引发页面未找到和不同的消息:

    Using the URLconf defined in Members.urls, Django tried these URL patterns, in this order:
    ^accounts/
    ^admin/
    The current URL, , didn't match any of these.

我认为“allauth”安装的其余部分已正确完成,但我遗漏了一些东西。

想法?

【问题讨论】:

    标签: python django django-allauth


    【解决方案1】:

    在您的 view.py 文件中,您只需要在分发页面之前进行一点“过滤”,以查看用户是否已通过身份验证。

    一个例子是:

    def myview(request):
        if request.user.is_authenticated():
             # do something if the user is authenticated, like showing a page.
        else:
            # do somthing else
    

    重新调整 urls 结构 - 只需尝试将 /accounts/ 添加到 url,如果您处于调试模式 (DEBUG = True),404 页面将显示所有端点。 您还可以在文档中找到端点的所有 url。

    希望我正确理解了您的问题:)

    【讨论】:

    • 嗯,你是说allauth 需要我创建默认视图吗?文档似乎没有暗示,但我可能没有正确阅读它们。
    • 您获得的视图几乎是一个演示 - 正如您看到的那样,它们在真实站点中并不真正可用。您需要使用库将平台嵌入到您的视图中:)
    • 好的,但我的问题是我似乎无法访问示例视图。我得到的只是页面未找到错误和有关 url 模式的神秘消息
    猜你喜欢
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多