我有一个CreateView
的{{1}},其中我有一个Post
和title
列,我试图为该帖子添加content
列,但它不会可以,但是不会显示错误,所以我不知道自己在做什么错...我将其迁移,也不会显示用于上传图片的列。这是我的代码。
views.py
image upload
forms.py
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 5
class UserPostListView(ListView):
model = Post
template_name = 'blog/user_posts.html'
context_object_name = 'posts'
paginate_by = 5
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return Post.objects.filter(author=user).order_by('-date_posted')
class PostDetailView(LoginRequiredMixin, DetailView):
model = Post
class PostCreateView(LoginRequiredMixin, CreateView):
form_class = PostUpdateForm
model = Post
fields = ['title', 'content', 'image']
def form_valid(self, form): # Author for post (required- if no- error)
form.instance.author = self.request.user
return super().form_valid(form)
models.py
class PostUpdateForm(forms.ModelForm):
class Meta:
model = Post
fields = ['image']
post_form.html
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)
image = models.ImageField(default='default.jpg', upload_to='uploaded_pic')
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('Post-detail', kwargs={'pk': self.pk})