На моей веб-странице есть таблица, в которую я хочу добавить информацию о продукте, и она работала до тех пор, пока я не попытался добавить paginator. Сервер работает нормально, а остальная часть сайта работает.
Вот мои файлы:
shop / products.html
{% extends 'base.html' %}
{% block content %}
<h1>On sale here</h1>
<div class="col-sm-3">
<h4>Categories</h4>
<ul class="list-group">
<a href="{% url 'products' %}" class="list-group-item"> All Categories </a>
{% for c in countcat %}
<a href="{{ c.get_absolute_url }}" class="list-group-item catheight">{{c.name}}
<span class="badge">{{c.num_products}}</span>
</a>
{% endfor %}
</ul>
</div>
<div class="col-sm-9">
<h4>Note that all products are second hand, unless otherwise stated.</h4>
<form class="form-inline my-2 my-lg-0" action="{% url 'search_result' %}" method="get">
{% csrf_token %}
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" name="q">
<button class="glyphicon glyphicon-search h-100" type="submit"></button>
</form>
<table class="table table-bordered table-hover table-condensed">
<thead>
<!-- The header row-->
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
<th>Category</th>
<th>Description</th>
<th>Stock</th>
<th>Price</th>
<th>Buy</th>
</tr>
</thead>
<tbody>
<!-- Product row(s) -->
{% for product in products %}
<tr>
<td>{{product.id}}</td>
<td>{{product.name}}</td>
{% if product.image %}
<td><img src="{{product.image.url}}"></td>
{% endif %}
<td>{{product.category}}</td>
<td>{{product.description}}</td>
<td>{{product.stock}}</td>
<td>€{{product.price}}</td>
{% if product.stock > 0 %}
<td><a href="{% url 'add_cart' product.id %}" class="btn btn-default btn-xs"><span
class="glyphicon glyphicon-shopping-cart"></span></a></td>
{% else %}
<td><a href="" class="btn btn-default btn-xs"><span
class="glyphicon glyphicon-warning-sign red"></span></a></td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
<hr>
<div class="text-center">
{% for pg in products.paginator.page_range %}
<a href="?page={{pg}}" class="btn btn-light btn-sm
{% if products.number == pg %}
active
{% endif %}">{{pg}}</a>
{% endfor %}
</div>
</div>
{% endblock content %}
shop / models.py
from django.db import models
from django.urls import reverse
class Category(models.Model):
name = models.TextField()
products = models.ManyToManyField('Product')
def get_products(self):
return Product.objects.filter(category=self)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('product_list_by_category',args=[self.id])
class Product(models.Model):
name = models.TextField()
description = models.TextField()
stock = models.IntegerField()
price = models.DecimalField(max_digits=10,decimal_places=2)
image = models.ImageField(upload_to='images/', blank=True)
def __str__(self):
return self.name
shop / urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.product_list,
name='products'),
path('<int:category_id>/', views.product_list,
name='product_list_by_category'),
]
shop / views.py
from django.shortcuts import render, get_object_or_404, redirect
from .models import Category, Product
from django.db.models import Count
from django.contrib.auth.models import Group, User
from .forms import SignUpForm
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login, authenticate, logout
from django.core.paginator import Paginator, EmptyPage, InvalidPage
def product_list(request, category_id=None):
category = None
products = Product.objects.all()
ccat = Category.objects.annotate(num_products=Count('products'))
if category_id :
category = get_object_or_404(Category, id=category_id)
products = products.filter(category=category)
paginator = Paginator('products', 3)
try:
page = int(request.GET.get('page', 1))
except:
page = 1
try:
products = paginator.page(page)
except(EmptyPage, InvalidPage):
products = paginator.page(paginator.num_pages)
return render(request, 'products.html',
{'products': products,
'countcat':ccat})
def signupView(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get("username")
signup_user = User.objects.get(username=username)
customer_group = Group.objects.get(name= 'Customer')
customer_group.user_set.add(signup_user)
else:
form = SignUpForm()
return render(request, 'accounts/signup.html', {'form':form})
def signinView(request):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect('products')
else:
return redirect('signup')
else:
form = AuthenticationForm()
return render(request, 'accounts/signin.html', {'form':form})
def signoutView(request):
logout(request)
return redirect('signin')
Я своего рода нуб джанго, поэтому любая помощь будет очень полезнаоценил, спасибо:)