2

我正在使用 django 和 jquery 来使用 ajax 上传文件。但是,在文件上传后(并且文件确实上传正确;数据库已更新并且它们在服务器上)javascript 返回SyntaxError: Unexpected token <错误。

我不知道这是javascript中的错误还是django中的错误;我看到的其他问题似乎建议确保信息确实是 JSON 格式,但在我的情况下似乎格式正确。我真的不明白为什么会出现错误,因为一切似乎都很好。我的日志中没有错误。

如果我只能看到更有用的错误消息,我会更容易解决问题。

这是我的views.py:

def response_mimetype(request):
    if "application/json" in request.META['HTTP_ACCEPT']:
        return "application/json"
    else:
        return "text/plain"

class JSONResponse(HttpResponse):
    """JSON response class."""

    def __init__(self,obj='',json_opts={},mimetype="application/json",*args,**kwargs):
        content = simplejson.dumps(obj,**json_opts)
        a = super(JSONResponse,self).__init__(content,mimetype,*args,**kwargs)

class UploadedFileCreateView(CreateView):
    model = UploadedFile
    form_class = UploadedFileForm

    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.object.project_id = self.kwargs['proj_key']
        self.object.save()
        f = self.request.FILES.get('file')

        data = [{
            'name': self.object.name(),
            'url': "/uploads/xmlfiles/" + self.object.name().replace(" ", "_"),
            'delete_url': reverse('fileupload:upload-delete',
                kwargs={'pk':self.object.id,
                'proj_key':self.kwargs['proj_key']}),
            'delete_type': "DELETE"}]

        response = JSONResponse(data, {}, response_mimetype(self.request))

        response['Content-Disposition'] = 'inline; filename=files.json'

        return super(UploadedFileCreateView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = super(UploadedFileCreateView, self).get_context_data(**kwargs)
        context['files'] = UploadedFile.objects.all()
        context['proj'] = int(self.kwargs["proj_key"])
        return context

这是我的models.py:

class UploadedFile(models.Model):
    """This represents a file that has been uploaded to the server."""
    STATE_UPLOADED = 0
    STATE_ANNOTATED = 1
    STATE_PROCESSING = 2
    STATE_PROCESSED = 4
    STATES = (
        (STATE_UPLOADED, "Uploaded"),
        (STATE_ANNOTATED, "Annotated"),
        (STATE_PROCESSING, "Processing"),
        (STATE_PROCESSED, "Processed"),
    )

    status = models.SmallIntegerField(choices=STATES,
        default=0, blank=True, null=True) 
    file = models.FileField(upload_to=settings.XML_ROOT)
    project = models.ForeignKey(Project)

    def __unicode__(self):
        return self.file.name

    def name(self):
        return os.path.basename(self.file.name)

    def save(self, *args, **kwargs):
        if not self.status:
            self.status = self.STATE_UPLOADED
        super(UploadedFile, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        os.remove(self.file.path)
        self.file.delete(False)
        super(UploadedFile, self).delete(*args, **kwargs)

    def get_absolute_url(self):
        return u'/upload/projects/%d' % self.id 

class UploadedFileForm(ModelForm):
    class Meta:
        model = UploadedFile
        exclude = ('project',)
4

1 回答 1

3

根据此处的文档,该form_valid()方法需要将 JSON 作为HTTPResponse. form_valid()相反,调用超类将覆盖前两行创建的响应。

编辑 -这对我有用:

def response_mimetype(request):
  if "application/json" in request.META['HTTP_ACCEPT']:
    return "application/json"
  else:
    return "text/plain"

class UploadedFileCreateView(CreateView):
  model = UploadedFile
  form_class = UploadedFileForm

  def form_valid(self, form):
    self.object = form.save(commit=False)
    self.object.project_id = self.kwargs['proj_key']
    self.object.save()
    f = self.request.FILES.get('file')

    data = [{
      'name': self.object.name(),
      'url': "/uploads/xmlfiles/" + self.object.name().replace(" ", "_"),
      'type': "application/xml",
      'size': self.object.file.size,
      'delete_url': reverse('fileupload:upload-delete',
        kwargs={'pk':self.object.id,
        'proj_key':self.kwargs['proj_key']}),
      'delete_type': "DELETE"}]

    return HttpResponse(simplejson.dumps(data), content_type = response_mimetype(self.request))

上面链接中的代码将 ajax 响应代码捆绑为 Mixin,这将允许项目中的其他形式重用代码。

在 Firefox 插件 Firebug、Net 选项卡或 Chrome 等效项中查看对 Ajax 调用的响应会有所帮助。这些将显示请求和响应标头,确保发送和返回正确的设置。

于 2013-11-05T13:56:52.627 回答