由于Django不允许动态上传多个文件,因此我知道必须使用AJAX和jQuery才能在我的服装图片内容管理系统中做到这一点。但是我基本上是一个后端人员,不想通过尝试在代码中合并前端库来弄乱现有的工作应用程序(我曾尝试过本教程https://simpleisbetterthancomplex.com/tutorial/2016/11/22/django-multiple-file-upload-using-ajax.html,但它只是弄乱了代码)
在多次阅读文档并进行了大量搜索之后,我在quora上找到了它。 http://qr.ae/TUICrh
这真是太好了,因为我不必弄清楚前端库,只用Django / python代码就可以进行多次上传。
但是我不知道如何将其与我的代码集成。我将在此处附加我的代码。请帮助我提供具体的代码。
models.py
def file_rename(instance, filename):
if instance.category.name == 'Jeans':
prefix = 'string-1'
count = Count.objects.get(pk=1)
elif instance.category.name == 'Kurti':
prefix = 'string-2'
count = Count.objects.get(pk=2)
ext = filename.split('.')[-1]
ZSN = prefix + str(count.jeans_count + 1)
filename = '{}.{}'.format(ZSN, ext)
return os.path.join('images', filename)
class Images(models.Model):
design_id = models.CharField(max_length=128)
file = models.ImageField(upload_to=file_rename)
cost_price = models.FloatField()
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=False)
fabric = models.ForeignKey(Fabric, on_delete=models.CASCADE, blank=False)
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE, blank=False)
selling_price = models.FloatField()
aliveness = models.IntegerField(default=1)
date_added = models.DateTimeField(default=datetime.datetime.now)
group = models.ForeignKey(Group, on_delete=models.CASCADE, blank=False)
no_of_designs_or_colors = models.IntegerField(blank=False, null=True)
catalogue_name = models.CharField(max_length=50, null=False, blank=False)
visit_count = models.IntegerField(default=0)
set_sizes = models.ForeignKey(Sizes, on_delete=models.CASCADE, blank=True)
set_cat_id = models.IntegerField(default=1)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE, blank=False)
colours = models.CharField(max_length=128, null=True, blank=True)
def __str__(self):
return self.design_id
forms.py
class ImagesForm(forms.ModelForm):
class Meta:
model = Images
fields = ('file','category','fabric','manufacturer','cost_price','group','catalogue_name','brand', 'set_sizes','no_of_designs_or_colors','colours')
upload-photo.html
{% extends "cms/base.html" %}
{% block title %}
Upload-photo
{% endblock %}
{% block main %}
<form id="upload_form" method="post" action="/home/upload-photo/" enctype="multipart/form-data">
{% csrf_token %}
{{ upload_image.as_p }}
<input type="submit" value="Upload" />
</form>
<br />
{% endblock %}
views.py
@login_required
def uploadphoto(request):
context_dict = {}
if request.method == 'POST':
form = ImagesForm(request.POST,request.FILES)
if form.is_valid():
image = form.save(commit=False)
#calculate selling price based on a percentage and store in sp
image.selling_price = sp
#getting all attributes and generating design-id and store in string
image.design_id = string
image.save()
#putting a border to the image
image2 = Image.open('media/' + str(image.file))
width, height = image2.size;
image2 = ImageOps.expand(image2, border=(int(width/25),int(height/20),int(width/25),int(height/10)), fill='rgb(0,0,0)')
draw = ImageDraw.Draw(image2)
font = ImageFont.truetype('Roboto-Bold.ttf', size = int(height/40))
header = "CMS"
#ZSN for jeans
if cat == "Jeans":
count = Count.objects.get(pk=1)
count.jeans_count = count.jeans_count + 1
count.save()
header2 = "string-1" + str(count.jeans_count)
else:
count = Count.objects.get(pk=2)
count.jeans_count = count.jeans_count + 1
count.save()
header2 = "string-2" + str(count.jeans_count)
image2.save('media/' + str(image.file))
data = open('media/' + str(image.file), 'rb')
s3.Bucket('bucket').put_object(Key=str(image.file), Body=data)
return HttpResponseRedirect('/home/')
else:
form = ImagesForm()
context_dict = {'upload_image': form}
return render(request, 'cms/upload-photo.html',context_dict)
我已经放置了代码,以便可以获取流程并建议我根据代码中的定额链接必须进行哪些更改才能准备好多个上传。