如何从表单或Django中的上传输入文件中获取输入?

时间:2017-10-12 14:25:25

标签: python django django-forms django-views

我写了一个视图,可以从您可以直接发布数据的表单中获取输入,也可以上传" .csv"文件如下:

views.py

def Some_view(request):

    if request.method == 'POST':
        form = My_form(request.POST)


    else:
        request.method == 'POST' and request.FILES['myfile']
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)
        return render(request, 'myapp/des.html')

    return render(request, 'myapp/des.html', {'form': form})

html模板:des.html是这样的:

{% extends 'protocol/base.html' %}
{% load staticfiles %}

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>


{% block content %}


    <body>
        <div class="container">

            <div style="width:30%">
                <form action="/result_view/" method="post">
                    {%csrf_token%}
                    {% for error in form.non_field_errors %}
                    <div class="form-group has-errors text-danger small">
                        {{error}}
                    </div>
                    {% endfor %}
                    {% for field in form %}
                    <div class="form-group has-errors text-danger small">
                        {{field.errors}}
                    </div>
                    <div class="form-group has-errors text-danger small">
                    </div>
                    {% endfor %}

                    <div class="form-group">
                         {{form.AA}}
                    </div>

                    <div class="form-group">
                        <button class="btn btn-primary" style="width:100%; margin-bottom: 30px ">Submit</button>
                    </div>

                     <h2> or <h2>
<style type="text/css">



.button {
    background-color:  #ff9800; /* Green */
    border: none;
    color: black;
    padding: 10px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;

    margin-top: 10px;
    margin-bottom: 30px;
}

</style>

 <form method="post" enctype="multipart/form-data">
 <div class="heading">
</h4>
 </div>
    {% csrf_token %}
    <input class="input" type="file" name="myfile">
    <button class="button" type="submit">Upload</button>
  </form>

  {% if uploaded_file_url %}
    <p class="text" >File uploaded successfully. </p>
  {% endif %}

            </div>
            </form>
        </div>
    </body>
</html>
   {% endblock %}

forms.py

from django import forms

class My_form(forms.Form):

    AA = forms.CharField(widget=forms.Textarea(attrs={'class':'form-control','placeholder':'Ex.: ARNDCEQGHILKMFPSTWYV'}))


class Meta:
    fields = ['AA']

表单工作正常但在我的情况下上传方法不起作用,当我单独使用上传文件方法如果工作正常但使用表单方法,它不工作请帮助我在这方面谢谢。

1 个答案:

答案 0 :(得分:0)

我认为你需要改变条件顺序:

if request.method == 'POST' and request.FILES.get('myfile'):
    # You code here

elif request.method == 'POST':