I have a Django project and I created a post operation in this project, so the user will be able to share multiple picture posts. But I have a problem. I can't write multiple image upload function. I looked at a lot of content, but it doesn't work. Either it reports a problem or context is not sent to the HTML page I wanted. Please help me. The multi picture function I want should be under CreateView
and should be placed in the same template as it. Also, there should be 4 Image upload buttons, and the last one should be assigned multiple features (to HTML tag).
models.py:
class Photo(models.Model):
post= models.ForeignKey(Person, on_delete=models.CASCADE)
image = models.ImageField(upload_to=get_image_filename)
uploaded_at = models.DateTimeField(auto_now_add=True)
views.py:
class PersonCreateView(CreateView):
model = Person
form_class = PersonForm
success_url = reverse_lazy('person_changelist')
forms.py:
class PhotoForm(forms.ModelForm):
image = forms.ImageField(label='Image')
class Meta:
model = Photo
fields = ('image', )
class PersonForm(forms.ModelForm):
title = forms.CharField(max_length=128)
body = forms.CharField(max_length=245, label="Item Description.")
file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
class Meta:
model = Person
widgets = {
'valyuta': forms.RadioSelect,
'barter': forms.CheckboxInput,
}
fields = ('country', 'city', 'ban', 'yurus', 'reng', 'qiymet', 'valyuta', 'yanacaqnovu', 'oturucu', 'squtu', 'buraxilisili', 'hecm', 'seher', 'barter', 'metin')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['city'].queryset = City.objects.none()
if 'country' in self.data:
try:
country_id = int(self.data.get('country'))
self.fields['city'].queryset = City.objects.filter(country_id=country_id).order_by('name')
except (ValueError, TypeError):
pass # invalid input from the client; ignore and fallback to empty City queryset
elif self.instance.pk:
self.fields['city'].queryset = self.instance.country.city_set.order_by('name')