如何将单个Django模板用于多种用途

时间:2018-09-01 19:43:07

标签: django django-templates django-urls

我有不同的 django模型,例如

  • ItemCategory
  • 库存类别
  • MenuCategory
  • ExpenseCategory

所有这些模型都具有相同的属性。

我想使用单个 html页面(即 category_list.html )将这些模型显示为列表。我不想使用其他页面,例如 item_category_list.html inventroy_category_list.html menu_category_list.html 等。每个页面都包含页面标题,这是 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)

1 个答案:

答案 0 :(得分:2)

不是基于类的,但是我的解决方案将是:

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)