使用Django表单进行文件上传

huangapple go评论138阅读模式
英文:

File upload using Django Forms

问题

I have a file field in MissFormModel model:

file = models.FileField(upload_to='uploads/')

My index view displays all MissFormModel instances:

def index(request):
    context = {}
    forms = MissFormModel.objects.all()
    if forms.exists():
        return render(request, 'app/index.html', {'forms': forms, 'context': context})
    else:
        return render(request, 'app/index.html', {'forms': None, 'context': context})

My index template:

{% for i in forms %}
<tr>
    <th scope="row">{{ i.id }}</th>
    <th>{{ i.sender }}</th>
    <th>
        {% if i.file %}
        <a href="{{ i.file.url }}" class="btn btn-primary btn-sm" target="_blank">
            Download
        </a>
        {% endif %}
    </th>
</tr>
{% endfor %}

Now I am trying to implement file upload forms.

My forms.py looks like:

class SendNewRequest(ModelForm):
    class Meta:
        model = MissFormModel
        fields = ("sender", "file")
        labels = {
            'sender': '',
            'file': '',
        }
        widgets = {}

My view for file upload:

def send_new_request(request):
    form = SendNewRequest()
    if request.method == 'POST':
        form = SendNewRequest(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('index')
    return render(request, 'forms/new_request.html', {'form': form})

My new_request.html:

<form action="" method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Save</button>
</form>

But when I've created new forms with attachments, my index page displays nothing. The same situation in the admin page. Could you explain what I am doing wrong?

英文:

I have a file field in MissFormModel model:

file = models.FileField(upload_to =&#39;uploads/&#39;)

My index view display all MissFormModel instances:

def index(request):
context ={}

forms = MissFormModel.objects.all()

if forms.exists():
    return render(request, &#39;app/index.html&#39;, {&#39;forms&#39;:forms,&#39;context&#39;:context})
else:
    return render(request, &#39;app/index.html&#39;, {&#39;forms&#39;:None,&#39;context&#39;:context})

My index template:

{% for i in forms %}

&lt;tr&gt;
  &lt;th scope=&quot;row&quot;&gt;{{i.id}}&lt;/th&gt;
  &lt;th&gt;{{i.sender}}&lt;/th&gt;
    &lt;th&gt;
        {% if i.file %}
            &lt;a href=&quot;{{ i.file.url }}&quot; class=&quot;btn btn-primary btn-sm&quot; target=&quot;_blank&quot;&gt;
          Download
        &lt;/a&gt;
        {% endif %}
        
    &lt;/th&gt;
&lt;/tr&gt;
  {% endfor %}

Now I am trying to implement file upload forms.

My forms.py looks like:

class SendNewRequest(ModelForm):
class Meta:
    model = MissFormModel
    fields = (&quot;sender&quot;, &quot;file&quot;)
    labels = {
        &#39;sender&#39;:&#39;&#39;,
        &#39;file&#39;:&#39;&#39;,
        }
    widgets = {}

My view to file upload:

def send_new_request(request):
form = SendNewRequest()
if request.method == &#39;POST&#39;:
    form = SendNewRequest(request.POST, request.FILES)

    if form.is_valid():
        form.save()
        return redirect(&#39;index&#39;)

return render(request, &#39;forms/new_request.html&#39;, {&#39;form&#39;:form})

My new_request.html:

&lt;form action=&quot;&quot; method=&quot;POST&quot; encytpe=&quot;multipart/form-data&quot;&gt;
				{% csrf_token %}
				{{ form.as_p }}
				&lt;button type=&quot;submit&quot;&gt;Save&lt;/button&gt;
			&lt;/form&gt;

But when I've created new forms with attachment at my index page i see nothing. The same situation in admin page. Could you explain what i am doing wrong?

答案1

得分: 1

尝试这样做

您已经初始化了空表单,然后尝试保存它。

def send_new_request(request):
    
    if request.method == 'POST':
        form = SendNewRequest(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('index')
    form = SendNewRequest()
    
    return render(request, 'forms/new_request.html', {'form': form})
英文:

try this

You have intialzied the empty form and then you are trying to save it.

def send_new_request(request):
    
    if request.method == &#39;POST&#39;:
        form = SendNewRequest(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect(&#39;index&#39;)
    form = SendNewRequest()
    
    return render(request, &#39;forms/new_request.html&#39;, {&#39;form&#39;:form})

huangapple
  • 本文由 发表于 2023年5月29日 18:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定