开发者

problem handling upload file django

I'm trying to do a feature that would allow the user to upload a file. That file would have information to insert into the database, that's why in the function I'm saving data. Not sure if it's the best option.

What I've done so far:

forms.py:

class UploadFileForm(forms.Form):
    file = forms.FileField()

views:

def last_step(request, word):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            msg = handle_uploaded_file(word, request.FILES['file'])
            return render_to_response('final_insert.html', {'msg': msg})
    else:
        form = UploadFileForm()
    return render_to_response('upload.html', 
                               {
                                'word': word,
                                'form': form })

template:

<form enctype="multipart/form-data" action="{% url upload_done 'clinical' %}" method="post">
<div>
    {% for field in form %}
        {{ field }}
    {% endfor %} 
    <input type="submit" value="Save" />
</div>
</form>

function:

def handle_uploaded_file(word, f):
    msg = "first stop" 
    data = []
    for chunk in f.chunks():
        data.append(chunk)
    msg = "after chunk"    

开发者_如何学编程    if word == 'clinical':
        pat = Patient.objects.values_list('patient', flat=True)
        for i in data:
            if i[0] not in pat:
                b2 = Patient(patient=i[0])
                b2.save()
                msg = "number was inserted"
            else:
                msg = "something"

    return msg       

The problem is when I hit "save" in the template, it redirects well to another template, but I don't see any message, like I suppose to see (final_insert.html shows {{ msg }})

Can someone help me understand what I'm doing wrong?

Any help is welcome! Thanks for your help!


I was able to understand my mistake.
sorry guys for my silly mistake

so this is the form:

<form enctype="multipart/form-data" action="{% url upload_done 'clinical' %}" method="post">
<div>
    {% for field in form %}
        {{ field }}
    {% endfor %} 
    <input type="submit" value="Save" />
</div>
</form>

urls:

url(r'^insert/file/(?P<word>clinical)/upload/$', last_step, name="upload"),
url(r'^insert/file/(?P<word>clinical)/upload/result/$', final, name='upload_done'),

so the view last_step corresponds to the url "upload" and not "upload_done"
I wrote into the form action={% url upload_done 'clinical' %}, so when I hit save it will redirect me automatically to the other template. Without running the code!!

So I changed the form to:

<form enctype="multipart/form-data" action="{% url upload 'clinical' %}" method="post">
<div>
    {% for field in form %}
        {{ field.label_tag }}
        {{ field }}
    {% endfor %} 
    <input type="submit" value="Guardar" />
</div>
</form>

and now it works..

sorry guys, I thought I needed to redirect to the other page but when he redirects he doesn't run the code..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