Django Display multipe categories on page - django

I have this model.py file:
class Category(models.Model):
title = models.CharField(max_length=255, verbose_name="Title")
class Meta:
verbose_name_plural = "Categories"
def __str__(self):
return self.title
class Video(models.Model):
title = models.CharField(max_length=128)
description = models.TextField(default='')
thumbnail = models.ImageField(default='default_thumbnail.png', upload_to='thumbnails')
date_posted = models.DateTimeField(default=timezone.now)
category = models.ForeignKey(
Category,
on_delete=models.SET_DEFAULT,
#null=True,
default=1,
verbose_name="Category"
)
def __str__(self):
return self.title
Each video has its own category and I want to display on one page a given number of films in each category.
Currently, I'm only showing the last 5 videos on the page:
def home(request):
latest_videos = Video.objects.all().order_by('-date_posted')[:5]
categories = Category.objects.all()
context = {
'latest_videos': latest_videos,
#'categories': categories,
}
return render(request, 'videos/home.html', context)
I have no idea how to display a given number of movies in each category. I tried to send all the videos and query them on the template, but this is probably impossible.
Any ideas? I'm still learning Django, but this time I found a problem I can't solve all day.
Thanks

I don't know how many categories do you need to show in your template but one way to do this is like:
def home(request):
ready_data = []
all_categories = Category.objects.prefetch_related('video_set').all() # Note that video_set is your default related query name
for _c in all_categories:
related_videos = _c.video_set.all().order_by('-date_posted')[:5]
ready_data.append({"category_id": _c.id, "category_title": _c.title, "related_videos": related_videos})
context = {
'data': ready_data
}
return render(request, 'videos/home.html', context)
And you should also change this part of your template to something like:
{% for d in data %}
<p> category: {{ d.category_id }}, {{ d.category_title }} </p>
{% for v in d.related_videos %}
<h3> Videos </h3>
<p> {{ v.id }}- {{v.title}} </p>
{% endfor %}
{% endfor %}
This way you can show each category and 5 last of it's related videos.

Related

django eccomerce prodcut name not showing in file

i am creating a new django eccomorce website now in product detail page here is my code
the problem is i cant see product name correct in html page problem with first()
when i use first then only product name showing but all products have same name i have 8 producs in my page eight product name same to first just like overwriting also i cant use for loop with first()
i will add some pics
urls.py
path('collection/<str:cate_slug>/<str:prod_slug>',views.product_view,name="productview"),
views.py
def product_view(request,cate_slug,prod_slug):
if (Category.objects.filter(slug=cate_slug, status=0)):
if (Products.objects.filter(slug=prod_slug, status=0)):
products = Products.objects.filter(slug=prod_slug, status=0).first()
context = {'products':products}
else:
messages.error(request,"no such product found")
return redirect("collection")
else:
messages.error(request,"no such category found")
return redirect("collection")
return render(request,"product_view.html",context)
models.py
class Products(models.Model):
category = models.ForeignKey(Category,on_delete=models.CASCADE)
slug = models.CharField(max_length=150, null=False, blank=False)
product_name = models.CharField(max_length=150, null=False, blank=False)
product_image = models.ImageField( upload_to=get_image,null=True,blank=True)
description = models.TextField(max_length=500,null=False,blank=False)
original_price = models.IntegerField(null=False,blank=False)
selling_price = models.IntegerField(null=False,blank=False)
status = models.BooleanField(default=False,help_text="0=default , 1=Hidden")
trending = models.BooleanField(default=False,help_text="0=default , 1=Trending")
meta_title = models.CharField(max_length=150,null=False,blank=False)
meta_keyword = models.CharField(max_length=150,null=False,blank=False)
meta_description = models.CharField(max_length=400,null=False,blank=False)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.product_name
productview.html
{% block content %}
<h1>{{ products.product_name }} </h1>
{% endblock %}
i just want correct product name for every category i stucked here in morning helping are appreciated thank you all for helping till now
I think you need to loop through your produckts:
{% for product in products %}
<h1>{{ product.product_name }}</h1>
{% endfor %}
view:
def product_view(request,cate_slug,prod_slug):
if (Category.objects.filter(slug=cate_slug, status=0).exists()):
if (Products.objects.filter(slug=prod_slug, status=0).exists()):
products = Products.objects.filter(slug=prod_slug, status=0)
context = {'products':products}
else:
messages.error(request,"no such product found")
return redirect("collection")
else:
messages.error(request,"no such category found")
return redirect("collection")
return render(request,"product_view.html",context)
Note that usually the model name is singular, so it should be class Products(models.Model):

