Reputation: 318
I have different django models which are identical e.g
All of these models carry same attributes.
I want to use single html page which is category_list.html for displaying these models as list. I don't want to use different pages like item_category_list.html, inventroy_category_list.html, menu_category_list.html and so on. Every page contains page header which is actual kind of title of page represented in h1 tag. I want to change with respect to currently loaded page.
Note: I am using Generic Views for Listing Items and I am currently using context for recognizing which view is sending response.
Any help would be appreciated.
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)
Upvotes: 0
Views: 112
Reputation: 652
Not class based but my solution for this simply would be :
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)
Upvotes: 2