django/base/views.py

46 lines
1.4 KiB
Python
Raw Normal View History

2023-04-23 16:08:01 +00:00
from django.shortcuts import render, redirect
from django.contrib import messages
from users.models import User
from .models import VendorsData
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
# Create your views here.
@login_required(login_url='login')
def index(request):
context = {}
return render(request, 'base/dashboard.html', context)
def LoginPage(request):
if request.method == 'POST':
email = request.POST.get('email')
password = request.POST.get('password')
user = None
error = None
try:
user = User.objects.get(email=email)
except:
error = messages.error(request, 'Email not registered')
if user is not None and error is None:
user = authenticate(request, email=email, password=password)
if user is not None:
login(request, user)
return redirect('dashboard')
else:
messages.error(request, 'Wrong password')
context = {}
return render(request, 'base/login.html', context)
def LogoutPage(request):
logout(request)
return redirect('login')
@login_required(login_url='login')
def VendorPage(request):
vendors = VendorsData.objects.filter(vendorid_id=request.user.id)
context = {'vendors': vendors}
return render(request, 'base/vendor.html', context)