Django - 如何使 CloudinaryField 不是强制性的以及如何允许用户删除他们上传的图像?
Django - how to make a CloudinaryField not mandatory and how to allow user to delete their uploaded image?
嘿,我刚开始学习Django,我希望我的用户能够上传个人资料图片,所以我用个人资料模型扩展了用户模型,但是,到目前为止我还没有弄清楚如何使图像上传成为非强制性的。当用户添加图片时,我希望他们也能够将其删除。
Screenshot - Not able to Submit without adding file
Screenshot - Not able to remove the added file
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField()
profile_image = CloudinaryField('profile_image')
def __str__(self):
return self.user.username
forms.py
class ProfileForm(ModelForm):
class Meta:
model = Profile
fields = ('bio', 'profile_image',)
views.py
@login_required(login_url='login')
def createPost(request):
form = PostForm()
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
# request.FILES necessary so that file is submitted
# also required to add enctype to the form
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('home')
context = {'form': form}
return render(request, 'my8gag/post_form.html', context)
无法在文档中或 SO 此处找到任何关于此的内容,因此将不胜感激 help/hints!提前致谢。
允许字段为空
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField()
profile_image = CloudinaryField('profile_image', null=True, default=None, blank=True)
def __str__(self):
return self.user.username
嘿,我刚开始学习Django,我希望我的用户能够上传个人资料图片,所以我用个人资料模型扩展了用户模型,但是,到目前为止我还没有弄清楚如何使图像上传成为非强制性的。当用户添加图片时,我希望他们也能够将其删除。
Screenshot - Not able to Submit without adding file
Screenshot - Not able to remove the added file
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField()
profile_image = CloudinaryField('profile_image')
def __str__(self):
return self.user.username
forms.py
class ProfileForm(ModelForm):
class Meta:
model = Profile
fields = ('bio', 'profile_image',)
views.py
@login_required(login_url='login')
def createPost(request):
form = PostForm()
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
# request.FILES necessary so that file is submitted
# also required to add enctype to the form
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
return redirect('home')
context = {'form': form}
return render(request, 'my8gag/post_form.html', context)
无法在文档中或 SO 此处找到任何关于此的内容,因此将不胜感激 help/hints!提前致谢。
允许字段为空
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField()
profile_image = CloudinaryField('profile_image', null=True, default=None, blank=True)
def __str__(self):
return self.user.username