Reputation:
I have a CreateView
in my project to post a tweet-like-post. It has a title and content.
It look like this in views.py
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['title', 'content']
def form_valid(self, form): # Author for post (required- if no- error)
form.instance.author = self.request.user
return super().form_valid(form)
And I would like to add picture-upload
column to make a persons able to upload a picture with the post.
My models.py looks like this
class Post(models.Model):
title = models.CharField(max_length=100, verbose_name='タイトル')
content = models.TextField(verbose_name='内容')
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('Post-detail', kwargs={'pk': self.pk})
I already have a uploaded_pic
folder in my static
with 777 permision.
Could you please tell me how to add there working picture-upload
column
Upvotes: 0
Views: 8712
Reputation: 323
picture_upload = models.ImageField()
If left as is it will upload to MEDIA_ROOT, which is recommended, but if you really want to modify that directory, you can use the upload_to
property.
from django.conf import settings
picture_uploaded = models.ImageField(upload_to=settings.STATIC_URL + '/uploaded_pic')
Upvotes: 0