How to get a single model object with a relation to the template in Django

I am working on this project that I can add two or more forms in a template. I am able to get the two forms in the template but when I submit the form, I get the objects for the rentalproperty model and not the contract model. I have created two different solution but the two doesn't solve the problem.
The first solution below display both objects multiple times in the detailview but what I want is to display the two model objects just once. The second solution display the rentalproperty object once but the contract objects multiple times. Could someone point me in the right direction? Thanks.
First solution:
views.py
class DetailView(generic.DetailView):
model = RentalProperty
template_name = 'rental/detail.html'
context_object_name = 'property'
def new_rental(request, pk):
if request.method == 'POST':
rental_form = NewRentalPropertyForm(request.POST, request.FILES, prefix = "rentals")
contract_form = NewContractForm(request.POST, prefix = "contracts")
if rental_form.is_valid() and contract_form.is_valid():
print ("all validation passed")
rentalproperty = rental_form.save()
contract_form.cleaned_data["rentalproperty"] = rentalproperty
print(contract_form)
contract = contract_form.save(commit=False)
contract.rentalproperty = rentalproperty
contract = contract_form.save()
return HttpResponseRedirect(reverse("home"))
else:
messages.error(request, "Error")
contract = Contract.objects.get(pk=pk)
else:
rental_form = NewRentalPropertyForm(prefix = "rentals")
contract_form = NewContractForm(prefix = "contracts")
contract = Contract.objects.get(pk=pk)
return render(request, 'rental/new_rental.html', {
#'rentalproperty': rentalproperty,
'rental_form': rental_form,
'contract_form': contract_form,
'contract': contract,
})
detail.html
<h1>This is the detail view</h1>
<h3>From landlord</h3>
<p>Landlord: {{property.created_by}}</p>
<p>address: {{property.landlord.address}}</p>
<h3>From Rental property</h3>
<ul>
{% for rental in property.landlord.rentalpropertys.all %}
<br>
<li>Title: {{property.title}}</li>
<img src="{{property.image.url}}" height="200" alt=""/>
<li>created at: {{property.created_at}}</li>
<li>Type of property: {{property.type_of_property_listing}}</li>
<li>Street: {{property.street}}</li>
<li>Borough: {{property.borough}}</li>
<ul>
{% for contract in rental.contracts.all %}
<li> Insurance required: {{contract.insurance_required}}</li>
<li> other terms: {{contract.other_terms}}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
Second solution:
views.py
class DetailView(generic.DetailView):
model = RentalProperty
template_name = 'rental/detail.html'
context_object_name = 'property'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['landlord']= Landlord.objects.all()
context['contract']= Contract.objects.filter(rentalproperty__title=title).order_by('created_at')
return context
The post function is the same.
detail.html
<h1>This is the detail view</h1>
<h3>From landlord</h3>
<p>Landlord: {{property.created_by}}</p>
<p>address: {{property.landlord.address}}</p>
<h3>From Rental property</h3>
<ul>
<li>Title: {{property.title}}</li>
<img src="{{property.image.url}}" height="200" alt=""/>
<li>created at: {{property.created_at}}</li>
<li>Type of property: {{property.type_of_property_listing}}</li>
<li>Street: {{property.street}}</li>
<li>Borough: {{property.borough}}</li>
</ul>
<ul>
{% for data in contract %}
<li> insurance : {{data.insurance_required}}</li>
<li> other terms: {{data.other_terms}}</li>
{% endfor %}
</ul>
My model:
class Landlord(models.Model):
user = models.OneToOneField(UserModel, on_delete=models.CASCADE)
address = models.CharField(max_length=255)
def __str__(self):
return str(self.address)
class RentalProperty(models.Model):
landlord = models.ForeignKey("Landlord", related_name='rentalpropertys', on_delete=models.CASCADE)
created_by = models.ForeignKey(UserModel, related_name='rentalpropertys', on_delete=models.CASCADE)
title = models.TextField(unique=True)
created_at = models.DateTimeField(auto_now_add=True)
PROPERTY_LISTING_CHOICES = Choices(
('APARTMENT', _('Apartment')),
('HOLIDAY_HOME', _('Holiday home')),
('SINGLE_FAMILY_HOME', _('Single family home')),
('COMMERCIAL', _('Commercial')),
)
type_of_property_listing = models.CharField(
max_length = 50,
choices = PROPERTY_LISTING_CHOICES,
default = PROPERTY_LISTING_CHOICES.APARTMENT,)
street = models.CharField(max_length=255)
borough = models.CharField(max_length=255)
image = models.ImageField(upload_to='images/', null=True, blank=True,)
def __str__(self):
return str(self.title)
class Contract(models.Model):
rentalproperty = models.ForeignKey("RentalProperty", related_name='contracts', on_delete=models.CASCADE)
insurance_required = models.BooleanField(default=True)
other_terms = models.TextField(blank=True)
def __str__(self):
return str(self.insurance_required)
first solution output:
address: Helsinki
title: build apps
insurance: Yes
- This repeats itself multiple times.
Second solution output:
address: Helsinki
title: build apps
insurance: Yes
- insurance repeats itself multiple times
What I expect:
address: Helsinki
title: build apps
insurance: Yes
Thanks for your time :)
With the help of #Timmy O'Mahony answer, I did {% for contract in property.contracts.all %} in my template using the first solutions and it works. The contracts is the related name to the rental property. Thanks all.

