I'm building an ecommerce app and I'd like to make a section that shows some featured items by category. I'm using three sliders that display those items; Each slider is a featured category and each item in the slider is a featured item.
The problem is that I can't figure out how to assign the item to the proper slider. For example: I want to assign a JeansJacket to "Clothes and accesories" and display it. I tried this:
{% for cat in categories %}
<h1>{{ cat.cat_name }}</h1>
<!--(carousel code in between)-->
<div class="carousel-inner" role="listbox">
{% for item in featured_items %}
{% if item.Categoría in cat.cat_name %}
{{ item }}
{% endif %}
This is a simplified version of what I have, without the rest of the content. I just can't figure out how to iterate though the featured items and display them in the corresponding category.
Edit: This is in models.py:
class Categorías(models.Model):
cat_name = models.CharField(max_length=30)
Destacado = models.BooleanField()
class Meta:
ordering = ('cat_name',)
verbose_name = 'Categoría'
verbose_name_plural = 'Categorías'
def __str__(self):
return self.cat_name
class publicaciones(models.Model):
Título = models.CharField(max_length=30)
Descripción = models.TextField(max_length=200)
Precio = models.FloatField()
Fotos = models.ImageField()
Categoría = models.ForeignKey(Categorías, on_delete=models.CASCADE)
Promocionado = models.BooleanField()
class Meta:
verbose_name = 'Publicación'
verbose_name_plural = 'Publicaciones'
def __str__(self):
return self.Título
You can use prefetch_related and Prefetch with a custom query to get all related articles for each category.
categories = Categorías.objects.prefetch_related(Prefetch(
'publicaciones_set',
queryset=publicaciones.objects.filter(Promocionado=True),
to_attr='featured_items'
))
Now you can loop over each category and then loop over this prefetch. You don't need to create a separate featured_items queryset
for category in categories:
for featured_item in category.featured_items:
...
You can use apply this pattern to your template
Related
I have some models and connected each other. And in my views I get the Chapter model ordered by date published, and then remove the ones with same Manga (foreign key associated with other model).
But apparently accept for the fields is not connected with anything, and I can't access the models fields. But also because I am working with a query set I can't sort by id or anything. Also, I have to access the Manga models fields associated with chapter. But it does not show up in the HTML. How can I do it?
My models:
class Manga(models.Model):
manga_name = models.CharField(max_length=255, default="null")
manga_image = models.ImageField(upload_to="thumbnail", default="thumbnail/noimage.png")
manga_views = models.IntegerField(default=0)
def __str__(self):
return f"{self.id}, {self.manga_name}"
class Fansub(models.Model):
fansub_name = models.CharField(max_length=255, default="null")
def __str__(self):
return f"{self.id}, {self.fansub_name}"
class Chapter(models.Model):
chapter_number = models.IntegerField(default=0)
chapter_url = models.URLField(default="www.example.com")
manga = models.ForeignKey(Manga, on_delete=models.CASCADE)
fansub = models.ForeignKey(Fansub, on_delete=models.CASCADE)
relase_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"{self.chapter_number}, {self.manga}, {self.fansub}"
My view:
def Index(request):
manga = Manga.objects.all()
chapter = Chapter.objects.all().values()
fansub = Fansub.objects.all().values()
mostview = manga.order_by("-manga_views")[:5]
relasedateorder = chapter.order_by("relase_date")
context = {
"Manga": manga,
"Chapter": chapter,
"Fansub": fansub,
"Mostview": mostview,
"LastUpdated": relasedateorder,
}
template = loader.get_template("Index.html")
And finally the HTML:
{%for d in LastUpdated%}
<p>{{d.manga}}</p>
{%endfor%}
You can access the all the instances of Manga model associated with chapter instance by using _set as suffix in the following way:
{% for i in LastUpdated %}
<p>{{i.chapter_number}}</p>
<h2> manga model attributes below</h2>
{% for j in i.manga_set.all %}
{{j.manga_name}}
{{j.manga_views}}
{% endfor %}
{% endfor %}
Also you should use Chapter.objects.all().order_by("relase_date") this queryset as using .values() gives you a key-value pair (dict).
I have a following models.py for my Django blog, I made a following views.py to pass the value of the slug for my URL parameter.
However I am struggling to create a model in views to get other data(person & description) from Category class.
I have tried some patterns by myself but can not pass them to HTML. (always Error or not showing)
Can you please give me some idea of how to solve this.
models.py
class Category(models.Model):
person = models.CharField(max_length=20)
description = models.TextField()
slug = models.SlugField()
def __str__(self):
return self.person
views.py
def blog_category(request, category):
posts = Post.objects.filter(categories__slug__contains=category).order_by("-created_on").distinct()
context = {"category": category, "posts": posts}
return render(request, "blog_category.html", context)
HTML(Localhost:8000/slug)
{{ person }}
{{ description }}
this is full code of my models.py
class Category(models.Model):
person = models.CharField(max_length=20)
description = models.TextField()
slug = models.SlugField()
def __str__(self):
return self.person
class Recommender(models.Model):
recommender_name = models.CharField(max_length=20)
slug = models.SlugField()
def __str__(self):
return self.recommender_name
class Post(models.Model):
book_title = models.CharField(max_length=255)
author = models.CharField(max_length=255)
book_link = models.CharField(max_length=255)
recommenders = models.ForeignKey("Recommender", on_delete=models.CASCADE,)
source = models.TextField()
source_link = models.CharField(max_length=255)
created_on = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True)
categories = models.ManyToManyField("Category", related_name="posts")
slug = models.SlugField()
def __str__(self):
return self.book_title
posts = Post.objects.filter(categories__slug__contains=category).order_by("-created_on").distinct()
Is going to return a queryset. It can have more than one instance of the model class (since you are using filter). In your context you are sending this queryset as posts to your templates.
So in your HTML you can use something like this. You need to use a for loop since there can be more than one item in posts.
{% for post in posts %}
{% for category in post.categories.all %}
{{ category.person }}
{{ category.description }}
{% endfor %}
{% endfor %}
I would look at this example.
Namely, if you render the template like it is shown in the example, you should be able to do
{{ category.person }} {{ category.description }}
i would like to make a structure database for a restaurant menu without using mptt or django-tree. here is my models.py:
from django.db import models
class Menu(models.Model):
name = models.CharField(max_length=24, unique=True, verbose_name='menu name')
#slug = models.SlugField(max_length=24, unique=True, help_text='The slug is the URL friendly version of the menu name, so that this can be accessed at a URL like mysite.com/menus/dinner/.')
additional_text = models.CharField(max_length=128, null=True, blank=True, help_text='Any additional text that the menu might need, i.e. Served between 11:00am and 4:00pm.')
order = models.PositiveSmallIntegerField(default=0, help_text='The order of the menu determines where this menu appears alongside other menus.')
class Meta:
ordering = ['name', 'order']
class MenuCategory(models.Model):
menu = models.ForeignKey(Menu, on_delete=models.CASCADE,help_text='The menus that this category belongs to, i.e. \'Lunch\'.')
name = models.CharField(max_length=32, verbose_name='menu category name')
additional_text = models.CharField(max_length=128, null=True, blank=True, help_text='The additional text is any bit of related information to go along with a menu category, i.e. the \'Pasta\' category might have details that say \'All entrees come with salad and bread\'.')
order = models.IntegerField(default=0, help_text='The order is the order that this category should appear in when rendered on the templates.')
class Meta:
verbose_name='menu category'
verbose_name_plural='menu categories'
ordering = ['order', 'name']
def __unicode__(self):
return self.name
class MenuItem(models.Model):
CLASSIFICATION_CHOICES = (
('neither', 'Neither'),
('vegan', 'Vegan'),
('vegetarian', 'Vegetarian'),
)
name = models.CharField(max_length=48, help_text='Name of the item on the menu.')
description = models.CharField(max_length=128, null=True, blank=True, help_text='The description is a simple text description of the menu item.')
category = models.ManyToManyField(MenuCategory, verbose_name='menu category', help_text='Category is the menu category that this menu item belongs to, i.e. \'Appetizers\'.')
order = models.IntegerField(default=0, verbose_name='order', help_text='The order is to specify the order in which items show up on the menu.')
price = models.IntegerField(help_text='The price is the cost of the item.')
image = models.ImageField(upload_to='menu', null=True, blank=True, verbose_name='image', help_text='The image is an optional field that is associated with each menu item.')
class Meta:
verbose_name='menu item'
verbose_name_plural='menu items'
ordering = ['classification', 'order', 'name']
def __unicode__(self):
return self.name
views.py:
from .models import Menu,MenuCategory
from django.views.generic import ListView
class MenuView(ListView):
model= Menu
conext_object_name='name'
template_name = 'menu_list.html'
queryset = Menu.objects.all()
def get_context_data(self, **kwargs):
context = super(MenuView, self).get_context_data(**kwargs)
context['Menucategory'] = MenuCategory.objects.all()
context['Menus'] = self.queryset
return context
menu_list.html:
<div>
{%for item in Menu%}
<p>
{{item.menu}}
{{item.name}}
{{item.additiona_text}}
{{item.order}}
</p>
{%endfor%}
{%for item in MenuCategory%}
<p>
{{item.name}}
{{item.additiona_text}}
{{item.order}}
</p>
{%endfor%}
</div>
when i browse localhost page after executing runserver, it shows only
Django blog
Breakfast 1
Lunch 2
Dinner 3
but my desired output should be:
1)Breakfast
coffee
Items
1 2 3
Snacks Items 1 2 3
2)Lunch
Starter items 1 2 3
Main Courses Items 1 2 3
how can i get this structured table using plain bootstrap table row column where in every row column i will query the items with for loop? is their any other way? If there is alternative way, i am also interested....
I think your design is not good in the first place. If you want to display things in such a way, the logic would be the following:
you have menu categories (breakfast, lunch, dinner...),
you have menu items (coffee...) which belong to one or many menu categories,
you have menus which belong to one category
So, your data structure should be the following:
class MenuItem(models.Model):
# [...]
class MenuCategory(models.Model):
# [...]
name = CharField...
items = models.ManyToManyField(MenuItem)
class Menu(models.Model):
# [...]
menu_category = models.ForeignKey(MenuCategory)
Then, you could do what you want in your template (note: to name a queryset by its class name is a very bad practice. Just call it menus! That is what I'll do here) with a double for loop:
<div>
{% for menu in menus %}
<h3>
{{ menu.menu_category.name }}
</h3>
<p>
{%for item in menu.menu_category.items.all %}
{{ item.name }}
{% endfor %}
</p>
{% endfor %}
</div>
Once again, your initial design is not the most appropriate in my opinion.
Hope that helps!
I'm new to Django and I'm trying to learn as I go. And I've ended up in a situation where I can't figure out what is the best way forward.
snippet from models.py:
class ProjectMeta(models.Model):
project = models.ForeignKey(Project)
architect = models.CharField(max_length=200)
landscape = models.CharField(max_length=100, blank=True)
engineer = models.CharField(max_length=200, blank=True)
client = models.CharField(max_length=100)
consultant = models.CharField(max_length=100, blank=True)
size = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
location = models.CharField(max_length=200)
date = models.DateField()
STATUS = (
('CP', 'Competition'),
('UC', 'Under construction'),
('CO', 'Completed'),
)
status = models.CharField(max_length=2, choices=STATUS, default=1)
And this is the view:
class ProjectDetailView(DetailView):
model = Project
def get_context_data(self, **kwargs):
context = super(ProjectDetailView, self).get_context_data(**kwargs)
context['projectmeta_list'] = ProjectMeta.objects.all()
return context
But if I want to output ProjectMeta in the template I could iterate over projectmeta_list.
{% for metadata in projectmeta_list %}
<p>Architect: {{ metadata.architect }}</p>
{% endfor %}
But this require alot of repeating myself, and I wont work. Because lets say the architect field is empty, I would get Archiect: printed to the page. Is there a built-in way of converting a model into a dict or list, so I can iterate over it and only print out fields that aren't empty to the page?
I've been looking at get_fields(), would that work? https://docs.djangoproject.com/en/1.10/ref/models/meta/#retrieving-all-field-instances-of-a-model
I tried this in the shell, threw me and AttributeError:
>>> from projects.models import *
>>> Project._projectmeta.get_fields()
You should try wrapping the <p>Architect: {{ metadata.architect }}</p> piece in a conditional {% if metadata.architect != '' %} or some condition to that effect.
Try with another ProjectMeta model. Take a look at this one.
class ProjectMeta(models.Model):
project = models.ForeignKey(Project)
name = models.CharField(max_length=50)
value = models.TextField()
And this query should work. myproject.projectmeta_set.filter(name="status")
You can use built-in default or default_if_none template filters to show a default value if it is None or empty string.
{% for metadata in projectmeta_list %}
<p>Architect: {{ metadata.architect|default:"-" }}</p>
{% endfor %}
Check this for more details.
I'm trying to sort related items in a template by a field in a model three ForeignKey relationships away. I'm assembling the data for the template in the view as proposed in another StackOverflow answer:
Sorting related items in a Django template
As far as I can tell, I copied the code from this as-is except for I had to change variable names. It doesn't throw an error, it just displays no list items in the HTML unordered list.
# checkout.html
{% for item in cart_items %}
<tr>
<td class="left">
{{ item.name }}
<ul>
{% for part in part_list %}
<li>{{ part.name }}
{% endfor %}
</ul></td>
</tr>
{% endfor %}
And the view...
# views.py
def checkout(request):
cart_items = get_cart_items(request)
itemCollection = []
for item in cart_items:
item.part_list = item.product.buildpart.all().order_by('product__family__type')
itemCollection.append(item)
return render(request, 'checkout.html', locals())
And the get_cart_items function:
# cart.py
def get_cart_items(request):
""" return all items from the current user's cart """
return CartItem.objects.filter(cart_id=get_cart_id(request))
As I said, the template and view are pretty much copies of the solution presented in the aforementioned StackOverflow article. One thing I thought was curious was that itemCollection[] from the view is never referenced in the template.
I believe the order_by clause ('product__family__type') is right only because it doesn't generate an error. But in case that is the problem or a part of it here is the chain of models I am attempting to navigate in that order_by clause:
We start from the shopping cart model (CartItem):
class Item(models.Model):
cart_id = models.CharField(max_length=50)
quantity = models.IntegerField(default=1)
product = models.ForeignKey(PartModel, unique=False)
class Meta:
abstract = True
class CartItem(Item):
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['date_added']
verbose_name = "Cart Item"
Through the 'product' field we get to the model holding our inventory and its self-referential BuildPart ManyToMany model:
class PartModel(models.Model):
family = models.ForeignKey(PartFamily)
name = models.CharField("Model Name", max_length=50, unique=True)
buildpart = models.ManyToManyField('self', through='BuildPart',
symmetrical=False, related_name='+')
class Build(models.Model):
build = models.ForeignKey(PartModel, related_name='+')
part = models.ForeignKey(PartModel, related_name='+')
quantity = models.PositiveSmallIntegerField(default=1)
class Meta:
abstract = True
unique_together = ('build', 'part')
def __unicode__(self):
return self.build.name + ' with ' + str(self.quantity) + ' * ' + \
self.part.family.make.name + ' ' + self.part.name
class BuildPart(Build):
pass
class Meta:
verbose_name = "Build Part"
From there we follow the 'family' field to the PartFamily model:
class PartFamily(models.Model):
make = models.ForeignKey(PartMake)
type = models.ForeignKey(PartType)
name = models.CharField("Family Name", max_length=30,
unique=True)
slug = models.SlugField(unique=True)
And lastly, we get to the model with the 'order' field, the one we wish to sort the related items by, PartType:
class PartType(models.Model):
name = models.CharField("Part Type", max_length=30, unique=True)
slug = models.SlugField(unique=True)
order = models.PositiveSmallIntegerField()
description = models.TextField(blank=True, null=True)
To recap, how do I get the shopping cart products' related items, and sort them by the 'order' field in the PartType model?
You have two errors, both in the template.
Firstly, you've put your items with the sorted relationship in a list called itemCollection, but in the template you're iterating over cart_item instead. This is a very good example of why you should be explicit about what variables you pass to the template, rather than relying on locals().
Secondly, you then iterate over part_list without defining it. You mean item.part_list.