[英]django-tenants: redirecting to tenant url after login raises "NoReverseMatch: 'demo.localhost' is not a registered namespace"
I don't seem to find a way to solve an error happening at the user authentication.我似乎找不到解决用户身份验证时发生的错误的方法。
User needs to login at the public website level let's say localhost/login
and when he/she is authenticated, I need the user to be redirected in the corresponding sub domain, for example demo.localhost
.用户需要在公共网站级别登录,比方说localhost/login
,当他/她通过身份验证时,我需要将用户重定向到相应的子域,例如demo.localhost
。 This is not working for me so far, even after checking some posts about the subject.到目前为止,这对我不起作用,即使在检查了一些有关该主题的帖子之后也是如此。
Here is the views.py in customers.views
where user log in:这是用户登录的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)
here is what urls in the core folder:这是核心文件夹中的网址:
urlpatterns = [
path('admin/', admin.site.urls),
path('django_plotly_dash/', include('django_plotly_dash.urls')),
path('mydashboard/', include('mydashboard.urls',namespace="mydashboard")),
]
and I have added app_name = 'mydashboard'
in mydashboard.urls
我在mydashboard.urls
中添加了app_name = 'mydashboard'
Here is the traceback of the error I keep getting:这是我不断收到的错误的回溯:
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
UPDATE: Remove the reverse
and included request.scheme
in the url.更新:删除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)
this is still working and output the following error:这仍然有效,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]
This is strange because the requested url is perfectly working url, registered and accessible without a problem before trying to redirect to it after the login这很奇怪,因为请求的 url 可以正常工作 url,在登录后尝试重定向到它之前已注册并且可以毫无问题地访问
EDIT #2:编辑#2:
Found out that the above code works when the target url does not require login required
发现上面的代码在目标 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')
I am obviously make sure that the app is secured and can only be accessed by logged in users.我显然确保该应用程序是安全的,并且只能由登录用户访问。 Is there a work around to make sure it works?是否有解决方法来确保它有效?
reverse
is an internal django utility to go from "app name" + ':' + "url friendly name" to "real url". reverse
是一个内部 django 实用程序到 go,从“应用程序名称”+“:”+“url 友好名称”到“真实 url”。 In your example, you're already providing the "real url".在您的示例中,您已经提供了“真实网址”。
Quit while you're ahead and just do:在你领先的时候退出,然后做:
if user is not None: login(request, user) url = request.scheme + '://' + schema_name + '.localhost:8000/mydashboard/' print("url") return HttpResponseRedirect(url)
reverse
注意没有reverse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.