I'm wanting to create a form that allows multiple image uploads. I've got a Listing model that looks like this:
class Listing(models.Model):
location = models.CharField("Address/Neighborhood", max_length=250)
class ListingImage(models.Model):
listing = models.ForeignKey(
Listing,
related_name="images",
on_delete=models.SET_NULL,
null=True,
)
image = models.ImageField()
I'm using django-crispy-forms to create the form on the page but I cannot figure out how to get the listing field onto the page.
class ListingModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset(
"Location",
Div("location", css_class="col-md-12 col-lg-6")
)
)
class Meta:
model = Listing
fields = "__all__"
And this is my view:
class ListingCreateView(LoginRequiredMixin, CreateView):
model = Listing
form_class = ListingModelForm
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
formfieldon the page? If yes, please add that code to your post.