1👍
Right now in trades_filtered.html
you take the ID of a given scenario
and use that ID to manually construct your five URLs. If it were me, I’d use a custom model method to determine whether or not we need to generate a URL first.
class Scenario(models.Model):
name = models.CharField(max_length=256, blank=False, null=False, unique=True)
description = models.TextField(max_length=1000)
def generate_trade_url(self):
if self.trade_set.exists():
return reverse('scenarios:trade-scenario', kwargs={'pk':self.pk})
return None
def generate_email_url(self):
...
You would need one method like this for each URL you want to generate. You can this use this logic either in the view (preferable) or in your template (simpler but slower) to dynamically generate your URLs only when they are valid.
EDIT: I just looked at this answer a second time. I included null=False
in the name
field definition because it was in the original, but be aware that it doesn’t actually do anything useful on a CharField
. Django doesn’t use null values for those fields, instead storing them as ''
(empty string).