开发者

Problems in uploading files with django

开发者 https://www.devze.com 2023-02-14 15:08 出处:网络
I\'m new using django. I\'ve to upload a file and fot this I\'m followin开发者_JAVA百科g the instructions present in official documentation:

I'm new using django. I've to upload a file and fot this I'm followin开发者_JAVA百科g the instructions present in official documentation: http://docs.djangoproject.com/en/dev/topics/http/file-uploads/?from=olddocs

my index.htlm

    <form action="upload_file" enctype="multipart/form-data" method="POST">
        {% csrf_token %}
        <input type="file" name="upfile" size="30">
        <input type="submit" name="upfile" value= " Upload ">
    </form> 

my views.py:

def handle_uploaded_file(f):
    destination = open('/my_path_to_tmp/tmp_files/input_file', 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()
    if ( f.file_name.endswith("sdf") ):
        return "sdf"
    elif ( f.file_name.endswith("smi") ):
        return "smi"

def upload_file(request):
    if request.method == "POST":
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            file_type = handle_uploaded_file(request.FILES['upfile'])
            return HttpResponseRedirect('calculate', file_type)
    else:
        form = UploadFileForm()
    return render_to_response('upload.html', {'form': form})

my urls.py

urlpatterns = patterns('myapp.views',
    (r'^upload_file$', 'upload_file'),   
    (r'calculate/$', 'calculation'),
)

Really I don't know what I'm doing wrong here but it seems that the condition

if request.method == "POST":

in views.py fails. Even if the method="POST" to the html form.

Anybody has an idea?

Thank you so much!


Are you sure that your form action is correct?

Shouldn't it be something like this instead:

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


You could perhaps output the request.method at the beginning of your method, just to be sure... after that, print form._errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消