[Fixed]-Django forms without model

0👍

Views.py:

def submitfilecontent(request):
    ext_allowed = ['gif', 'jpg', 'jpeg', 'png']
    today = datetime.datetime.today()
    save_dir = 'personne/static/personne/%d/%d/%d/' % (today.year, today.month, today.day)
    save_path = os.path.join(settings.MEDIA_ROOT, save_dir)
    save_url = os.path.join(settings.MEDIA_URL, save_dir)

    if request.method == 'POST':
        file = request.FILES['myfile']
        ext = file.name.split('.').pop()
        if not os.path.isdir(save_path):
            os.makedirs(save_path)

        new_file = '%s.%s' % (int(time.time()), ext)

        destination = open(save_path+new_file, 'wb+')
        for chunk in file.chunks():
            destination.write(chunk)
        destination.close()
        return  HttpResponse("Upload Succsefull to URL:%s" % (save_url+new_file)  )
    else:
        raise Http404
👤Lilia

1👍

Please add a url(below) in urls.

url(r'^submitfilecontent/$', submitfilecontent, name="submit-file-content"),

and in HTML, add the action url

<form enctype="multipart/form-data" action="/submitfilecontent/" method="post">{% csrf_token %}

Leave a comment