用户不存在,并且没有重定向到Django项目中创建的租户 [英] User doesn't exists and no redirect to created tenant in django project
问题描述
我在django 1.11中有一个应用程序,我在其中使用django-tenant-schemas( https://github.com/bernardopires/django-tenant-schemas )为用户创建一个帐户. 创建客户端,架构和domain_url之后,不会将用户重定向到domain_url中给定的地址.
I have an application in django 1.11 where I use django-tenant-schemas (https://github.com/bernardopires/django-tenant-schemas) to create an account for the user. After creating a client and schema and domain_url, the user is not redirected to the address given in domain_url.
例如:我的表单中具有domain_url = test.localhost. 创建帐户后,我仍位于localhost而不是test.localhost.
For example: I have domain_url = test.localhost in the form. After creating an account, I am still on localhost instead of test.localhost.
当我进入test.localhost时,会看到一个登录面板.我使用创建时提供的数据登录,但收到一条消息,要求输入正确的数据.我使用shell检查数据库-用户存在.
When I go to test.localhost I get a login panel. I log in with the data I provided when creating, but I get a message to enter the correct data. I check the database using shell - the user exists.
用户使用ForeignKey连接到公司.
The user is connected to the Company using ForeignKey.
accounts/view.py
accounts/view.py
def signup(request):
if request.method == 'POST':
company_form = CompanyForm(request.POST, prefix='company')
user_form = SignUpForm(request.POST, prefix='user')
if company_form.is_valid() and user_form.is_valid():
company_form.instance.name = company_form.cleaned_data['name']
company_form.instance.domain_url = company_form.cleaned_data['name'] + '.localhost'
company_form.instance.schema_name = company_form.cleaned_data['name']
company = company_form.save()
user_form.instance.company = company
user = user_form.save()
auth_login(request, user)
return HttpResponseRedirect(reverse('post:post_list'))
else:
company_form = CompanyForm(prefix='company')
user_form = SignUpForm(prefix='user')
args = {}
args.update(csrf(request))
args['company_form'] = company_form
args['user_form'] = user_form
return render(request, 'accounts/signup.html', args)
用于创建公司和用户的表格:
Forms to create company and user:
class CompanyForm(forms.ModelForm):
name = forms.CharField(label='Company', widget=forms.TextInput(attrs={'autofocus': 'autofocus'}))
class Meta:
model = Company
fields = ('name',)
class SignUpForm(UserCreationForm):
email = forms.EmailField(max_length=254, required=True, widget=forms.EmailInput())
class Meta:
model = User
exclude = ('company', )
fields = ('email', 'password1', 'password2', )
推荐答案
re:用户不存在错误-
根据您的注册视图功能,将在公共架构中创建用户记录.但是,当您尝试登录到test.localhost
时,所有数据库查询都使用test
模式.您可以切换-
re: User doesn't exist error -
According to your signup view function, the user record is getting created in the public schema. But when you try to login to test.localhost
, test
schema is being used for all the DB queries. You can switch schemas like -
from tenant_schemas.utils import schema_context
with schema_context(customer.schema_name):
# create your user here.
这篇关于用户不存在,并且没有重定向到Django项目中创建的租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!