Get image.url as atributte in a related model in django template

I have a ListView where a I want to list products. The problem is that I can't get the related image of these products as they are in a different model.
The model for products is:
class Product(models.Model):
brand = models.ForeignKey(Brand, on_delete=models.CASCADE, verbose_name='marca')
name = models.CharField('nombre', max_length=40)
description = models.TextField('descripción', blank=True)
price = models.DecimalField(max_digits=8, decimal_places=2)
slug = models.SlugField(max_length=50)
active = models.BooleanField('activo',default=True)
in_stock = models.BooleanField('en stock', default=True)
tags = models.ManyToManyField(ProductTag, blank=True)
date_updated = models.DateTimeField('última actualización', auto_now=True)
The model of images is:
class ProductImage(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name='producto')
image = models.ImageField('imagen', upload_to="product-images")
thumbnail = models.ImageField('miniatura', upload_to="product-thumbnails", null=True)
To get both models in the template I used context.update in the view.
class ProductListView(ListView):
template_name = 'product_list.html'
context_object_name = 'products_list'
model = models.Product
paginate_by = 4
def get_context_data(self, **kwargs):
context = super(ProductListView, self).get_context_data(**kwargs)
context.update({'product_images_list': models.ProductImage.objects.all()})
return context
def get_queryset(self):
tag = self.kwargs['tag']
self.tag = None
if tag != 'all':
self.tag = get_object_or_404(models.ProductTag, slug=tag)
if self.tag:
products = models.Product.objects.active().filter(tags=self.tag)
else:
products = models.Product.objects.active()
return products.order_by('name')
Additionally, I created a filter to iterate both models in a forloop but I think is useless as I don't want to iterate both models, I just want to get the first image that matches the product's FK to show it in the template:
from django import template
register = template.Library()
#register.filter(name='zip')
def zip_lists(a, b):
return zip(a, b)
The template I'm using is:
{% extends 'base.html' %}
{% load humanize %}
{% load product_extras %}
{% block content %}
<div class="destacados">
{% for product, image in products_list|zip:product_images_list %}
<div class="collections coll-watches">
<img class="foto" src="{{ image.thumbnail.url }}">
<p class="prod-description">{{ product.name }}</p>
<p class="prod-description prices"><strong>$ {{ product.price|intcomma }}</strong></p>
<a class="boton-tr boton-tr-watches" href="#">Agregar al carrito</a>
</div>
{% endfor %}
</div>
{% endblock content %}
As you can see the problem is in <img class="foto" src="{{ image.thumbnail.url }}">. I know this is incorrect, but I don't know how to get the image related to the product through its FK.
I'm new i django (only two months) and I'm sure this should be easier, but I can figure it out...
Any help would be appreciated!!
Best regards
you can define a get image url method in your product model as below
def get_image_url(self):
img = self.productimage_set.first()
if img:
return img.thumbnail.url
return img #None
and call it in your templates get_image_url
Thank you very much #bmons. I had to modify the method a little bit, but your answer gave me the clue I needed.
This is the final code:
def get_image_url(self):
img = self.productimage_set.first().thumbnail.url
if img:
return img
return img #None
When Using img = self.productimage_set.thumbnail.first() I get a RelatedManager error.
Anyway, I really appreciate your help!! Cheers

