0
from django.http import HttpResponse
from django.shortcuts import render,redirect, get_list_or_404, get_object_or_404
from .models import Users
from .forms import UsersForm
from django.contrib.auth import authenticate

# Create your views here.

def login(request):

    #Username = request.POST.get(‘username’)
    #password = request.POST.get(‘password’)

    form = UsersForm(request.POST or None)
    if form.is_valid():
        form.save()
        form = UsersForm()
        return redirect('/product/itemlist')  
    user = authenticate(username='username', password='password')
    if user is not None:
    # redirect to homepage
        return redirect('/product/itemlist')
    else:
    # display login again
        return render(request, 'users/login.html',  {'form':form})

This is my view page when i runserver it takes me to login page then the problem start when i enter my credintials and try to log in

Page not found (404) Request Method: POST Request URL: http://127.0.0.1:8000/users/login/index.html Using the URLconf defined in mazwefuneral.urls, Django tried these URL patterns, in this order:

admin/ product/ users/ login/ [name='login'] main/ accounts/

The current path, users/login/index.html, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

This is the error am getting

Ma0
  • 15,057
  • 4
  • 35
  • 65
  • 2
    Please post your `urls.py`: most likely `render(request, 'users/login.html', {'form':form})` needs to be changed to match the appropriate URL. – Kent Shikama Dec 06 '19 at 07:15
  • Welcome to Stack Overflow and congratulations on a very nice first question! – Ma0 Dec 06 '19 at 07:18

1 Answers1

1

Are you trying to authenticate users with the login function here? If so you need to change your view like this:

def login(request):
   form = UsersForm()
   # first check if the request is post 
   if request.method == 'POST':
      form = UsersForm(request.POST)
      if form.is_valid():
         # get username and password if the form is valid
         username = form.cleaned_data['username']
         password = form.cleaned_data['password']
        #now authenticate user
         user = authenticate(request,username=username, password=password)
         if user is not None:
           #redirect to your success url
           return redirect('/product/itemlist')
         else:
         # redirect to login page
          messages.error(request,"Invalid username or password")
          return redirect('login')

    return render(request,'users/login.html',{'form':form})
arjun
  • 7,230
  • 4
  • 12
  • 29
  • Thanks for your response but it giving me the following error File "C:\Users\NonsikeleloZ\Desktop\Nonsi_Project\venv\mazwefuneral\users\urls.py", line 2, in from . import views File "C:\Users\NonsikeleloZ\Desktop\Nonsi_Project\venv\mazwefuneral\users\views.py", line 28 return render(request,'users/login.html',{'form':form}) ^ IndentationError: unindent does not match any outer indentation level – Nonsikelelo Zuma Dec 06 '19 at 07:27
  • This is my url from django.urls import path from .import views app_name="users" urlpatterns = [ path('login/', views.login, name='login'), ] – Nonsikelelo Zuma Dec 06 '19 at 07:48