【问题标题】:getting error while fetching urls in templates- Django在模板中获取 url 时出错 - Django
【发布时间】:2021-12-23 20:23:52
【问题描述】:

我正在尝试复制一个电子商务网站,并在他们点击该类别的不同链接时显示单独的产品。但是我在获取模板中的 url 时遇到了麻烦。

这是我的 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)

这是我的 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- 

模板'), ]

这是模板:

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

我想在导航栏中添加类别的url,但是在导航栏中添加类别的url时显示错误。

【问题讨论】:

  • 你能告诉我们错误吗? :)

标签: django django-templates django-urls django-template-filters django-url-reverse


【解决方案1】:

像这样更正模板:

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

注意: 当您使用{% url %} 标签时,指定您在urls.py 文件中定义的路径的名称,您也不必指定url 参数的名称。

【讨论】:

    【解决方案2】:

    在您的路径中,您提供了name=ebooks,因此您可以在模板中使用该名称。

    你写了ebook,应该是ebooks。此外,您的 a 标签格式不正确。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 2016-01-20
      • 1970-01-01
      相关资源
      最近更新 更多