0
votes

I have a lock screen page which only consist of a password field(INPUT) and just a username image above the password field. Keep in mind that lock screen is different from a login page.

Note that:- Login form requires Username and the Password while the Lock screen form does only require the password since the user did not actually logged out but its held somewhere to my knowledge.

Here's the simple login function view.py

def lockscreen(request):

    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(request, username=username, password=password)

        if user is not None:
            login(request, user)
            return redirect('core:dashboard')

        else:
            messages.info(request, 'Username OR Password is incorrect!')

    context = {}
    return render(request, 'accounts/lockscreen.html', context)

html

<div class="auth-form-transparent text-left p-5 text-center">
                <img src="{% static 'images/faces/face15.jpg' %}" class="lock-profile-img" alt="img">
                <form class="pt-5" method="POST">
                    {% csrf_token %}
                  <div class="form-group">
                    <label for="examplePassword1">Password to unlock</label>
                    <input type="password" name="password" class="form-control text-center" id="exampleInputPassword" placeholder="Password">
                  </div>
                  <div class="mt-5">
                    <button type="submit" value="Login" class="btn btn-block btn-gradient-primary btn-lg font-weight-medium auth-form-btn" >Unlock</button>
                  </div>
                  <div class="mt-3 text-center">
                    <a href="{% url 'login' %}" class="auth-link">Sign in using a different account</a>
                  </div>
                </form>
              </div>

forms.py

class CreateUserForm(UserCreationForm):
    username        = forms.CharField(max_length=255, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder':'User Name ...'}))
    password1       = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder':'Password ...'}))
    password2       = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder':'Confirm Password ...'}))
    email           = forms.CharField(max_length=255, widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder':'E-mail ...'}))

    name            = forms.CharField(max_length=255, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder':'Full Name ...'}))
    phone           = forms.CharField(max_length=255, widget=forms.NumberInput(attrs={'class': 'form-control', 'placeholder':'Phone Number ...'}))
    account_type    = forms.ChoiceField(choices = ACCOUNT_TYPE,  widget=forms.Select(attrs={'class': 'form-control', 'placeholder':'Phone Number ...'}))



    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

Any idea how to achieve this kind of situation. Thank you

1

1 Answers

1
votes

You could save the username in the session, when he signs in.

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Sessions

With this you can access the username in the lockscreen function.