0

I have a form in which the value of the first field can be determined by the URL. ie: example.com/value/form in which value is the appropriate response to the first field on the form.

How can I pass this value to the form on the page?

class VarietyCreate(CreateView):
    template_name = "generic_create.html"
    model = Variety
    form_class = VarietyForm
    pk_url_kwarg = "pk"
    initial = {"product_group" : pk_url_kwarg}

    def get_success_url(self):
        return reverse("purchases:Variety Index", kwargs={'pk': self.kwargs.get('pk')})

What I have does not seem to work.

From my URLs:

url(r'^commodities/view/(?P<pk>[^\/]+)/create/$', views.VarietyCreate.as_view(), name='Variety Create'),
Adam Starrh
  • 6,428
  • 8
  • 50
  • 89

1 Answers1

1

Found the answer based on this post.

class VarietyCreate(CreateView):
    template_name = "generic_create.html"
    model = Variety
    form_class = VarietyForm

    def get_initial(self):
        return {"product_group" : self.kwargs.get('pk')}

    def get_success_url(self):
        return reverse("purchases:Variety Index", kwargs={'pk': self.kwargs.get('pk')})
Community
  • 1
  • 1
Adam Starrh
  • 6,428
  • 8
  • 50
  • 89