Pacôme Avahouin
2018-11-22 07:49:02 UTC
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})
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.
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.