在FBV(Funtional-Base View)中反向获取url的方式是重载get_absolute_url
方法。
电子购物网站的模型:
from django.db import models
from django.urls import reverse
class Category(models.Model):
name = models.CharField(max_length=150, db_index=True)
slug = models.SlugField(max_length=150, unique=True ,db_index=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ('name', )
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_list_by_category', args=[self.slug])
class Product(models.Model):
category = models.ForeignKey(Category, related_name='products', on_delete=models.CASCADE)
name = models.CharField(max_length=100, db_index=True)
slug = models.SlugField(max_length=100, db_index=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
available = models.BooleanField(default=True)
stock = models.PositiveIntegerField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
class Meta:
ordering = ('name', )
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('shop:product_detail', args=[self.id, self.slug])
视图 views.py
from django.shortcuts import render, get_object_or_404
from .models import Category, Product
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = Product.objects.filter(category=category)
context = {
'category': category,
'categories': categories,
'products': products
}
return render(request, 'shop/product/list.html', context)
def product_detail(request, id, slug):
product = get_object_or_404(Product, id=id, slug=slug, available=True)
context = {
'product': product
}
return render(request, 'shop/product/detail.html', context)
路由urls.py
from django.urls import path, re_path
from . import views
app_name = "shop"
urlpatterns = [
path("", views.product_list, name="product_list"),
re_path(r"^(?P<category_slug>[\w-]+)/$", views.product_list,
name="product_list_by_category"),
re_path(r"^(?P<id>\d+)/(?P<slug>[\w-]+)/$", views.product_detail,
name="product_detail"),
]
这是在html页面的逻辑:
list.html
{% for product in products %}
<div class="col-md-4">
<div class="thumbnail">
<a href="{{ product.get_absolute_url }}">
<img src="{% if product.image %} {{ product.image.url }} {% else %} {% static 'img/default.jpg' %} {% endif %}" alt="..." style="height: 130px; width: auto">
</a>
<div class="caption">
<h3 class="text-center">
<a href="{{ product.get_absolute_url }}">{{ product.name }}</a>
</h3>
<p class="text-center">Kshs. {{ product.price }}</p>
</div>
</div>
</div>
{% endfor %}
页面逻辑:
在product.get_obsolute_url
里面用的是product.id
和product.slug
来连接到该product的detail.html
。
网友评论