Django 图像字段不更新图像
Django Image field doesn't update image
我最近遇到一个问题,我的 Django 应用程序中的 Django 图像字段在按下 "Post" 后没有更新图像。在管理面板中,一切正常。也许我错过了什么...让我知道。
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
categories = models.ManyToManyField('Category', related_name='posts')
image = models.ImageField(upload_to='images', default="images/None/no-img.jpg")
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
class Category(models.Model):
name = models.CharField(max_length=20
)
views.py
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .models import Post
from django.contrib.auth.models import User
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
def home(request):
content = {
'posts': Post.objects.all()
}
return render(request, 'blog/home.html', content)
def blog_category(request, category):
posts = Post.objects.filter(categories__name__contains=category).order_by('-date_posted')
content = {
'category': category,
'posts': posts
}
return render(request, 'blog/blog_category.html', content) #<--(didn't add content block) bug found 05.11.19
def upload_pic(request):
if request.method == 'POST':
form = ImageUploadForm(request.POST, request.FILES)
if form.is_valid():
m = ExampleModel.objects.get(pk=course_id)
m.model_pic = form.cleaned_data['image']
m.save()
return HttpResponse('image upload success')
return HttpResponseForbidden('allowed only via POST')
...
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['title', 'content', 'categories', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Post
fields = ['title', 'content', 'categories', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
我最近刚刚在我的网络应用程序中添加了 Category 和 Image 部分,Category[=28] =] 部分工作正常,但图像字段只是 post default
即使我选择当前图像。在管理领域它运作良好,所以我决定向社区询问这个烦人的问题。
确保模板中的表单具有 enctype
set correctly:
Note that request.FILES will only contain data if the request method was POST and the that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.
我最近遇到一个问题,我的 Django 应用程序中的 Django 图像字段在按下 "Post" 后没有更新图像。在管理面板中,一切正常。也许我错过了什么...让我知道。
models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
categories = models.ManyToManyField('Category', related_name='posts')
image = models.ImageField(upload_to='images', default="images/None/no-img.jpg")
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
class Category(models.Model):
name = models.CharField(max_length=20
)
views.py
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .models import Post
from django.contrib.auth.models import User
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
def home(request):
content = {
'posts': Post.objects.all()
}
return render(request, 'blog/home.html', content)
def blog_category(request, category):
posts = Post.objects.filter(categories__name__contains=category).order_by('-date_posted')
content = {
'category': category,
'posts': posts
}
return render(request, 'blog/blog_category.html', content) #<--(didn't add content block) bug found 05.11.19
def upload_pic(request):
if request.method == 'POST':
form = ImageUploadForm(request.POST, request.FILES)
if form.is_valid():
m = ExampleModel.objects.get(pk=course_id)
m.model_pic = form.cleaned_data['image']
m.save()
return HttpResponse('image upload success')
return HttpResponseForbidden('allowed only via POST')
...
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['title', 'content', 'categories', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Post
fields = ['title', 'content', 'categories', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
我最近刚刚在我的网络应用程序中添加了 Category 和 Image 部分,Category[=28] =] 部分工作正常,但图像字段只是 post default
即使我选择当前图像。在管理领域它运作良好,所以我决定向社区询问这个烦人的问题。
确保模板中的表单具有 enctype
set correctly:
Note that request.FILES will only contain data if the request method was POST and the that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.