1

I have a url of the form:

path('makeoffer/<listing_id>', OfferCreateView.as_view(), name="CreateOfferview"),

This view is a form, with a couple of parameters. The listing_id needs to be saved as part of the Offer object, and is not user-editable. I essentially want to do this:

def form_valid(self, form):

    form.instance.user = self.request.user
    form.instance.datetime = dt.now()
    form.instance.listing_id = listing_id #(from the url)

How do I access the parameter from inside the form_valid function? I've tried get_context_data, but the only way I could see to do it would be for the get_context_data to pass it to the view, and then have the view pass it back to the form, which seems a bit hacky.

Alex
  • 2,270
  • 3
  • 33
  • 65
  • Possible duplicate of [URL-parameters and logic in Django class-based views (TemplateView)](https://stackoverflow.com/questions/15754122/url-parameters-and-logic-in-django-class-based-views-templateview) – ikkuh Aug 28 '18 at 12:30
  • I did get the answer from the linked question, thank you. Not an exact duplicate, as that referred to getting these parameters into the view (for which one can override get_context_data). One of the answers/comments mentioned self.kwargs, which was the solution here. – Alex Aug 28 '18 at 12:35

0 Answers0