'How to display reverse relation data in Django with unique title?
I have 2 models and I want to display the data on my template, but currently, it's reflected with my other app. I have created the dynamic title and it's working fine on live but it's conflicting with other apps. Could you please help me here?
I want to display the data on ListingTech models, and a ListingTech models have multiple data of Listing so it should be displayed under the ListingTech model slug. A ListingTech model has multiple Listing so the title and description should be displayed of ListingTech.
It is working fine with the current code of the views.py file but it's giving an error whenever I try to access other app URL (Error is coming: The view company.views.company_list didn't return an HttpResponse object. It returned None instead.)
Please solve this issue, your help will be appreciated.
Here is my models.py file...
class ListingTech(models.Model):
city = models.ForeignKey(City, default=None, related_name="ListTechCity", on_delete=models.CASCADE, help_text=f"Value: Select City")
name = models.CharField(max_length=60, default=None, help_text=f'Type: String, Values: Enter Company Name')
slug= models.SlugField(max_length=60, unique=True, help_text=f'Type: String, Values: Enter Slug')
title = models.CharField(max_length=100, null=True, blank=True, verbose_name="Meta Title", help_text=f"Values: Enter ListingTech Title.")
description = models.TextField(max_length=200, verbose_name="Meta Description", null=True, blank=True, help_text=f'Values: Enter ListingTech Meta Description.')
listing_img = models.ImageField(upload_to="listing-image", default=None, help_text=f"Enter Listing Image", verbose_name="Upload Image")
overview=RichTextField(null=True,
blank=True,
verbose_name='Listing Overview',
help_text=f"Values: Enter Listing Overview")
here is another model
class Listing(models.Model):
city = models.ForeignKey(City,
verbose_name="Select City",
related_name='ListingCIty', null=True, blank=True, on_delete=models.CASCADE, help_text=f"Values: Select Company City")
listing_technology = models.ForeignKey(ListingTech,
verbose_name="Select Listing For",
related_name='ListingTechnology', on_delete=models.CASCADE, help_text=f"Values: Select Company Technology")
company = models.ForeignKey(Company,
verbose_name="Select Company",
related_name='ListingCompany', on_delete=models.CASCADE, help_text=f"Values: Select Company")
company_url = models.URLField(max_length=256, null=True, blank=True, verbose_name="Company Page URL", help_text=f"Values: Enter Company Page URL")
listing_overview = models.TextField(max_length=1500, null=True, blank=True, help_text=f'Values: Enter Listing Page Overview.')
here is my urls.py file...
re_path(r'^(?P<city_slug>[\w\-]+)/(?P<slug>[\w\-]+)', views.company_list, name="company_list"),
here is my views.py file...
def company_list(request, city_slug, slug):
comp_list = Listing.objects.filter(listing_technology__city__slug = city_slug, listing_technology__slug=slug).select_related('company','listing_technology').order_by('-boosted_price')
for item in comp_list:
if item.listing_technology.title and item.listing_technology.description != '':
title= item.listing_technology.title
description = item.listing_technology.description
template_name = 'company/list.html'
context = {'comp_list':comp_list, 'title':title, 'description':description}
return render(request, template_name, context)
else:
this_year = str(datetime.now().year)
this_month = datetime.strftime(datetime.now(), '%B')
proj_prop = list(map(lambda x:"Top" + " " + x.listing_technology.name + " " + this_month + " " + this_year, comp_list))
title = "".join(proj_prop[0])
prop_desc = list(map(lambda x:x.listing_technology.city.city_name + " " +"Based Top" + " " + x.listing_technology.name + ". " + "Are you looking for the top" + " " + x.listing_technology.name + "" +", Here is the list of top" + " " + x.listing_technology.name, comp_list))
description = "".join(prop_desc[0])
template_name = 'company/list.html'
context = {'comp_list':comp_list, 'title':title, 'description':description}
return render(request, template_name, context)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
