django处理上传文件的问题

发布于 2024-11-04 09:39:37 字数 1718 浏览 0 评论 0原文

我正在尝试实现一项允许用户上传文件的功能。该文件将包含要插入数据库的信息,这就是我在函数中保存数据的原因。不确定这是否是最好的选择。

到目前为止我所做的:

forms.py:views:template:function

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

那样

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 })

:

<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>

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       

问题是当我在模板中点击“保存”时,它很好地重定向到另一个模板,但我没有看到任何消息,就像我想 看看(final_insert.html 显示 {{ msg }})

有人可以帮助我理解我做错了什么吗?
欢迎任何帮助! 感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ〆星空下的拥抱 2024-11-11 09:39:37

我能够理解我的错误。
抱歉,我的愚蠢错误

是这样的形式:

<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对应于url“upload”而不是“upload_done”
我在表单中写入了 action={% url upload_done 'clinical' %},因此当我点击“保存”时,它会自动将我重定向到另一个模板。无需运行代码!

所以我将表单更改为:

<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>

现在它可以工作了..

对不起,我想我需要重定向到另一个页面,但是当他重定向时,他不会运行代码..

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

所以视图last_step对应于url“upload”而不是“upload_done”
我在表单中写入了 action={% url upload_done 'clinical' %},因此当我点击“保存”时,它会自动将我重定向到另一个模板。无需运行代码!

所以我将表单更改为:


现在它可以工作了..

对不起,我想我需要重定向到另一个页面,但是当他重定向时,他不会运行代码..

, final, name='upload_done'),

所以视图last_step对应于url“upload”而不是“upload_done”
我在表单中写入了 action={% url upload_done 'clinical' %},因此当我点击“保存”时,它会自动将我重定向到另一个模板。无需运行代码!

所以我将表单更改为:

现在它可以工作了..

对不起,我想我需要重定向到另一个页面,但是当他重定向时,他不会运行代码..

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/

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..

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

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:


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..

, 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:

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文