Discussion:
MULTIPLE IMAGES UPLOAD FOR A POST
Pacôme Avahouin
2018-11-22 07:49:02 UTC
Permalink
Please help me. I am new to Django, cannot undertsand the following. I have
subclass of CreateView for creating a post. I'm creating a rental site
project where people can post their apart and attach files (images) to it.
One should have possibility to attach as many images as he wants to ONE
form. I have found in Internet a decision that I need to use 2 models - 1
model for post + 1 separate model for images. Is it so?

Post form is created and handled in my views.py by sublass of CreateView.
Now how to connect new separate model for images with my CreateView ? And
save all images to the current Post Form?

Thanks in advance

................
models.py

class Post(models.Model):
post_author = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
post_title = models.CharField(_('Title'),max_length=200, null=True,
blank=False)
post_type = models.CharField(_('Type'),max_length=18,
choices=post_type_list, default='appart')
post_content = models.TextField(_('Your post description'),blank=True)
post_created_date = models.DateTimeField(default=timezone.now,
help_text=_('Post creation date and time.'),)
post_published_date = models.DateTimeField(null=True, blank=True,
help_text=_('Post publication date and time.'),)

def get_absolute_url(self):
return reverse("posts:post_details",
kwargs={'pk':self.pk,'post_type':self.post_type})


class Post_Gallery(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
current_hour = datetime.datetime.now()
images = models.ImageField(_('Upload some
pictures'),upload_to='uploads/%Y/%m/%d/{}'.format(current_hour.hour),
max_length=100, null=True, blank=True)

def __unicode__(self):
return self.images.url

..............
forms.py

class NewPostForm(forms.ModelForm):

class Meta:
model = Post

fields = [
'post_title','post_type','post_content',
]

class GalleryForm(forms.ModelForm):

class Meta:
model = Post_Gallery

fields = ['images',]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['images'].widget.attrs.update({'multiple': True,
'accept': 'image/jpg,image/jpeg,image/png,image/gif',} )

.........................
actual views.py


class NewPostView(CreateView):
form_class = NewPostForm
template_name = 'posts/submit-post.html'
model = Post

@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)

def form_valid(self, form):
form.instance.post_author = self.request.user
return super().form_valid(form)

def get(self, request):
form_class = self.get_form_class()
form = self.get_form(form_class)
return render(self.request, 'posts/submit-post.html', {'form':
form})
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d72587c0-0c40-4bea-84f4-0e1b30900387%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
mazz ahmed
2018-11-22 08:13:05 UTC
Permalink
https://django-filer.readthedocs.io/en/latest/
Post by Pacôme Avahouin
Please help me. I am new to Django, cannot undertsand the following. I
have subclass of CreateView for creating a post. I'm creating a rental site
project where people can post their apart and attach files (images) to it.
One should have possibility to attach as many images as he wants to ONE
form. I have found in Internet a decision that I need to use 2 models - 1
model for post + 1 separate model for images. Is it so?
Post form is created and handled in my views.py by sublass of CreateView.
Now how to connect new separate model for images with my CreateView ? And
save all images to the current Post Form?
Thanks in advance
................
models.py
post_author = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
post_title = models.CharField(_('Title'),max_length=200, null=True,
blank=False)
post_type = models.CharField(_('Type'),max_length=18,
choices=post_type_list, default='appart')
post_content = models.TextField(_('Your post description'),blank=True)
post_created_date = models.DateTimeField(default=timezone.now,
help_text=_('Post creation date and time.'),)
post_published_date = models.DateTimeField(null=True, blank=True,
help_text=_('Post publication date and time.'),)
return reverse("posts:post_details", kwargs={'pk':self.pk
,'post_type':self.post_type})
post = models.ForeignKey(Post, on_delete=models.CASCADE)
current_hour = datetime.datetime.now()
images = models.ImageField(_('Upload some
pictures'),upload_to='uploads/%Y/%m/%d/{}'.format(current_hour.hour),
max_length=100, null=True, blank=True)
return self.images.url
..............
forms.py
model = Post
fields = [
'post_title','post_type','post_content',
]
model = Post_Gallery
fields = ['images',]
super().__init__(*args, **kwargs)
self.fields['images'].widget.attrs.update({'multiple': True,
'accept': 'image/jpg,image/jpeg,image/png,image/gif',} )
.........................
actual views.py
form_class = NewPostForm
template_name = 'posts/submit-post.html'
model = Post
@method_decorator(login_required)
return super().dispatch(*args, **kwargs)
form.instance.post_author = self.request.user
return super().form_valid(form)
form_class = self.get_form_class()
form = self.get_form(form_class)
form})
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/d72587c0-0c40-4bea-84f4-0e1b30900387%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/d72587c0-0c40-4bea-84f4-0e1b30900387%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABuXbh8CEN6PrMib%3DJ%2BkQ8d8K4P1sjrgBF%3D3TzS7VgpHbfrKPQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Pacôme Avahouin
2018-11-22 10:08:53 UTC
Permalink
Thank you mazz ahmed for your answer, but i still don't see how to use that
with my forms.py and views.py?
Post by Pacôme Avahouin
Please help me. I am new to Django, cannot undertsand the following. I
have subclass of CreateView for creating a post. I'm creating a rental site
project where people can post their apart and attach files (images) to it.
One should have possibility to attach as many images as he wants to ONE
form. I have found in Internet a decision that I need to use 2 models - 1
model for post + 1 separate model for images. Is it so?
Post form is created and handled in my views.py by sublass of CreateView.
Now how to connect new separate model for images with my CreateView ? And
save all images to the current Post Form?
Thanks in advance
................
models.py
post_author = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
post_title = models.CharField(_('Title'),max_length=200, null=True,
blank=False)
post_type = models.CharField(_('Type'),max_length=18,
choices=post_type_list, default='appart')
post_content = models.TextField(_('Your post description'),blank=True)
post_created_date = models.DateTimeField(default=timezone.now,
help_text=_('Post creation date and time.'),)
post_published_date = models.DateTimeField(null=True, blank=True,
help_text=_('Post publication date and time.'),)
return reverse("posts:post_details", kwargs={'pk':self.pk
,'post_type':self.post_type})
post = models.ForeignKey(Post, on_delete=models.CASCADE)
current_hour = datetime.datetime.now()
images = models.ImageField(_('Upload some
pictures'),upload_to='uploads/%Y/%m/%d/{}'.format(current_hour.hour),
max_length=100, null=True, blank=True)
return self.images.url
..............
forms.py
model = Post
fields = [
'post_title','post_type','post_content',
]
model = Post_Gallery
fields = ['images',]
super().__init__(*args, **kwargs)
self.fields['images'].widget.attrs.update({'multiple': True,
'accept': 'image/jpg,image/jpeg,image/png,image/gif',} )
.........................
actual views.py
form_class = NewPostForm
template_name = 'posts/submit-post.html'
model = Post
@method_decorator(login_required)
return super().dispatch(*args, **kwargs)
form.instance.post_author = self.request.user
return super().form_valid(form)
form_class = self.get_form_class()
form = self.get_form(form_class)
form})
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3c82ddd5-9f92-4f10-804a-6d337618899f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...