Home > front end >  getting error while fetching urls in templates- Django
getting error while fetching urls in templates- Django

Time:11-11

I'm trying to replicate an ecommerce website and show separate products when they click at different links of the category. But I'm having trouble with fetching the urls in the template.

Here's my views.py :

from django.shortcuts import render, redirect, reverse, get_object_or_404
from django.views import View
from .models import Product, Category

class ProductView(View):
    def get(self, request, *args, **kwargs):

        products = Product.objects.filter(is_active = True)


        context = {
            'products': products,
        }

        return render(request, 'Product/products.html', context)

class ProductDetailView(View):
    def get(self, request, slug):
        singleproduct = Product.objects.get(slug = slug)
        context = {
            'singleproduct': singleproduct,
        }
        return render(request, 'Product/productdetail.html', context)

class eBookView(View):
    def get(self, request, catslug):
        ebooks = Product.objects.filter(category__category = 'eBooks')

        context = {
            'ebooks': ebooks
        }

        return render(request, 'Product/ebooks.html', context)

class IllustrationView(View):
    def get(self, request, catslug):
        illustration = Product.objects.filter(category__category = 'Illustration')

        context = {
            'illustration': illustration
        }

        return render(request, 'Product/downloadillustration.html', context)

class PhotosView(View):
    def get(self, request, catslug):
        photos = Product.objects.filter(category__category = 'Photos')

        context = {
            'photos': photos
        }

        return render(request, 'Product/photos.html', context)

class WebsiteTemplatesView(View):
    def get(self, request, catslug):
        website = Product.objects.filter(category__category = 'Website Templates')

        context = {
            'website': website
        }

        return render(request, 'Product/websitetemplates.html', context)

Here's my urls.py :

from django.urls import path
from .views import ProductView, ProductDetailView, eBookView, IllustrationView, PhotosView, 
   WebsiteTemplatesView

urlpatterns = [
    path('', ProductView.as_view(), name = 'products'),
    path('<slug:slug>/', ProductDetailView.as_view(), name = 'product-detail'),
    path('ebook/<slug:catslug>/', eBookView.as_view(), name = 'ebooks'),
    path('illustration/<slug:catslug>/', IllustrationView.as_view(), name = 'illustration'),
    path('photos/<slug:catslug>/', PhotosView.as_view(), name = 'photos'),
    path('website-templates/<slug:catslug>', WebsiteTemplatesView.as_view(), name = 'website- 

templates'), ]

And here's the template :

{% extends 'Base/base.html %}
{% block content %}
<a href="{% url 'ebook' catslug=instance.catslug %}</a>
{% endblock %}

I want to add the urls of the categories in the navigation bar but it shows an error while adding the urls of the categories in the navigation bar.

CodePudding user response:

Correct the template like this :

{% extends 'Base/base.html %}
{% block content %}
    <a href="{% url 'ebooks' instance.catslug %}>Category</a>
{% endblock %}

NB : When you use {% url %} tag, specify the name of the path you defined in the urls.py file and you also don't have to specify the name of the url parameter.

CodePudding user response:

In your path you have given name=ebooks so you can use that name in your templates.

You have written ebook, which should be ebooks. Also your a tag is not formatted properly.

{% extends 'Base/base.html %}
{% block content %}
    <a href="{% url 'ebooks' instance.catslug %}>Category</a>
{% endblock %}
  • Related