Can't login in admin page after accessing my website pages
up vote
1
down vote
favorite
I created a superuser in my admin section. After that I can log in admin page. But Whenever I try accessing some myapp pages then and then I again want to access the admin page It shows this error"Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive." I tried it multiple times. And I can create a new superuser by same name I created last time.
Here is two screenshot:
Login Error
creating superuser
How to solve this issue?
Update:
forms.py
from django.contrib.auth.models import User
from django.forms import ModelForm
from django import forms
from .models import Profile
class UserForm(forms.ModelForm):
password= forms.CharField(widget= forms.PasswordInput)
class Meta:
model = User
fields = ('username','email','password')
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('full_name','codeforces_id','Uva_Id')
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
full_name = models.CharField(max_length=256, blank=False)
codeforces_id = models.CharField(max_length=256, blank=False)
Uva_Id = models.CharField(max_length=256, blank=False)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
views.py:
from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from .forms import UserForm,ProfileForm
class UserFormView(View):
user_form_class = UserForm
profile_form_class= ProfileForm
#display a blank form
def get(self , request):
user_form = self.user_form_class(None)
profile_form = self.profile_form_class(None)
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
#process form data
def post(self, request):
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save(commit= False)
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user.save()
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active:
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
python django
add a comment |
up vote
1
down vote
favorite
I created a superuser in my admin section. After that I can log in admin page. But Whenever I try accessing some myapp pages then and then I again want to access the admin page It shows this error"Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive." I tried it multiple times. And I can create a new superuser by same name I created last time.
Here is two screenshot:
Login Error
creating superuser
How to solve this issue?
Update:
forms.py
from django.contrib.auth.models import User
from django.forms import ModelForm
from django import forms
from .models import Profile
class UserForm(forms.ModelForm):
password= forms.CharField(widget= forms.PasswordInput)
class Meta:
model = User
fields = ('username','email','password')
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('full_name','codeforces_id','Uva_Id')
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
full_name = models.CharField(max_length=256, blank=False)
codeforces_id = models.CharField(max_length=256, blank=False)
Uva_Id = models.CharField(max_length=256, blank=False)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
views.py:
from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from .forms import UserForm,ProfileForm
class UserFormView(View):
user_form_class = UserForm
profile_form_class= ProfileForm
#display a blank form
def get(self , request):
user_form = self.user_form_class(None)
profile_form = self.profile_form_class(None)
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
#process form data
def post(self, request):
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save(commit= False)
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user.save()
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active:
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
python django
please share some codes about User Model and User Model Manager
– ruddra
Nov 21 at 19:52
Post updated. Actually I'm facing a lot others issue. Tried to debug but now this errors occurs
– Ahsan Shuvo
Nov 21 at 19:59
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I created a superuser in my admin section. After that I can log in admin page. But Whenever I try accessing some myapp pages then and then I again want to access the admin page It shows this error"Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive." I tried it multiple times. And I can create a new superuser by same name I created last time.
Here is two screenshot:
Login Error
creating superuser
How to solve this issue?
Update:
forms.py
from django.contrib.auth.models import User
from django.forms import ModelForm
from django import forms
from .models import Profile
class UserForm(forms.ModelForm):
password= forms.CharField(widget= forms.PasswordInput)
class Meta:
model = User
fields = ('username','email','password')
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('full_name','codeforces_id','Uva_Id')
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
full_name = models.CharField(max_length=256, blank=False)
codeforces_id = models.CharField(max_length=256, blank=False)
Uva_Id = models.CharField(max_length=256, blank=False)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
views.py:
from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from .forms import UserForm,ProfileForm
class UserFormView(View):
user_form_class = UserForm
profile_form_class= ProfileForm
#display a blank form
def get(self , request):
user_form = self.user_form_class(None)
profile_form = self.profile_form_class(None)
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
#process form data
def post(self, request):
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save(commit= False)
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user.save()
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active:
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
python django
I created a superuser in my admin section. After that I can log in admin page. But Whenever I try accessing some myapp pages then and then I again want to access the admin page It shows this error"Please enter the correct username and password for a staff account. Note that both fields may be case-sensitive." I tried it multiple times. And I can create a new superuser by same name I created last time.
Here is two screenshot:
Login Error
creating superuser
How to solve this issue?
Update:
forms.py
from django.contrib.auth.models import User
from django.forms import ModelForm
from django import forms
from .models import Profile
class UserForm(forms.ModelForm):
password= forms.CharField(widget= forms.PasswordInput)
class Meta:
model = User
fields = ('username','email','password')
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('full_name','codeforces_id','Uva_Id')
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
full_name = models.CharField(max_length=256, blank=False)
codeforces_id = models.CharField(max_length=256, blank=False)
Uva_Id = models.CharField(max_length=256, blank=False)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
views.py:
from django.http import HttpResponse
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from .forms import UserForm,ProfileForm
class UserFormView(View):
user_form_class = UserForm
profile_form_class= ProfileForm
#display a blank form
def get(self , request):
user_form = self.user_form_class(None)
profile_form = self.profile_form_class(None)
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
#process form data
def post(self, request):
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(request.POST, instance=request.user.profile)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save(commit= False)
password= user_form.cleaned_data['password']
username = user_form.cleaned_data['username']
user.set_password(password)
user.save()
profile_form.save()
# auto login
user = authenticate(username =username, password = password)
if user is not None:
if user.is_active:
login(request,user)
return redirect('website:index')
return render(request, 'website/registration_form.html',{
'user_form':user_form,
'profile_form':profile_form
})
python django
python django
edited Nov 21 at 19:58
asked Nov 21 at 19:46
Ahsan Shuvo
65
65
please share some codes about User Model and User Model Manager
– ruddra
Nov 21 at 19:52
Post updated. Actually I'm facing a lot others issue. Tried to debug but now this errors occurs
– Ahsan Shuvo
Nov 21 at 19:59
add a comment |
please share some codes about User Model and User Model Manager
– ruddra
Nov 21 at 19:52
Post updated. Actually I'm facing a lot others issue. Tried to debug but now this errors occurs
– Ahsan Shuvo
Nov 21 at 19:59
please share some codes about User Model and User Model Manager
– ruddra
Nov 21 at 19:52
please share some codes about User Model and User Model Manager
– ruddra
Nov 21 at 19:52
Post updated. Actually I'm facing a lot others issue. Tried to debug but now this errors occurs
– Ahsan Shuvo
Nov 21 at 19:59
Post updated. Actually I'm facing a lot others issue. Tried to debug but now this errors occurs
– Ahsan Shuvo
Nov 21 at 19:59
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
I think you are over complicating things. So try like this:
# forms
class UserForm(forms.ModelForm):
password = forms.CharField(widget= forms.PasswordInput)
full_name = forms.CharField()
codeforces_id = forms.CharField()
Uva_Id = forms.CharField()
class Meta:
model = User
fields = ('username','email','password', 'full_name', 'codeforces_id', 'Uva_Id')
def save(self, commit=True):
full_name = self.cleaned_data.pop('full_name')
codeforces_id = self.cleaned_data.pop('codeforces_id')
uva_Id = self.cleaned_data.pop('Uva_Id')
user = super(UserForm, self).save(commit=False)
password = self.cleaned_data.get('password')
username = self.cleaned_data.get('username')
user.set_password(password)
user.save()
Profile.objects.create(
user=user,
full_name = full_name,
codeforces_id = codeforces_id,
Uva_Id = uva_id
)
authenticate(username=username, password=password)
return user
# view
class UserFormView(CreateView):
form_class = UserForm
template_name = 'some_template.html'
# template
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
# Also you can remove the signal related codes from Profile Model as well
What should I do in post and get method in views.py file ?
– Ahsan Shuvo
Nov 21 at 20:53
@AhsanShuvo you don't need to override them. Create View will take care ofgetandpostautomatically. and the codes which you had put on there now in Form'ssavemethod (like authentication).
– ruddra
Nov 21 at 21:03
My bad. I followed your approach. But now I am facing a new error "ImproperlyConfigured at /website/register/" "TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'".
– Ahsan Shuvo
Nov 21 at 21:34
I've fixed the issue. :)
– Ahsan Shuvo
Nov 21 at 21:50
Ah. I should have named 'template_name', not 'template'. If this answer helped you, please mark it as accepted :)
– ruddra
Nov 22 at 1:47
add a comment |
up vote
0
down vote
You need to use a LoginRequiredMixin for your views this will authenticate the request.
settings.py
# Redirect to home URL after login
LOGIN_REDIRECT_URL = '/'
# Redirect to the login page after log out.
LOGOUT_REDIRECT_URL = '/accounts/login/'
views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import FormView
from django.forms import all_valid
# Using a generic form view and you won't need all of the code above.
class UserFormView(LoginRequiredMixin, FormView):
form_class = UserForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['user_form'] = context['form']
context['profile_form'] = self._get_profile_form()
return context
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['instance'] = self.request.user
return kwargs
def _get_profile_form(self):
form_kwargs = self.get_form_kwargs()
form_kwargs['instance'] = request.user.profile()
return ProfileForm(**form_kwargs)
def post(self, request, *args, **kwargs):
form = self.get_form()
profile_form = self._get_profile_form()
if all_valid([form, profile_form]):
return self.form_valid(form, profile_form)
else:
return self.form_invalid(form, profile_form)
def form_valid(self, form, profile_form=None):
# Handle both forms being valid.
...
def form_invalid(form, profile_form=None):
# Handle one or all of the forms being invalid.
...
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I think you are over complicating things. So try like this:
# forms
class UserForm(forms.ModelForm):
password = forms.CharField(widget= forms.PasswordInput)
full_name = forms.CharField()
codeforces_id = forms.CharField()
Uva_Id = forms.CharField()
class Meta:
model = User
fields = ('username','email','password', 'full_name', 'codeforces_id', 'Uva_Id')
def save(self, commit=True):
full_name = self.cleaned_data.pop('full_name')
codeforces_id = self.cleaned_data.pop('codeforces_id')
uva_Id = self.cleaned_data.pop('Uva_Id')
user = super(UserForm, self).save(commit=False)
password = self.cleaned_data.get('password')
username = self.cleaned_data.get('username')
user.set_password(password)
user.save()
Profile.objects.create(
user=user,
full_name = full_name,
codeforces_id = codeforces_id,
Uva_Id = uva_id
)
authenticate(username=username, password=password)
return user
# view
class UserFormView(CreateView):
form_class = UserForm
template_name = 'some_template.html'
# template
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
# Also you can remove the signal related codes from Profile Model as well
What should I do in post and get method in views.py file ?
– Ahsan Shuvo
Nov 21 at 20:53
@AhsanShuvo you don't need to override them. Create View will take care ofgetandpostautomatically. and the codes which you had put on there now in Form'ssavemethod (like authentication).
– ruddra
Nov 21 at 21:03
My bad. I followed your approach. But now I am facing a new error "ImproperlyConfigured at /website/register/" "TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'".
– Ahsan Shuvo
Nov 21 at 21:34
I've fixed the issue. :)
– Ahsan Shuvo
Nov 21 at 21:50
Ah. I should have named 'template_name', not 'template'. If this answer helped you, please mark it as accepted :)
– ruddra
Nov 22 at 1:47
add a comment |
up vote
0
down vote
accepted
I think you are over complicating things. So try like this:
# forms
class UserForm(forms.ModelForm):
password = forms.CharField(widget= forms.PasswordInput)
full_name = forms.CharField()
codeforces_id = forms.CharField()
Uva_Id = forms.CharField()
class Meta:
model = User
fields = ('username','email','password', 'full_name', 'codeforces_id', 'Uva_Id')
def save(self, commit=True):
full_name = self.cleaned_data.pop('full_name')
codeforces_id = self.cleaned_data.pop('codeforces_id')
uva_Id = self.cleaned_data.pop('Uva_Id')
user = super(UserForm, self).save(commit=False)
password = self.cleaned_data.get('password')
username = self.cleaned_data.get('username')
user.set_password(password)
user.save()
Profile.objects.create(
user=user,
full_name = full_name,
codeforces_id = codeforces_id,
Uva_Id = uva_id
)
authenticate(username=username, password=password)
return user
# view
class UserFormView(CreateView):
form_class = UserForm
template_name = 'some_template.html'
# template
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
# Also you can remove the signal related codes from Profile Model as well
What should I do in post and get method in views.py file ?
– Ahsan Shuvo
Nov 21 at 20:53
@AhsanShuvo you don't need to override them. Create View will take care ofgetandpostautomatically. and the codes which you had put on there now in Form'ssavemethod (like authentication).
– ruddra
Nov 21 at 21:03
My bad. I followed your approach. But now I am facing a new error "ImproperlyConfigured at /website/register/" "TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'".
– Ahsan Shuvo
Nov 21 at 21:34
I've fixed the issue. :)
– Ahsan Shuvo
Nov 21 at 21:50
Ah. I should have named 'template_name', not 'template'. If this answer helped you, please mark it as accepted :)
– ruddra
Nov 22 at 1:47
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I think you are over complicating things. So try like this:
# forms
class UserForm(forms.ModelForm):
password = forms.CharField(widget= forms.PasswordInput)
full_name = forms.CharField()
codeforces_id = forms.CharField()
Uva_Id = forms.CharField()
class Meta:
model = User
fields = ('username','email','password', 'full_name', 'codeforces_id', 'Uva_Id')
def save(self, commit=True):
full_name = self.cleaned_data.pop('full_name')
codeforces_id = self.cleaned_data.pop('codeforces_id')
uva_Id = self.cleaned_data.pop('Uva_Id')
user = super(UserForm, self).save(commit=False)
password = self.cleaned_data.get('password')
username = self.cleaned_data.get('username')
user.set_password(password)
user.save()
Profile.objects.create(
user=user,
full_name = full_name,
codeforces_id = codeforces_id,
Uva_Id = uva_id
)
authenticate(username=username, password=password)
return user
# view
class UserFormView(CreateView):
form_class = UserForm
template_name = 'some_template.html'
# template
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
# Also you can remove the signal related codes from Profile Model as well
I think you are over complicating things. So try like this:
# forms
class UserForm(forms.ModelForm):
password = forms.CharField(widget= forms.PasswordInput)
full_name = forms.CharField()
codeforces_id = forms.CharField()
Uva_Id = forms.CharField()
class Meta:
model = User
fields = ('username','email','password', 'full_name', 'codeforces_id', 'Uva_Id')
def save(self, commit=True):
full_name = self.cleaned_data.pop('full_name')
codeforces_id = self.cleaned_data.pop('codeforces_id')
uva_Id = self.cleaned_data.pop('Uva_Id')
user = super(UserForm, self).save(commit=False)
password = self.cleaned_data.get('password')
username = self.cleaned_data.get('username')
user.set_password(password)
user.save()
Profile.objects.create(
user=user,
full_name = full_name,
codeforces_id = codeforces_id,
Uva_Id = uva_id
)
authenticate(username=username, password=password)
return user
# view
class UserFormView(CreateView):
form_class = UserForm
template_name = 'some_template.html'
# template
<form action="/your-name/" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
# Also you can remove the signal related codes from Profile Model as well
edited Nov 22 at 7:51
answered Nov 21 at 20:33
ruddra
10.7k32647
10.7k32647
What should I do in post and get method in views.py file ?
– Ahsan Shuvo
Nov 21 at 20:53
@AhsanShuvo you don't need to override them. Create View will take care ofgetandpostautomatically. and the codes which you had put on there now in Form'ssavemethod (like authentication).
– ruddra
Nov 21 at 21:03
My bad. I followed your approach. But now I am facing a new error "ImproperlyConfigured at /website/register/" "TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'".
– Ahsan Shuvo
Nov 21 at 21:34
I've fixed the issue. :)
– Ahsan Shuvo
Nov 21 at 21:50
Ah. I should have named 'template_name', not 'template'. If this answer helped you, please mark it as accepted :)
– ruddra
Nov 22 at 1:47
add a comment |
What should I do in post and get method in views.py file ?
– Ahsan Shuvo
Nov 21 at 20:53
@AhsanShuvo you don't need to override them. Create View will take care ofgetandpostautomatically. and the codes which you had put on there now in Form'ssavemethod (like authentication).
– ruddra
Nov 21 at 21:03
My bad. I followed your approach. But now I am facing a new error "ImproperlyConfigured at /website/register/" "TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'".
– Ahsan Shuvo
Nov 21 at 21:34
I've fixed the issue. :)
– Ahsan Shuvo
Nov 21 at 21:50
Ah. I should have named 'template_name', not 'template'. If this answer helped you, please mark it as accepted :)
– ruddra
Nov 22 at 1:47
What should I do in post and get method in views.py file ?
– Ahsan Shuvo
Nov 21 at 20:53
What should I do in post and get method in views.py file ?
– Ahsan Shuvo
Nov 21 at 20:53
@AhsanShuvo you don't need to override them. Create View will take care of
get and post automatically. and the codes which you had put on there now in Form's save method (like authentication).– ruddra
Nov 21 at 21:03
@AhsanShuvo you don't need to override them. Create View will take care of
get and post automatically. and the codes which you had put on there now in Form's save method (like authentication).– ruddra
Nov 21 at 21:03
My bad. I followed your approach. But now I am facing a new error "ImproperlyConfigured at /website/register/" "TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'".
– Ahsan Shuvo
Nov 21 at 21:34
My bad. I followed your approach. But now I am facing a new error "ImproperlyConfigured at /website/register/" "TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'".
– Ahsan Shuvo
Nov 21 at 21:34
I've fixed the issue. :)
– Ahsan Shuvo
Nov 21 at 21:50
I've fixed the issue. :)
– Ahsan Shuvo
Nov 21 at 21:50
Ah. I should have named 'template_name', not 'template'. If this answer helped you, please mark it as accepted :)
– ruddra
Nov 22 at 1:47
Ah. I should have named 'template_name', not 'template'. If this answer helped you, please mark it as accepted :)
– ruddra
Nov 22 at 1:47
add a comment |
up vote
0
down vote
You need to use a LoginRequiredMixin for your views this will authenticate the request.
settings.py
# Redirect to home URL after login
LOGIN_REDIRECT_URL = '/'
# Redirect to the login page after log out.
LOGOUT_REDIRECT_URL = '/accounts/login/'
views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import FormView
from django.forms import all_valid
# Using a generic form view and you won't need all of the code above.
class UserFormView(LoginRequiredMixin, FormView):
form_class = UserForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['user_form'] = context['form']
context['profile_form'] = self._get_profile_form()
return context
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['instance'] = self.request.user
return kwargs
def _get_profile_form(self):
form_kwargs = self.get_form_kwargs()
form_kwargs['instance'] = request.user.profile()
return ProfileForm(**form_kwargs)
def post(self, request, *args, **kwargs):
form = self.get_form()
profile_form = self._get_profile_form()
if all_valid([form, profile_form]):
return self.form_valid(form, profile_form)
else:
return self.form_invalid(form, profile_form)
def form_valid(self, form, profile_form=None):
# Handle both forms being valid.
...
def form_invalid(form, profile_form=None):
# Handle one or all of the forms being invalid.
...
add a comment |
up vote
0
down vote
You need to use a LoginRequiredMixin for your views this will authenticate the request.
settings.py
# Redirect to home URL after login
LOGIN_REDIRECT_URL = '/'
# Redirect to the login page after log out.
LOGOUT_REDIRECT_URL = '/accounts/login/'
views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import FormView
from django.forms import all_valid
# Using a generic form view and you won't need all of the code above.
class UserFormView(LoginRequiredMixin, FormView):
form_class = UserForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['user_form'] = context['form']
context['profile_form'] = self._get_profile_form()
return context
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['instance'] = self.request.user
return kwargs
def _get_profile_form(self):
form_kwargs = self.get_form_kwargs()
form_kwargs['instance'] = request.user.profile()
return ProfileForm(**form_kwargs)
def post(self, request, *args, **kwargs):
form = self.get_form()
profile_form = self._get_profile_form()
if all_valid([form, profile_form]):
return self.form_valid(form, profile_form)
else:
return self.form_invalid(form, profile_form)
def form_valid(self, form, profile_form=None):
# Handle both forms being valid.
...
def form_invalid(form, profile_form=None):
# Handle one or all of the forms being invalid.
...
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to use a LoginRequiredMixin for your views this will authenticate the request.
settings.py
# Redirect to home URL after login
LOGIN_REDIRECT_URL = '/'
# Redirect to the login page after log out.
LOGOUT_REDIRECT_URL = '/accounts/login/'
views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import FormView
from django.forms import all_valid
# Using a generic form view and you won't need all of the code above.
class UserFormView(LoginRequiredMixin, FormView):
form_class = UserForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['user_form'] = context['form']
context['profile_form'] = self._get_profile_form()
return context
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['instance'] = self.request.user
return kwargs
def _get_profile_form(self):
form_kwargs = self.get_form_kwargs()
form_kwargs['instance'] = request.user.profile()
return ProfileForm(**form_kwargs)
def post(self, request, *args, **kwargs):
form = self.get_form()
profile_form = self._get_profile_form()
if all_valid([form, profile_form]):
return self.form_valid(form, profile_form)
else:
return self.form_invalid(form, profile_form)
def form_valid(self, form, profile_form=None):
# Handle both forms being valid.
...
def form_invalid(form, profile_form=None):
# Handle one or all of the forms being invalid.
...
You need to use a LoginRequiredMixin for your views this will authenticate the request.
settings.py
# Redirect to home URL after login
LOGIN_REDIRECT_URL = '/'
# Redirect to the login page after log out.
LOGOUT_REDIRECT_URL = '/accounts/login/'
views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import FormView
from django.forms import all_valid
# Using a generic form view and you won't need all of the code above.
class UserFormView(LoginRequiredMixin, FormView):
form_class = UserForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['user_form'] = context['form']
context['profile_form'] = self._get_profile_form()
return context
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['instance'] = self.request.user
return kwargs
def _get_profile_form(self):
form_kwargs = self.get_form_kwargs()
form_kwargs['instance'] = request.user.profile()
return ProfileForm(**form_kwargs)
def post(self, request, *args, **kwargs):
form = self.get_form()
profile_form = self._get_profile_form()
if all_valid([form, profile_form]):
return self.form_valid(form, profile_form)
else:
return self.form_invalid(form, profile_form)
def form_valid(self, form, profile_form=None):
# Handle both forms being valid.
...
def form_invalid(form, profile_form=None):
# Handle one or all of the forms being invalid.
...
answered Nov 21 at 20:40
jackotonye
1,3251120
1,3251120
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419504%2fcant-login-in-admin-page-after-accessing-my-website-pages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
please share some codes about User Model and User Model Manager
– ruddra
Nov 21 at 19:52
Post updated. Actually I'm facing a lot others issue. Tried to debug but now this errors occurs
– Ahsan Shuvo
Nov 21 at 19:59