Entirely new to python and django framework.
Trying to build a login/registration system.'
After registering, redirects to home but user is not authenticated (returns false always)
views.py
def register(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
fullname = form.cleaned_data.get('fullname')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, fullname=fullname, password=password)
messages.success(request, f'Welcome to blaza {fullname}')
if user is not None:
login(request, user)
return redirect('home')
else:
form = SignUpForm()
return render(request, 'accounts/signup.html', {'form': form})
home.html
{% extends 'base.html' %}
{% block title %} Home {% endblock %}
{% block content %}
{% if user.is_authenticated %}
Welcome, {{user.username}}!
Logout
{% else %}
<P>You are not logged in</P>
<p>Login here</p>
{% endif %}
{% endblock %}
It returns false always. "You are not logged".
I have tried {% if request.user.is_authenticated %} still not working.
is_authenticated is a method not a property. I think you forgot the parenthesis.
Try this:
{% if request.user.is_authenticated() %}
Have you saved the user after signing up ?
myuser.save()
I have attached the code which worked for me.
def signup(request):
if request.method == 'POST':
username = request.POST['username']
name = request.POST['name']
email = request.POST['email']
pass1 = request.POST['pass1']
pass2 = request.POST['pass2']
myuser = User.objects.create_user(username, email)
myuser.set_password(pass1)
myuser.us_name = name
myuser.save()
messages.success(request, "Your account has been sucessfully created. ")
return redirect('signin')
return render(request, "authentication/signup.html")
Related
Below code probably works (no errors present):
views.pl
class SignInView(View):
def get(self, request):
return render(request, "signin.html")
def post(self, request):
user = request.POST.get('username', '')
pass = request.POST.get('password', '')
user = authenticate(username=user, password=pass)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/')
else:
return HttpResponse("Bad user.")
else:
return HttpResponseRedirect('/')
....but in template:
{% user.is_authenticated %}
is not True. So I don't see any functionality for authenticated user.
What is the problem?
You should do something like:
{% if request.user.is_authenticated %}
<!-- code for authenticated user -->
{% else %}
<!-- code for unauthenticated user -->
{% endif %}
I could see another problem in views, pass is a reverse keyword in Python, you should also change variable name.
You should do like {% if request.user.is_authenticated %} or {% if user.is_authenticated %}
Hello I am making Django App. I have a problem with my login form. Whenever I want to login,
form does not do anything or it throws csrf token error.
Views:
def loginView(request):
if request.method == 'POST':
form = AuthenticationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f"You successfully logged in {username}")
return redirect('home-page')
else:
form = AuthenticationForm()
return render(request, 'shop/login.html', {'form': form})
HTML TEMPLATE:
{% extends 'shop/base.html' %}
{% block content %}
<div class = "form-container">
<form class="form" method="POST">{% csrf_token %}
<label for="username">Email:</label>
{{form.username}}
<label for="password">Passoword:</label>
{{form.password}}
<button type="submit">Login</button>
</form>
</div>
{% endblock content %}
you have to check whether user existed or not in db if yes then login and redirect if not throw error or redirect on other page
def my_login(request):
if request.method == 'POST':
form = AuthenticationForm(request.POST)
if form.is_valid():
username = form.cleaned_data["username"]
password = form.cleaned_data["password"]
user = authenticate(username=username, password=password)
if user:
login(request, user)
return redirect('path')
else:
return redirect('path')
else:
return redirect('path')
else:
form = AuthenticationForm()
return render(request, "shop/login.html", {'form': form})
still get error add the complete traceback & i will also suggest you to read the full documentation of django https://docs.djangoproject.com/en/3.2/ref/contrib/auth/
I have refrenced this stackoverflow page and tried to display my forms error on the html template.
I did:
{% if form.error %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
as said in the stackoverflow question I also tried simply doing:
{% if form.error %}
This should pop up if there is an error
{% endif %}
Nothing comes up regardless:
Heres my view.py code:
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password2')
user = authenticate(username=username, password=password)
login(request, user)
return HttpResponseRedirect('/')
else:
print(form.errors)
form = UserCreationForm()
return render(request, 'registration/register.html', {'form': form})
I am able to get the form errors onto the django console but it refuses to show up on the template.
Printing form.errors prints to the console: <li>password2<ul class="errorlist"><li>The two password fields didn't match.</li></ul></li></ul>
forms.errors fired up, but at the end, you declare a new form form = UserCreationForm() just before you render your view.
After checking whether the form is valid or not, all your validation errors are inside form instance, remember processes run as sequence, at the end, you destroy the variable form with form = UserCreationForm() so no validation errors anymore.
What you can do is add this new form form = UserCreationForm() to else statement when your request method is GET to keep having an empty form. By adding the else statement you avoid the new assignment of the form; after the validation process, it will jump to render(request,....) with the form instance containing all validation errors
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password2')
user = authenticate(username=username, password=password)
login(request, user)
return HttpResponseRedirect('/')
else:
print(form.errors)
else:
form = UserCreationForm()
return render(request, 'registration/register.html', {'form': form})
Note, the correct call for form errors in templates is form.errors with s not form.error
{% if form.error %} {% if form.errors %}
I have a custom signup app from views:
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
userObj = form.cleaned_data
username = userObj['username']
email = userObj['email']
password1 = userObj['password1']
password2 = userObj['password2']
if password1 != password2:
return HttpResponse('password not match')
elif User.objects.filter(email=email).exists():
return HttpResponse('email must be unique')
elif User.objects.filter(username=username).exists():
return HttpResponse('username exists')
else:
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
mail_subject = 'Activation'
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return HttpResponse('Letter is sent')
else:
form = SignupForm()
return render(request, 'signup.html', {'form': form})
This is the only way I made test for unique email working (models didn't work, dont know why).
How can i make custom errors which i can put to my html using template tags {% if %}?
Thanks!
You have not tried using the Django message framework.
Look:
Adding a message
To add a message, call:
from django.contrib import messages
messages.add_message(request, messages.INFO, 'Hello world.')
Some shortcut methods provide a standard way to add messages with commonly used tags (which are usually represented as HTML classes for the message):
messages.debug(request, '%s SQL statements were executed.' % count)
messages.info(request, 'Three credits remain in your account.')
messages.success(request, 'Profile details updated.')
messages.warning(request, 'Your account expires in three days.')
messages.error(request, 'Document deleted.')
Displaying messages
get_messages(request)[source]
In your template, use something like:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
If you’re using the context processor, your template should be rendered with a RequestContext. Otherwise, ensure messages is available to the template context.
Even if you know there is only just one message, you should still iterate over the messages sequence, because otherwise the message storage will not be cleared for the next request.
The context processor also provides a DEFAULT_MESSAGE_LEVELS variable which is a mapping of the message level names to their numeric value:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>
{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}Important: {% endif %}
{{ message }}
</li>
{% endfor %}
</ul>
{% endif %}
EDIT:
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
userObj = form.cleaned_data
username = userObj['username']
email = userObj['email']
password1 = userObj['password1']
password2 = userObj['password2']
if password1 != password2:
message = 'password not match'
elif User.objects.filter(email=email).exists():
message = 'email must be unique'
elif User.objects.filter(username=username).exists():
message = 'email must be unique'
messages.error(request, message)
else:
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
mail_subject = 'Activation'
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
message = 'Letter was sent'
messages.success(request, message)
else:
form = SignupForm()
return render(request, 'signup.html', {'form': form})
Hope it Helps.
If we want to make a custom additional validation we can use messages.
First of all add in views:
from django.contrib import messages
Then we just add terms of sending message to our HTML, for example:
if User.objects.filter(email=email).exists():
messages.warning(request, 'email error.')
Now we can work with:
{% if messages %}
{% endif %}
In our HTML.
Also we can customise all our validations and ignore is_valid method.
Using:
from django.core import validators
we can use validate_email or validate_slug for fields we need.
I have created a form to add users in my front-end but the form does not validate duplicated username.I am using auth.user model.
This is my code:
views.py
from django.contrib.auth.models import User, Group
#login_required(login_url='/login/')
#permission_required('auth.add_user',raise_exception=True)
def user_new(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.set_password(user.password)
user.save()
return redirect('userdetail', user.id)
else:
form = NewUserForm()
return render(request, 'ace/user_edit.html', {'form': form})
forms.py
class NewUserForm(forms.ModelForm):
class Meta:
model = User
fields = ['username','first_name','last_name','password','email','is_active','is_staff','groups']
widgets = {
'username':TextInput(attrs={'class': u'form-control'}),
'first_name':TextInput(attrs={'class': u'form-control'}),
'last_name':TextInput(attrs={'class': u'form-control'}),
'password':PasswordInput(attrs={'class': u'form-control'}),
'email':EmailInput(attrs={'class': u'form-control'}),
'is_active':NullBooleanSelect(attrs={'class': u'form-control'}),
'is_staff':NullBooleanSelect(attrs={'class': u'form-control'}),
'groups':SelectMultiple(attrs={'class': u'form-control'}),
}
def clean_username(self):
username = self.cleaned_data['username']
user_exists = User.objects.get(username=username)
if user_exists:
raise ValidationError("User exists")
template
...
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}
<form method="POST" class="service-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-info">Salvar</button>
<a href="{% url 'userlist' %}">
<button class="btn btn-danger" type="button">Cancelar</button>
</a>
</form>
...
When I create a new user OK, but when a try create a user that same username of other I get a error:
The view ace.views.user_new didn't return an HttpResponse object. It
returned None instead.
If I add a print line "print form.errors" in view i get in console:
usernameUser
exists
Your view does not have an else statement for if, form is not valid it should render the template with form errors.
You need to change your view like this,
def user_new(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.set_password(user.password)
user.save()
return redirect('userdetail', user.id)
else:
return render(request, 'ace/user_edit.html', {'form': form})
else:
form = NewUserForm()
return render(request, 'ace/user_edit.html', {'form': form})
And also you need to add the tag {%for field in form%} {{field.error}}{%endfor%} along with the form fields and labels.
You need to make sure that your view returns a response for POST requests when the form is invalid. You can do this by moving the final return render() statement out of the else block.
def user_new(request):
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
...
return redirect('userdetail', user.id)
else:
form = NewUserForm()
return render(request, 'ace/user_edit.html', {'form': form})
For registration django.contrib.auth User needs the username field to be unique. If you want to use other model field as unique (as unique registration field) and not the username, for example the email field, you can use this approach or use other registration bakends like django registration or django registration redux.
Instead of fixing the bug in your code I suggest to not invent the wheel and use excellent django-allauth package. It handles user login, logout, change password, registration and social sign in. I always start new projects from adding django-allauth - it handles all authentication problems with no effort.
You can use the saved time and effort to write actual application code instead of solving trivial user management details.
Also, the proper way to check for existence of the model instance is this:
user_exists = User.objects.filter(username=username).exists()
if user_exists:
raise ValidationError("User exists")