urls.py
from django.urls import path
from . import views
app_name='shop'
urlpatterns = [
path('',views.allproduct,name='allproductcat'),
path('<slug:c_slug>/',views.allproduct,name='product_by_catagory')
]
views.py
from django.shortcuts import render, get_object_or_404
from . models import catagory,product
# Create your views here.
def allproduct(request,c_slug=None):
c_page=None
products=None
if c_slug!=None:
c_page=get_object_or_404(catagory,slug=c_slug)
products=product.objects.all().filter(catagory=c_page,available=True)
else:
products=product.objects.all().filter(available=True)
return render(request,'catagory.html',{'catagory':c_page,'products':products})
catagory.html
{% extends 'base.html' %}
{% load static %}
{% block metadiscription %}
{% if catagory %}
{{catagory.discription}}
{% else %}
welcome
{% endif %}
{% endblock %}
{% block title %}
{% if catagory %}
{{catagory.name}}--ABC store
{% else %}
see our new collection
{% endif %}
{% endblock %}
{% block content %}
{% if catagory %}
<div>
<div>
<a href="{% url 'shop:allproduct' %}">OUR PRODUCT COLLECTION</a>
</div>
</div>
{% endif %}
<div>
{% if catagory %}
<img src="{{catagory.img.url}}" alt="{{catagory.name}}">
</div>
<br>
<div>
<h1>
{{catagory.name}}
</h1>
<p>
{{catagory.discription}}
</p>
</div>
{% else %}
<div>
<img src="{% static 'img/banner.png' %}">
</div>
<br>
<div>
<h1>OUR PRODUCT COLLECTION</h1>
<p>INDIA IS MY COUNTRY I LOVE MY COUNTRY</p>
</div>
{% endif %}
<div>
<div>
{% for product in products %}
<div>
<div>
<a href="" ><img src="{{product.img.url}}"></a>
<div>
<h4>{{product.name}}</h4>
<p>{{product.price}}</p>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
navbar.html
<nav>
<ul>
<li><a href="{% url 'shop:allproductcat' %}">ALL PRODUCTS</a></li>
{% for cat in links %}
<li><a href="{{cat.get_url}}"></a></li>
{% endfor %}
<li>YOUR CART</li>
</ul>
</nav>
context_processors.py
from . models import catagory
def menu_link(request):
link=catagory.objects.all()
return dict(links=link)
models.py
from django.db import models
# Create your models here.
from django.urls import reverse
class catagory(models.Model):
name=models.CharField(max_length=250,unique=True)
slug=models.SlugField(max_length=250,unique=True)
description=models.TextField(blank=True)
image=models.ImageField(upload_to='catagory',blank=True)
class Meta:
ordering=('name',)
verbose_name='catagory'
verbose_name_plural='catagories'
def __str__(self):
return '{}'.format(self.name)
def get_url(self):
return reverse('shop:product_by_catagory',args=self.slug)
我收到一个错误,没有找到反向的slug,我无法打开我的网站,请解决我的问题。没有找到反向的'product_by_catagory',参数'('d','r','e','s ','s')'。尝试了1个模式:['(?P<c_slug>[-a-zA-Z 0 -9_]+)/$'].我无法运行我的代码,请帮助我解决此问题
2条答案
按热度按时间flvlnr441#
你可以将args中的项目作为对象的可迭代对象传递,即使只有一个参数。因此,你不应该将其实现为:
而是:
因此,我们在这里将项目 Package 在一个单例元组中,使得它只包含 * 一个 * 项目:
Category
对象的slug
。注意:通常Django模型会实现一个**
.get_absolute_url()
方法[Django-doc]来确定该模型的规范URL,而不是get_url
。使用get_absolute_url
的好处是,你可以将此传递给redirect(…)
**函数[Django-doc],然后它会自动返回一个HTTP重定向响应,其中包含使用.get_absolute_url()
方法生成的URL。h6my8fg22#
return reverse('eCommerceApp:products_by_category',args=[self.slug])
change args=[self.slug])放在方括号中