Need some help with preselecting form fields...

I am attempting to pre-fill out 2 fields in a form that are foreign keys. The user selects an Account (not the user model) to use and then is given a list (from django-tables2) of products. The user then selects a Product to create a Listing for. The form has fields for Product and Listing. I am able to prefill the Product field but cannot figure out how to pass the Account pk/slug to the ListingCreate view. I am able to pass the Account if I hard code it as below but it needs to be dynamic. Thanks in advance!

class ListingCreateFromAccountView(SuperUserRequiredMixin, CreateView):
model = Listing
form_class = ListingForm
template_name = 'listings/listing_create.html'
success_url = reverse_lazy('listings')

def get_initial(self):
    product = Product.objects.get(pk=self.kwargs['pk'])
    facebook_account = FacebookAccount.objects.get(name='account1')
    return {'product':, 'facebook_account':}

def form_valid(self, form):
    form.instance.product = Product.objects.get(pk=self.kwargs.get('pk'))
    return super(ListingCreateFromAccountView, self).form_valid(form)

Are you looking to do this on one page or two?

If you’re looking to do this on one page, you’ll need to use some JavaScript to populate the ListingCreate view.

If you’re looking to do this on two pages, then your first page links you to the second page where you can pass the appropriate keys as a parameter.

1 Like

How would I pass it as a parameter?

Page 1: select Account
Page 2: select Product
Page 3: CreateListing View with Account and Product prefilled.

I should be able to pass the parameter in the URL but the link to the CreateView is in a django-tables2 row and parameters are passed as or record.slug. I have not been able to figure out how to pass 2 parameters in django-tables2. I think this is my main issue. maybe…

You can pass multiple parameters through the url. It’s perfectly valid to construct a url that looks something like:

path('/create-listing/<int:account_pk>/<int:product_pk>/', SomeViewName.as_view())

1 Like

Ive tried passing it in the url with:

path('account/<slug:slug>/listing/create/<int:pk>', views.ListingCreateFromAccountView.as_view(), name='listing_create_from_account'),

but my link to the CreateView is:

<a href="{% url 'listing_create_from_account' %}"><button type="button" class="btn btn-primary">Create listing for this product</button></a>

I cannot figure out how to pass 2 parameters with the django-tables2 template column as it forces me to use or record.slug. Any ideas?

I get a NoReverseMatch error as it cannot find the account parameter.

I’m not familiar with django-tables2, so I can’t address it’s requirements, but you can supply multiple parameters in the url template call:
<a href="{% url 'listing_create_from_account' %}">

If there’s some reason why you can’t do this in your template, you could always generate the url in your view and pass the completed url to render through the context.

1 Like

I was able to solve the problem by passing the parameter through context.

Thank you again for all of your help, Ken! This is twice now you’ve saved my butt - I owe you some beers. Thanks!