How to create link for one of the category in Django

I'm coding a news website.I have 'category' in News model.
I want to get all the news in one of the categories named 'opinion' in my index.html. And create detail page link for each of them.
I can the title ,author, etc of the news mentioned above .But my brain really junks,I don't know how to create a link points to opinion_new.html or news_detail.htlm for each of them.I have a link for regular news to point to news_detail.htlm.
If you don't quite understand what I'm asking, please also read my last question How to get all the post in the same category in Django you so much!
here is part of my News model:
class News(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="cate", blank=True, verbose_name='分类')
here is my Category model:
class Category(models.Model):
name = models.CharField(max_length=40) # 分类名
class Meta:
verbose_name = "分类"
verbose_name_plural = verbose_name
def __str__(self):
return self.name
here is part of my view:
class NewsView(View):
def get(self, request):
opinion_news = News.objects.filter(category="opinion")
return render(request, 'index.html', {
'opinion_news': opinion_news,
})
here is part of my index.html
{% for opinion in opinion_news %}
<li class="media">
<a href='?'> <h>{{opinion.title}}</h></a>
</li>
{% endfor %}
here is part of my already works well news_detail view.
def newsDetailView(request, news_pk):
news = get_object_or_404(News, id=news_pk)
category = news.category
tags = news.tag.annotate(news_count=Count('news'))
all_comments = NewsComments.objects.filter(news=news)
news.comment_nums = all_comments.count()
news.save()
return render(request, "news_detail.html", {
'news': news,
'tags': tags,
'category': category,
})
here is my url for news_detail.html
path('-<int:news_pk>', views.newsDetailView, name="news_detail"),

django - can't filter object and transfer to template

I've got a model which I'm trying to filter according to an argument passed in the url, then display the filtered object via a template, but I don't know what I'm doing wrong.
Here's the urls.py:
url(r'^courses/(?P<course_code>\w+)/$', views.course, name="course"),
Here's the view:
from website.models import Course
def course(request, course_code):
current_course = Course.objects.filter(short_title='course_code')
template = loader.get_template('website/course.html')
context = Context({
'current_course': current_course,
})
return HttpResponse(template.render(context))
Here's the model:
class Course(models.Model):
title = models.CharField(max_length=200)
short_title = models.CharField(max_length=5)
course_type = models.CharField(max_length=100)
start_date = models.DateTimeField()
end_date = models.DateTimeField()
fee = models.IntegerField()
places = models.IntegerField()
venue = models.CharField(max_length=200)
description = models.TextField()
short_description = models.TextField()
age_low = models.IntegerField()
age_high = models.IntegerField()
And here's the template:
{% if current_course %}
{% for course in current_course %}
{{ current_course.title }}
{% endfor %}
{% else %}
<p>Sorry, that course doesn't exist.</p>
{% endif %}
And when I load the page /courses/CR1 (the course with short_title="CR1" definitely exists because it renders fine on another template where I'm not filtering but just displaying all the courses), it gives me "Sorry, that course doesn't exist."
Can anyone see what I'm doing wrong?
In this line:
current_course = Course.objects.filter(short_title='course_code')
You're checking for course titles with the exact text 'course_code'. You mean to use the value of the variable course_code:
current_course = Course.objects.filter(short_title=course_code)