繁体   English   中英

django-tenants:登录后重定向到租户 url 引发“NoReverseMatch:‘demo.localhost’不是已注册的命名空间”

[英]django-tenants: redirecting to tenant url after login raises "NoReverseMatch: 'demo.localhost' is not a registered namespace"

我似乎找不到解决用户身份验证时发生的错误的方法。

用户需要在公共网站级别登录,比方说localhost/login ,当他/她通过身份验证时,我需要将用户重定向到相应的子域,例如demo.localhost 到目前为止,这对我不起作用,即使在检查了一些有关该主题的帖子之后也是如此。

这是用户登录的customers.views中的 views.py:

def login_view(request):
    if request.method == 'POST':
        form = AuthenticationForm(request,data=request.POST)
        print("form login view")
        if form.is_valid():
            print("checkpoint")
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)

            user = form.get_user()
            schema_name = user.schema_name

            print(user)
            if user is not None:
                login(request, user)
                url = reverse(schema_name + '.localhost:8000/mydashboard/')
                print("url")
                return HttpResponseRedirect(url)
    else:
        print("not working")
        form = AuthenticationForm()

    context = {
        'form': form,
                }
    return render(request, 'login.html', context)

这是核心文件夹中的网址:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('django_plotly_dash/', include('django_plotly_dash.urls')),
    path('mydashboard/', include('mydashboard.urls',namespace="mydashboard")),
    
]

我在mydashboard.urls中添加了app_name = 'mydashboard'

这是我不断收到的错误的回溯:

Internal Server Error: /registration/loginUser
Traceback (most recent call last):
  File "/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/urls/base.py", line 71, in reverse
    extra, resolver = resolver.namespace_dict[ns]
KeyError: 'demo.localhost'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/pierre/Desktop/simulation_application/customers/views.py", line 65, in login_view
    url = reverse(schema_name + '.localhost:8000/mydashboard/')
  File "/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/urls/base.py", line 82, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'demo.localhost' is not a registered namespace

更新:删除reverse并包含在 url 中的request.scheme

def login_view(request):
    if request.method == 'POST':
        form = AuthenticationForm(request,data=request.POST)
        print("form login view")
        if form.is_valid():
            print("checkpoint")
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)

            user = form.get_user()
            schema_name = user.schema_name

            print(user)
            if user is not None:
                login(request, user)
                print(request.scheme)
                url = request.scheme + '://' + schema_name + '.localhost:8000/mydashboard/'
                print("url",url)
                return HttpResponseRedirect(url)
    else:
        print("not working")
        form = AuthenticationForm()

    context = {
        'form': form,
                }
    return render(request, 'login.html', context)

这仍然有效,output 出现以下错误:

                response = get_response(request) …
Local vars
/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/core/handlers/base.py, line 181, in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
Local vars
/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/contrib/auth/decorators.py, line 32, in _wrapped_view
            return redirect_to_login( …
Local vars
/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/contrib/auth/views.py, line 190, in redirect_to_login
    return HttpResponseRedirect(urlunparse(login_url_parts)) …
Local vars
/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/http/response.py, line 507, in __init__
            raise DisallowedRedirect("Unsafe redirect to URL with protocol '%s'" % parsed.scheme) …
Local vars

Unsafe redirect to URL with protocol 'localhost'
Bad Request: /mydashboard/simulations_list
HTTP GET /mydashboard/simulations_list 400 [0.09, 127.0.0.1:50218]

这很奇怪,因为请求的 url 可以正常工作 url,在登录后尝试重定向到它之前已注册并且可以毫无问题地访问

编辑#2:

发现上面的代码在目标 url 不需要login required有效

# this does not work 
@login_required
def simulationslistView(request,):
     return render(request, 'simulations_list.html')

# this works
def simulationslistView(request,):
     return render(request, 'simulations_list.html')

我显然确保该应用程序是安全的,并且只能由登录用户访问。 是否有解决方法来确保它有效?

reverse是一个内部 django 实用程序到 go,从“应用程序名称”+“:”+“url 友好名称”到“真实 url”。 在您的示例中,您已经提供了“真实网址”。

在你领先的时候退出,然后做:

            if user is not None:
                login(request, user)
                url = request.scheme + '://' + schema_name + '.localhost:8000/mydashboard/'
                print("url")
                return HttpResponseRedirect(url)
  • 注意没有reverse
  • 注意添加 URL 方案(http/https 由最初发出请求的方式决定)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM