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.
For example: I have domain_url = test.localhost in the form. After creating an account, I am still on localhost instead of test.localhost.
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.
The user is connected to the Company using ForeignKey.
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: 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.
来源:https://stackoverflow.com/questions/55012639/user-doesnt-exists-and-no-redirect-to-created-tenant-in-django-project