Python Django如果ImageField留空或等于none,则返回上次上载的图像
我正在尝试创建一个图像库,人们可以上传多张图像。有8个字段用于上传图像。我遇到的问题是,当用户上传的图像少于8张时。如果其中一个图像字段留空,则会导致库中该特定字段留空 当前,当我返回图像时,我会对其进行过滤,以显示上载的最新图像。 例如:Python Django如果ImageField留空或等于none,则返回上次上载的图像,python,django,image,imagefield,Python,Django,Image,Imagefield,我正在尝试创建一个图像库,人们可以上传多张图像。有8个字段用于上传图像。我遇到的问题是,当用户上传的图像少于8张时。如果其中一个图像字段留空,则会导致库中该特定字段留空 当前,当我返回图像时,我会对其进行过滤,以显示上载的最新图像。 例如: gallery = Content.objects.filter(user=request.user).order_by("-id")[0] 我想知道是否有人能帮我找到一种方法,当我使用: content.image1 = request.FILES.ge
gallery = Content.objects.filter(user=request.user).order_by("-id")[0]
我想知道是否有人能帮我找到一种方法,当我使用:
content.image1 = request.FILES.get('image1_upload', None)
如果没有返回任何图像,我可以让Django使用提交的最后一个图像,而不是它返回的图像。我觉得应该是这样的:
if 'image1_upload' in request.FILES:
content.image1 = request.FILES('image1_upload')
elif None:
content.image1 = Content.objects.get("the last uploaded image")
谢谢你的帮助!下面是我的代码“
Views.py:
@login_required
def register(request):
if request.POST:
content = Content()
content.user = request.user
content.image1 = request.FILES.get('image1_upload', None)
content.image2 = request.FILES.get('image2_upload', None)
content.image3 = request.FILES.get('image3_upload', None)
content.image4 = request.FILES.get('image4_upload', None)
content.image5 = request.FILES.get('image5_upload', None)
content.image6 = request.FILES.get('image6_upload', None)
content.image7 = request.FILES.get('image7_upload', None)
content.image8 = request.FILES.get('image8_upload', None)
content.terms = request.POST.get('terms')
content.date = timezone.now()
content.save()
return redirect('/portal/register')
try:
gallery = Content.objects.filter(user=request.user).order_by("-id")[0]
print gallery
return render(request, 'portal/register.html', {'gallery': gallery})
except ObjectDoesNotExist:
print 'Does Not Exist!'
return render(request, 'portal/register.html')
型号:
def content_file_name(instance, filename):
return '/'.join(['content', instance.user.username, filename])
class Content(models.Model):
user = models.OneToOneField(User, unique=True)
image1 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image2 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image3 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image4 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image5 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image6 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image7 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
image8 = models.ImageField(upload_to=content_file_name, null=True, blank=True)
terms = models.ImageField(upload_to=content_file_name, null=True, blank=True)
HTML:
{%extends'portal/base.html%}
{%load staticfiles%}
{%block head\u block%}
{%endblock%}
{%block body_block%}
{%csrf_令牌%}
{{user.username}
iPad视图
&时代;
情态标题
{{user.Username}
{{gallery.terms}
接近
保存更改
{%endblock%}
你为什么要做你正在做的事情?因此,如果你想上传500张图片,你可以向模型中添加500个字段,然后向视图中添加500行代码以捕获所有字段?来吧,有一个更好的方法。让编程语言来完成繁重的工作
根据,您需要的是创建一个图像模型,该模型将外键添加到您的内容模型中,然后您可以在视图中执行以下操作:
{% for i in content.image_set.all %}
{# then iterate over i to grab all the images into your image tag #}
{% endfor %}
链接帮助您前进(我建议您密切关注那里发生的事情):
- 了解
- InlineFormsetFactory是一个非常方便的功能,可以帮助您提交带有相关模型的表单
- 上面的代码片段,如
恕我直言,你的方法是一场灾难,不会有丝毫的伸缩性。你为什么要做你正在做的事情?因此,如果你想上传500张图片,你应该向模型中添加500个字段,然后向视图中添加500行代码来捕获所有字段?来吧,有一个更好的方法。让编程来吧语言起着重要作用 根据,您需要的是创建一个图像模型,该模型将外键添加到您的内容模型中,然后您可以在视图中执行以下操作:
{% for i in content.image_set.all %}
{# then iterate over i to grab all the images into your image tag #}
{% endfor %}
链接帮助您前进(我建议您密切关注那里发生的事情):
- 了解
- InlineFormsetFactory是一个非常方便的功能,可以帮助您提交带有相关模型的表单
- 上面的代码片段,如
恕我直言,您的方法是一场灾难,不会有任何规模。我认为您的问题与您想象的不同。您听说过循环吗?它们可以帮助您避免一直编写同一代码八次,也可以帮助您避免出现您首先描述的问题。是的,我听说过我肯定听说过循环。我能把多个图像保存到一个imageField中,然后按顺序调用它吗?你能举个例子吗?你可以创建一个
图像
模型,它有一个外键指向它所属的内容
实例。然后你可以在每个内容
中有任意数量的图像
s站姿。使用Content\u实例访问内容
实例的图像
s。图像集
(默认名称,可通过相关名称
更改)我认为你的问题与你想象的不同。你听说过循环吗?它们可以帮助你避免每次编写八次相同的代码,也可以帮助你避免出现你首先描述的问题。是的,我确实听说过循环。我能将多个图像保存到一个imageField中,然后按顺序调用它?你会有一个例子吗?你可以创建一个图像
模型,它有一个它所属的内容
实例的外键。然后你可以在每个内容
实例中拥有任意数量的图像
s。使用内容
实例访问内容
实例的图像.image\u set(默认名称,可通过相关\u name
更改)我感谢你的帮助。我肯定会研究这些。我将其设置为8个图像,因为我想专门使用8个图像。我还有更多的学习要做…在这方面还是新的。感谢你的指导!我感谢你的帮助。我肯定会研究这些。我将其设置为8个图像,因为我想专门使用8个图像。我有更多的学习要做…在这方面还是新的。谢谢你的直接指导