User doesn't exists and no redirect to created tenant in django project

不问归期 提交于 2019-12-08 08:49:57

问题


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', )

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!