When logging in, the error below is received. Also, on any product category I give, nothing happens.
views.py
from django.shortcuts import get_object_or_404, render, redirect
from .models import Parfum, Order, OrderItem
from .cart import Cart
from django.http import JsonResponse
from django.views.decorators.csrf import ensure_csrf_cookie
from .forms import CreateUserForm, LoginForm
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
def store(request):
perfum_list = Parfum.objects.all()
context = {‘perfums’: perfum_list}
return render(request, ‘store/store.html’, context)
def perfum_by_category(request, category):
perfums = Parfum.objects.filter(category=category)
context = {‘perfums’: perfums}
return render(request, ‘store/store.html’, context)
def perfum_info(request, slug):
perfum = get_object_or_404(Parfum, slug=slug)
context = {‘perfum’: perfum}
return render(request, ‘store/perfum-info.html’, context)
def cart_summary(request):
cart = Cart(request)
total_prices =
for item in cart:
total_prices.append(item[‘perfum’].price * item[‘qty’])
total_price = sum(total_prices)
context = {‘cart’: cart, ‘total_price’: total_price}
return render(request, ‘store/cart-summary.html’, context)
@ensure_csrf_cookie
def cart_add(request, id):
cart = Cart(request)
if request.method == ‘POST’:
perfum_id = int(request.POST.get(‘perfum_id’))
perfum_quantity = int(request.POST.get(‘perfum_quantity’))
perfum = Parfum.objects.get(id=perfum_id)
cart.add(perfum=perfum, perfum_qty=perfum_quantity)
cart_quantity = cart.len()
response = JsonResponse({‘qty’: cart_quantity})
return response
@ensure_csrf_cookie
def cart_delete(request):
cart = Cart(request)
if request.method == ‘POST’:
perfum_id = int(request.POST.get(‘perfum_id’))
cart.delete(perfum=perfum_id)
cart_quantity = cart.len()
cart_total = cart.get_total()
response = JsonResponse({‘qty’: cart_quantity, ‘total’: cart_total})
return response
def cart_update(request):
cart = Cart(request)
if request.method == ‘POST’:
perfum_id = int(request.POST.get(‘perfum_id’))
perfum_quantity = int(request.POST.get(‘perfum_quantity’))
perfum = Parfum.objects.get(id=perfum_id)
cart.update(perfum=perfum_id, qty=perfum_quantity)
cart_quantity = cart.len()
cart_total = cart.get_total()
response = JsonResponse({‘qty’: cart_quantity, ‘total’: cart_total})
return response
def checkout(request):
return render(request, ‘store/checkout.html’)
def complete_order(request):
if request.method == ‘POST’:
name = request.POST.get(‘name’)
email = request.POST.get(‘email’)
address = request.POST.get(‘address’)
country = request.POST.get(‘country’)
city = request.POST.get(‘city’)
zipcode = request.POST.get(‘zipcode’)
phone = request.POST.get(‘phone’)
shipping_address = (f"{address}, {country}, {city}, {zipcode}")
cart = Cart(request)
total_cost = cart.get_total()
if request.user.is_authenticated:
order = Order.objects.create(full_name=name, email=email, amount_paid=total_cost,
shipping_address=shipping_address, phone=phone, user=request.user)
else:
order = Order.objects.create(full_name=name, email=email, amount_paid=total_cost,
shipping_address=shipping_address, phone=phone)
order_id = order.pk
for item in cart:
OrderItem.objects.create(order_id=order_id, perfum=item[‘perfum’], quantity=item[‘qty’],
price=item[‘price’], user=request.user if request.user.is_authenticated else None)
order_success = True
response = JsonResponse({‘success’: order_success})
return response
def order_success(request):
request.session.flush()
return render(request, ‘store/order-success.html’)
def register(request):
form = CreateUserForm()
if request.method == ‘POST’:
form = CreateUserForm(request.POST)
if form.is_valid():
user = form.save()
user.save()
return redirect(“my-login”)
context = {‘form’: form}
return render(request, ‘store/register.html’, context=context)
def my_login(request):
form = LoginForm()
if request.method == ‘POST’:
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get(‘username’)
password = form.cleaned_data.get(‘password’)
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect(“dashboard”)
context = {‘form’: form}
return render(request, ‘store/my-login.html’, context=context)
def user_logout(request):
logout(request)
return redirect(“store”)
@login_required(login_url=‘my-login’)
def dashboard(request):
return render(request, ‘store/dashboard.html’)
@login_required(login_url=‘my-login’)
def track_orders(request):
try:
orders = OrderItem.objects.filter(user=request.user)
context = {‘orders’: orders}
return render(request, ‘store/track-orders.html’, context=context)
except:
return render(request, ‘store/track-orders.html’)
by category.html
{% extends “./base.html” %}
{% load static %}
{% block content %}
<div class="pb-3 h5"> {{ perfum.category | capfirst}} </div>
<hr>
<br>
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-5 g-3">
{% for perfum in perfums %}
<div class="col">
<div class="card shadow-sm">
<img class="img-fluid" alt="Responsive image" src="{{book.image}}">
<div class="card-body">
<p class="card-text">
<a class="text-info text-decoration-none" href="{% url 'by-category' %}"> {{perfum.title}} </a>
</p>
<div class="d-flex justify-content-between align-items-center">
<h5> {{perfum.price}} RON </h5>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
login.html
{% endblock %}
{% extends “./base.html” %}
{% load crispy_forms_tags %}
{% load static %}
{% block content %}
<style>
body{
background-color: rgb(0, 0, 0);
background-image: url("https://images.pexels.com/photos/3747468/pexels-photo-3747468.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1");
background-size: cover;
}
</style>
<body>
<br>
<div class="container bg-white shadow-md p-5 form-layout" style="width:500px; border-radius:20px;">
<h3 style="color: black;"> Login to your account </h3>
<hr>
<br>
<form method="POST" autocomplete="off">
{% csrf_token %}
{{form.username|as_crispy_field}}
<br>
{{form.password|as_crispy_field}}
<br>
<button type="submit" class="btn btn-primary navbar-btn text-white w-100 p-2"> <i class="fa fa-key" aria-hidden="true"></i> Login </button>
<br> <br>
{{form.non_field_errors}}
<div class="text-center">
<a href="{% url 'register' %}"> Don't have an account? </a>
<br> <br>
</div>
</form>
</div>
</body>
<br> <br>
{% endblock %}