django processing dynamic publishing of multiple image URLs

The back end is django

There is a way to handle a set of picture URLs. A dynamic is not with many pictures. If the database uses urlfield, only one url can be saved. So I save a set of URLs as a js array, and then pass them in with json, and save them in charfile. When reading them, I need to do a processing. With a user-defined filter, I first convert json into a python list, so that the template language can be called circularly

js code part

    $('#submit').click(function () {
        var src =[];
        var length = $('.upload-pic').length;
        for (var i=0;i<length;i++){
            src[i]=$('.upload-pic').eq(i).attr('src');//Get src of picture
        }//Access as list
        console.log(src);
        var content = $('#text-submit').val();
        var json_src = JSON.stringify(src);//Convert to json format
        $.ajax({
            url:'/ajax/submit_content/',
            type:'POST',
            contentType:'application/x-www-form-urlencoded',
            data:{'content':content,
                'src':json_src
            },
            tranditional:true,
            success:function (data,status) {
                $('.upload-pic').parent().remove();
                $('#img-wrap').hide();
                $('#text-submit').val('');//Empty the form and upload picture area
            }
        });
    });

Back end part

@login_required()
def submit_content(request):
    content = request.POST.get('content',None)
    src = request.POST.get('src',None)#Read src in json format
    Content.objects.create(user=request.user,
                           content=content,
                           type='dongtai',
                           publish_date=datetime.datetime.now(),
                           alter_date=datetime.datetime.now(),
                           image= src,
                           )
    return HttpResponse('bingo')

Tags: JSON Django Database Python

Posted on Sat, 02 May 2020 10:27:26 -0700 by jkeppens