如何将单个 Django 模板用于多种用途
How to use single Django Template for multiple purpose
我有不同的 django 模型,它们是相同的,例如
- 物品类别
- 库存类别
- 菜单类别
- 费用类别
所有这些模型都具有相同的属性。
我想使用单个 html 页 即 category_list.html 将这些模型显示为列表。我不想使用不同的页面,例如 item_category_list.html、inventroy_category_list.html、menu_ category_list.html 等等。每个页面都包含 page header 这是 h1 标签中表示的页面标题的实际种类。我想更改当前加载的页面。
注意:我正在使用通用视图列出项目,我目前正在使用上下文来识别哪个视图正在发送响应。
如有任何帮助,我们将不胜感激。
views.py
class ItemCategoryList(ListView):
model = ItemCategory
template_name = 'app/category_list.html'
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['page'] = 'Item'
return context
class InventoryCategoryList(ListView):
model = InventoryCategory
template_name = 'app/category_list.html'
class ExpenseCategoryList(ListView):
model = ExpenseCategory
template_name = 'app/category_list.html'
models.py
class ItemCategory(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=300, null=True, blank=True)
image = models.ImageField(upload_to="uploads/item_categories/", null=True, blank=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
class InventoryCategory(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=300, null=True, blank=True)
image = models.ImageField(upload_to="uploads/inventory_categories/", null=True, blank=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
class ExpenseCategory(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=300, null=True, blank=True)
image = models.ImageField(upload_to="uploads/expense_categories/", null=True, blank=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
不是基于 class 但我对此的解决方案是:
models.py:
class Category(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=300, null=True, blank=True)
image = models.ImageField(upload_to="uploads/categories/", null=True, blank=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
urls.py:
url(r'^(?P<category>\w+)/$', views.ItemCategoryList, name='category')
home.html:
<a href="{% url 'category' 'ItemCategory' %}">Item Category</a>
<a href="{% url 'category' 'InventoryCategory' %}">Inventory Category</a>
category_list.html:
{% block title %}{{category.name}}{% endblock %}
{% for l in list %}
{{l}}
{% endfor%}
views.py:
def ItemCategoryList(request, category):
list = Category.objects.filter(name=category)
category = category
context{
'list':list,
'category':category
}
return render(request, 'app/category_list.html',context)
我有不同的 django 模型,它们是相同的,例如
- 物品类别
- 库存类别
- 菜单类别
- 费用类别
所有这些模型都具有相同的属性。
我想使用单个 html 页 即 category_list.html 将这些模型显示为列表。我不想使用不同的页面,例如 item_category_list.html、inventroy_category_list.html、menu_ category_list.html 等等。每个页面都包含 page header 这是 h1 标签中表示的页面标题的实际种类。我想更改当前加载的页面。
注意:我正在使用通用视图列出项目,我目前正在使用上下文来识别哪个视图正在发送响应。
如有任何帮助,我们将不胜感激。
views.py
class ItemCategoryList(ListView):
model = ItemCategory
template_name = 'app/category_list.html'
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['page'] = 'Item'
return context
class InventoryCategoryList(ListView):
model = InventoryCategory
template_name = 'app/category_list.html'
class ExpenseCategoryList(ListView):
model = ExpenseCategory
template_name = 'app/category_list.html'
models.py
class ItemCategory(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=300, null=True, blank=True)
image = models.ImageField(upload_to="uploads/item_categories/", null=True, blank=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
class InventoryCategory(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=300, null=True, blank=True)
image = models.ImageField(upload_to="uploads/inventory_categories/", null=True, blank=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
class ExpenseCategory(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=300, null=True, blank=True)
image = models.ImageField(upload_to="uploads/expense_categories/", null=True, blank=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
不是基于 class 但我对此的解决方案是:
models.py:
class Category(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=300, null=True, blank=True)
image = models.ImageField(upload_to="uploads/categories/", null=True, blank=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
urls.py:
url(r'^(?P<category>\w+)/$', views.ItemCategoryList, name='category')
home.html:
<a href="{% url 'category' 'ItemCategory' %}">Item Category</a>
<a href="{% url 'category' 'InventoryCategory' %}">Inventory Category</a>
category_list.html:
{% block title %}{{category.name}}{% endblock %}
{% for l in list %}
{{l}}
{% endfor%}
views.py:
def ItemCategoryList(request, category):
list = Category.objects.filter(name=category)
category = category
context{
'list':list,
'category':category
}
return render(request, 'app/category_list.html',context)