0

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

mbieren
  • 1,034
  • 8
  • 31
  • You can just return `self.kwargs` in your `get_context_data` method. Or you can do something like `ctx.update(self.kwargs)`. – Yevhen Kuzmovych Apr 16 '17 at 10:21
  • And according to [this answer](http://stackoverflow.com/a/37336322/4727702) `view` is passed within Template context. So your second solution should work without methods like `share_type` and `country`. – Yevhen Kuzmovych Apr 16 '17 at 10:27
  • thanks a lot I have posted my solution – mbieren Apr 17 '17 at 11:56

1 Answers1

0

I think the best way is to do it like this :

def dispatch(self, request, *args, **kwargs):

    self.share_type = self.kwargs.get('share_type', None)
    self.country = self.kwargs.get('country', None)
    self.postal_code = self.kwargs.get('postal_code', None)
    self.position = int(self.kwargs.get('position', None))
    self.position_verbose = verbose_position(self.position)

    ret = super(CreateAdvertisment, self).dispatch(request, *args, **kwargs)

    return ret

You can use then in the form_valid method

def form_valid(self, form):

    form.instance.advertiser = self.advertiser
    form.instance.share_type = self.share_type
    form.instance.country = self.country
    form.instance.postal_code = self.postal_code
    form.instance.position = self.position

    ret = super(CreateAdvertisment, self).form_valid(form)
    return ret

and in the template of course like this

            <strong>
            <div class="col-sm-3">
                Typ : {{ view.share_type }}
            </div>
            <div class="col-sm-3">
                PLZ : {{ view.postal_code }}
            </div>
            <div class="col-sm-3">
                Ort : TODO
            </div>
            <div class="col-sm-3">
                Pos : {{ view.position_verbose }}
            </div>

Many thanks for the proposals.

Regards

Michael

mbieren
  • 1,034
  • 8
  • 31