非类型对象没有属性是\u ajax

非类型对象没有属性是\u ajax,ajax,django,django-forms,django-rest-framework,django-views,Ajax,Django,Django Forms,Django Rest Framework,Django Views,我正在我的项目中使用。但我不需要什么定制,而是添加了Upload和Author字段作为外键。如果我像图书馆那样做的话,效果很好。但这里我得到的是Nonetype对象没有属性是\u ajax。即使我已登录,我也不知道为什么它是Nonetype 型号: class File(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) author = models

我正在我的项目中使用。但我不需要什么定制,而是添加了
Upload
Author
字段作为外键。如果我像图书馆那样做的话,效果很好。但这里我得到的是
Nonetype对象没有属性是\u ajax
。即使我已登录,我也不知道为什么它是
Nonetype

型号:

class File(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    visible_to_home = models.ManyToManyField(Home, blank=True)  # when none visible to all home
    visible_to_company = models.ManyToManyField(Company, blank=True)  
# when none visible to all company
# To determine visibility, check if vtc is none or include company of user and if true, check same for home
    created_date = models.DateTimeField(auto_now=True)
    published = models.BooleanField(default=True)
    upload = models.FileField(blank=True, null=True, upload_to=update_filename)
    title = models.CharField(max_length=225, blank=True, null=True)
    description = models.TextField(blank=True, null=True)
形式

看法

在post视图方法中,如果我使用
form=self.get\u form()
而不是
form=self.form\u类(self.request.post,self.request.FILES)
,那么问题就会出现

home.html

{% block extrascripts %}
<script type="text/javascript">
  $(function () {
     $(".upload-file").modalForm({formURL: "{% url 'file-upload' %}"});
  });
 </script>
{% endblock extrascripts %}
[2019年1月6日00:06:30]“POST/upload/file/HTTP/1.1”500 110437

根据我正在使用的库,它在提交时说
,表单是通过AJAX请求发布到formURL的
我想我跟踪到了错误,即当
file=form.save(commit=False)
file.save()在
post
函数中调用
method时,我需要发出ajax请求。你知道我怎么做吗

回答:所以我最终解决了这个问题:

class FileForm(PopRequestMixin, CreateUpdateAjaxMixin, forms.ModelForm):
class Meta:
    model = File
    fields = ('title', 'description', 'upload')

def save(self, commit=False):

    if not self.request.is_ajax():
        instance = super(CreateUpdateAjaxMixin, self).save(commit=commit)
        instance.author = User.objects.get(pk=self.request.user.pk)
        instance.save()
    else:
        instance = super(CreateUpdateAjaxMixin, self).save(commit=False)

    return instance

save
方法被
CreateUpdateAjaxMixin
覆盖,我在视图中删除了
post
方法。

这看起来不像是AJAX请求。你的AJAX代码在哪里?实际上我是在遵循[[1]哦,我的错。你应该添加追溯,可能想修复你发布的代码中的缩进。现在我添加了追溯,我使用的库中说它将post vai ajax提交给FormUrl。我不熟悉这个包,但我猜
def post
实际上应该是
下的
方法类视图
而不是独立的
定义
[如此处]()。除此之外,我不知道。您可能还想更新表单缩进。
{% block extrascripts %}
<script type="text/javascript">
  $(function () {
     $(".upload-file").modalForm({formURL: "{% url 'file-upload' %}"});
  });
 </script>
{% endblock extrascripts %}
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python3.6/contextlib.py", line 52, in inner
return func(*args, **kwds)
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/django/views/generic/base.py", line 89, in dispatch
return handler(request, *args, **kwargs)
File "/home/bishwa/PycharmProjects/sharefile/sharefile/file/views.py", line 103, in post
file = form.save(commit=False)
File "/home/bishwa/PycharmProjects/sharefile/env/lib/python3.6/site-packages/bootstrap_modal_forms/mixins.py", line 40, in save
if not self.request.is_ajax():
AttributeError: 'NoneType' object has no attribute 'is_ajax'
class FileForm(PopRequestMixin, CreateUpdateAjaxMixin, forms.ModelForm):
class Meta:
    model = File
    fields = ('title', 'description', 'upload')

def save(self, commit=False):

    if not self.request.is_ajax():
        instance = super(CreateUpdateAjaxMixin, self).save(commit=commit)
        instance.author = User.objects.get(pk=self.request.user.pk)
        instance.save()
    else:
        instance = super(CreateUpdateAjaxMixin, self).save(commit=False)

    return instance