[英]Django file upload returns None
I'm trying to learn Python and Django, when I try to upload a file in my form Django detect my file as "None".我正在尝试学习 Python 和 Django,当我尝试以 Django 形式上传文件时,检测到我的文件为“无”。
forms.py: forms.py:
from django import forms
class FormularioTrabajo(forms.Form):
email = forms.EmailField(label="Correo Electrónico:", max_length=254)
Currículum = forms.FileField()
views.py:意见.py:
In views I want to save the file uploaded in the directory /media/在视图中,我想将上传的文件保存在目录 /media/
def trabaja(request):
if request.method == 'POST':
form = FormularioTrabajo(data=request.POST, files=request.FILES)
if form.is_valid():
file = request.FILES.get('file', None)
if file is not None:
file_name = file.name
file_path = '/media/' + file_name
with open(file_path, 'wb+') as destination:
for chunk in file.chunks:
destination.write(chunk)
form.save()
return HttpResponseRedirect(reverse("miapp:index"))
else:
return HttpResponse('None')
else:
form = FormularioTrabajo()
return render(request, "web/trabajar.html", {'form':form})
This is my template html:这是我的模板 html:
{% load bootstrap5 %}
{% bootstrap_css %}
{% bootstrap_javascript %}
{% bootstrap_messages %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% load static %}
<link rel="stylesheet" href="{% static '/style.css' %}">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
<title>Trabaja con nosotros</title>
</head>
<body>
<header>
<br>
<ul>
<li><a href="conocernos.html">Conócenos</a></li>
<li><a href="login.html">Inicio de Sesión</a></li>
<li><a href="signup.html">Registro de usuario</a></li>
<li><a href="contacto.html">Contacta con nosotros</a></li>
</ul>
{% load static %}
<a href="index.html"><img src="{% static 'img/logo.PNG'%}" class="logo"></a>
</header>
<div class="work-box">
<h3>Solicitud de Trabajo</h3>
<form id="contactForm" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button class="btn btn-primary btn-lg" type="submit">Submit</button>
{% endbuttons %}
</form>
</div>
</body>
</html>
Please correct this line请更正此行
if form.is_valid():
file = request.FILES.get('Currículum', None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.