我正在使用 Django 1.5.2、jQuery v1.9.0 和 POSTGRESQL 9.1。
我正在使用 ajax 发送表单,但 requst.FILE 为空,imgen 未发送。
这是我的文件。
模型.py
class Evento(models.Model):
nombre = models.CharField(max_length=16)
def __unicode__(self):
return self.nombre
class Imagen(models.Model):
def url(self,filename):
ruta = "MultimediaData/Evento/%s/%s"%(self.evento.id,str(filename))
return ruta
evento = models.ForeignKey(Evento)
photo = models.ImageField(upload_to=url)
def __unicode__(self):
return str(self.photo)
URSL.py
urlpatterns = patterns('CasadeAna.apps.home.views',
url(r'^addevento/$','addevent_view',name='event_add'),
url(r'^editevento/(?P<idevento>.*)/$','editevent_view',name='event_edit'),
)
视图.py
def editevent_view(request,idevento):
if request.method == 'GET' and request.is_ajax():
formulario = FormImagen()
ctx = {'form':formulario}
return render_to_response('home/event/imagenform.html',ctx,context_instance=RequestContext(request))
elif request.method == 'POST' and request.is_ajax():
print 'dasd'
print request.POST
print request.FILES
print 'dasd'
formulario = FormImagen(request.POST,request.FILES)
if formulario.is_valid():
formulario.save()
return HttpResponse('very good')
else:
ctx = {'form':formulario}
return render_to_response('home/event/imagenform.html',ctx,context_instance=RequestContext(request))
elif request.method == 'GET':
evento = Evento.objects.get(id=idevento)
ctx = {'evento':evento}
imagen = Imagen.objects.filter(evento=evento)
return render_to_response('home/event/editEvent.html',ctx,context_instance=RequestContext(request))
HTML
形式
imageform.html
<form id="formulario" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<button type="submit"class="btn btn-primary" onClick="enviar(this);">Guardar</button>
<button type="reset" class="btn btn-primary">Limpiar</button>
</form>
编辑事件.html
{% extends 'base.html' %}
{% block menu %}
{% endblock %}
{% block content %}
<div style="height:100%;" align="left" >
<h1 class="muted"> Editar Imagenes </h1>
<h2>{{evento.nombre}}</h2>
<div id="imagenes">
<div id="menu" >
<button class="btn" onClick="subirImagen(this);">Agregar Imagen <i class="icon-plus"></i></button>
<div>
<div id="contenedor">
<ul class="thumbnails">
{% if imagen %}
{% for i in imagen %}
<li class="span4">
<img data-src="{{i.photo}}" alt="">
</li>
{% endfor %}
{% endif %}
</ul>
</div>
</div>
</div>
{% endblock %}
{% block js %}
<script type="text/javascript">
//pedir formulario para subir una imagen
function subirImagen(e)
{
$.ajax({
url: '/editevento/14/',
type: 'GET',
dataType: 'html',
success: function(datos){
$('#contenedor').empty();
$('#contenedor').append(datos);
}
});
}
function enviar(e)
{
$('#formulario').submit(function(evento){
evento.preventDefault();
var datos_formulario = $(this).serialize();
alert('event444os');
$.ajax({
url: '/editevento/14/',
data: datos_formulario,
type: 'POST',
dataType: 'html',
processData:false,
cache:false,
success: function(datos){
$('#contenedor').empty();
$('#contenedor').append(datos);
}
});
});
}
</script>
{% endblock %}
提交表单 request.post 值和文件是 request.POST
请求文件
请帮忙!!