I have a model
class MyModel(models.Model):
slug = models.UUIDField(default=uuid4, blank=True, editable=False)
advertiser = models.ForeignKey(Advertiser)
position = models.SmallIntegerField(choices=POSITION_CHOICES)
share_type = models.CharField(max_length=80)
country = CountryField(countries=MyCountries, default='DE')
# some other Fields. Edited in a ModelForm
This view is called by a url containg position, share_type, country as parameters. I would like to display these parameters in the template. What is the best way to do this. I already have these possibilies
1) use get_context_date and store this in the context
def get_context_data(self, **kwargs):
ctx = super(MyModel, self).get_context_data(**kwargs)
ctx['share_type'] = self.kwargs.get('share_type', None)
ctx['country'] = self.kwargs.get('country', None)
ctx['postal_code'] = self.kwargs.get('postal_code', None)
ctx['position'] = int(self.kwargs.get('position', None))
return ctx
This can then be used in the template
2) use the view variant
def share_type(self):
ret = self.kwargs.get('share_type', None)
return ret
def country(self):
ret = self.kwargs.get('country', None)
return ret
like
<div class="row">
<strong>
<div class="col-sm-3">
Type : {{ view.share_type }}
<div class="col-sm-3">
Country : {{ view.country }}
I think both way are somewhat redundant. Does anybody know a more generic approach to this.
Kind regards
Michael