Joe
Joe

Reputation: 239

django get_absolute_url redirecting issue

I have an object from which I want to have two get_absolute_urls, since I have two categories(rent,sale). However, if I set get_absolute_url 'rent' last, the items from sale are redirecting to 'rents' url ex: rents/1.

How can I make this to work?

model:

class Listing(models.Model):
    agent = models.ForeignKey(Agent, on_delete = models.DO_NOTHING)
    title = models.CharField(max_length=120)
    address = models.CharField(max_length = 120)
    area = models.CharField(max_length=120)
    description = models.TextField(blank = True)
    price = models.IntegerField()
    bedrooms = models.DecimalField(max_digits=2, decimal_places=1)
    bathrooms = models.DecimalField(max_digits=2, decimal_places=1, blank = True, null = True)
    garage = models.IntegerField(default = 0)
    sqft = models.IntegerField()
    categories= (('sale', 'sale'),('rent','rent'))
    category= models.CharField(max_length = 10, choices= categories,  null = True)
    lot_size = models.DecimalField(max_digits=5, decimal_places=1)
    photo_main = models.ImageField(upload_to = 'photos/%Y/%m/%d/')
    photo_1 = models.ImageField(upload_to = 'photos/%Y/%m/%d/', blank = True, null = True)
    photo_2 = models.ImageField(upload_to = 'photos/%Y/%m/%d/', blank = True, null = True)
    photo_3 = models.ImageField(upload_to = 'photos/%Y/%m/%d/', blank = True, null = True)
    photo_4 = models.ImageField(upload_to = 'photos/%Y/%m/%d/', blank = True, null = True)
    is_published = models.BooleanField(default = True)
    is_featured = models.BooleanField(default = True)
    list_date = models.DateTimeField(default=datetime.now, blank = True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('sale', args=[str(self.id)])

    def get_absolute_url(self):
        return reverse('rent', args=[str(self.id)])

urls

path('rents/',rents, name = 'rents'),
path('rents/<int:pk>',RentView.as_view(), name = 'rent'),

path('sales/',sales, name = 'sales'),
path('sales/<int:pk>',SaleView.as_view(), name = 'sale'),

html

   <a href="{{ item_vip.get_absolute_url }}"><button class='btn-view fntmk'>View</button></a>

Upvotes: 0

Views: 48

Answers (1)

Nahidur Rahman
Nahidur Rahman

Reputation: 815

As you have two categories rent and sale, you can use if statement inside get_absolute_url.

def get_absolute_url(self):
    if self.category == 'sale':
        return reverse('sale', args=[str(self.id)])
    else:
        return reverse('rent', args=[str(self.id)])

But you have set null=True in category= models.CharField(max_length = 10, choices= categories, null = True)

So you need to handle the case when category is null.

def get_absolute_url(self):
    if self.category == 'sale':
        return reverse('sale', args=[str(self.id)])
    elif self.category == 'rent':
        return reverse('rent', args=[str(self.id)])
    else:
        return '/' # Handler null case

Upvotes: 1

Related Questions