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.

2
  • Possible duplicate of URL-parameters and logic in Django class-based views (TemplateView) Commented Aug 28, 2018 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. Commented Aug 28, 2018 at 12:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.