skip to Main Content

I have been trying to display html templates of Login and Register for an ecommerce website, using Django Framework and Python as server side scripting language. I want to make sure that If Users logs in using its email address and password then It should be redirected to its dashboard. New User has to first register then that user is allowed to login.

I’m done with the backend coding of the templates, but now able to display UI first so that I can try to login from html template and the desired result should be display in default database of the Django. How I can do this?

2

Answers


  1. Your question isn’t very clear so I’ll try to respond to parts of it I understand. To create template html files, make a folder called template and put html files inside of it. Make sure to define the path to templates in your settings.py.

    For login/register html pages, create them as .html files inside of templates and then define the url patterns in urls.py.

    For the code to make login/register work, you need functions in views.py. This is an example I found on the internet:

    def login_view(request):
        if request.method == 'POST':
            form = LoginForm(request.POST)
            if form.is_valid():
                email = form.cleaned_data['email']
                password = form.cleaned_data['password']
                user = authenticate(request, email=email, password=password)
                if user is not None:
                    login(request, user)
                    return redirect('dashboard')  # Redirect to the dashboard page
        else:
            form = LoginForm()
        return render(request, 'login.html', {'form': form})
    
    def register_view(request):
        if request.method == 'POST':
            form = RegistrationForm(request.POST)
            if form.is_valid():
                form.save()  # Save the registered user to the database
                return redirect('login')  # Redirect to the login page
        else:
            form = RegistrationForm()
        return render(request, 'registration/register.html', {'form': form})
    
    Login or Signup to reply.
  2. Follow @daviid’s advice. Here you will find a link with several tutorials for the authentication system with Django.
    I would like to add that if you want your user to log in only with email and password without username you must create a CustomUser model.
    moreover, it is recommended by Django himself from version 1.7 onwards

    Tutorials link

    @absurdity9’s post seems to take into account that you don’t have the Customuser model, in fact if you don’t enter the username, you don’t go ahead . If you don’t want to delve deeper into the topic, follow it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search