在arjango CreateView

时间:2018-01-07 13:22:48

标签: python html django forms

我制作了一个django表单,我想在其中传递一些我需要显示的上下文(主要是标签中的数据库条目,以便用户可以从中进行选择)。我因此创建了一个get_context_data function,我将上下文添加到现有上下文中,如下所示:

def get_context_data(self, **kwargs):
    context = super(UploadView, self).get_context_data(**kwargs)
    context['categories'] = Category.objects.all()
    context['form'] = VideoForm
    return context

但是,表单不保存传递给数据库的信息。为什么那不起作用?

以下是我的代码的一部分!

forms.py:

class VideoForm(forms.ModelForm):
    category = forms.ModelChoiceField(queryset=Category.objects.all(), empty_label=None)
    class Meta:
        model = Video
        fields = [
            'title',
            'description',
            'description_short',
            'category',
            'time',
            'thumbnail',
            'type',
        ]

    def clean_thumbnail(self):
        picture = self.cleaned_data.get("thumbnail")
        if not picture:
            raise forms.ValidationError("No Image")
        else:
            w, h = get_image_dimensions(picture)
            if w/h != (16/9):
                raise forms.ValidationError("Image in wrong aspect ratio (should be 16:9)")
        return picture

upload.html(它很长,所以将它上传到Pastebin会更好)

views.py:

class UploadView(LoginRequiredMixin, CreateView):
   form_class = VideoForm
   template_name= 'upload.html'

   def form_valid(self, form):
       instance = form.save(commit=False)
       instance.uploader=self.request.user
       return super(UploadView, self).form_valid(form)

   def get_context_data(self, **kwargs):
       context = super(UploadView, self).get_context_data(**kwargs)
       context['categories'] = Category.objects.all()
       context['form'] = VideoForm
       return context

我使用自定义表单,因此我可以设置用于编辑CSS的类(而不是仅仅使用form.as_p并且从那里开始非常难看......)

编辑:

经过一些测试后,我发现如果我将print(instance)置于def form_valid(self, form):函数中,它就不会打印出任何内容,表明该实例为空。怎么会这样?此外,我尝试删除:context['form'] = VideoForm并且没有错误显示,但如果我正确填写表单,它仍然无法正常工作!

编辑2:

我创建了一个' form_invalid'功能如下:

def form_invalid(self, form):
    print(form.instance)
    return super(UploadView, self).form_invalid()

导致:

TypeError: form_invalid() missing 1 required positional argument: 'form'

这让我想到,当我在提交后重定向回到表单时,我没有收到任何错误。这是我写的表格:https://pastebin.com/3H6VRZR1

此外,我尝试使用form.as_p进行测试,导致同样的问题

编辑3:

在测试了一些答案之后,我们发现HTML页面本身的形式可能是因为表单到达form_invalid()完全为空。我决定再次尝试使用form.as_p,看看它是否仍然导致与custom form相同的问题。现在我收到了一个新错误:

null value in column "category_id" violates not-null constraint
DETAIL:  Failing row contains (8, test new fom reg, test-new-fom-reg, null, test, test, 2018-01-10 13:44:58.81876+00, 2018-01-10 13:44:58.818789+00, 1, thumbnails/test-new-fom-reg.png, 2, 1, /home/trie/Desktop/django/vidmiotest/media/videos/test.mp4).

with:

USER
admin

GET
No GET data

POST
Variable Value
title 'test new fom reg'
category '8'
type '1'
time '1'
description 'test'
csrfmiddlewaretoken `BeizxWHU5KDbixit9vpxKoxEeBxgU9MNITaNlkM1qtI0Aq6kIThHrtjfUsQXjxON'
description_short 'test'

FILES
Variable Value
thumbnail <TemporaryUploadedFile: sixteentonineratio.png (image/jpeg)>
videoFile <TemporaryUploadedFile: test 3.mp4 (video/mp4)>

作为数据从表单中发送的数据,该表单建议(基于thiscategory_id不在我的表单模型中。它是什么(该字段只是被称为category),但为什么它认为它应该在那里?

我刚检查了phppgadmin以查看数据库的样子,并且该字段被称为id_category,而在我的模型中它被称为类别,为什么?

编辑4:我从未为上述错误添加了回溯:

内部服务器错误:/ upload / Traceback(最近一次调用最后一次):   文件&#34; /home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/backends/utils.py" ;,第85行,在_execute中     return self.cursor.execute(sql,params) psycopg2.IntegrityError:列&#34; category_id&#34;中的空值违反非空约束 细节:失败行包含(12,测试,测试,空,测试,测试,2018-01-16 18:18:25.907513 + 00,2018-01-16 18:18:25.907538 + 00,6,缩略图/ test_d1MHjMX。 png,2,1,/ home /trie /Desktop / django / vidmiotest / media / videos / test.mp4)。

上述异常是导致以下异常的直接原因:

Traceback (most recent call last):
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/contrib/auth/mixins.py", line 52, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 89, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/views/generic/edit.py", line 172, in post
    return super().post(request, *args, **kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/views/generic/edit.py", line 142, in post
    return self.form_valid(form)
  File "/home/trie/Desktop/django/vidmiotest/upload/views.py", line 21, in form_valid
    instance.save()
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1280, in execute_sql
    cursor.execute(sql, params)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/trie/Desktop/django/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "category_id" violates not-null constraint
DETAIL:  Failing row contains (12, test, test, null, test, test, 2018-01-16 18:18:25.907513+00, 2018-01-16 18:18:25.907538+00, 6, thumbnails/test_d1MHjMX.png, 2, 1, /home/trie/Desktop/django/vidmiotest/media/videos/test.mp4).

2 个答案:

答案 0 :(得分:7)

问题中的问题是调用了super()。form_valid()并用context ['form'] = VideoForm覆盖构造的表单。 让框架在使用CreateView时设置表单。

继承的CreateView提供了设置表单,保存表单,在视图上设置self.object以及重定向到成功URL的功能。

即CreateView.form_valid提供:

self.object = form.save()

在UploadView中设置上传器是正确的,但调用super.form_valid会尝试再次保存表单。

据我了解,所期望的行为是:

  1. set uploader
  2. 保存对象
  3. 重定向到成功网址
  4. 代码:

    instance = form.save(commit=False)
    instance.uploader = self.request.user
    instance.save()
    return redirect(self.get_success_url()) # Skip the call to super
    

    另外在其他答案中指出,context ['form'] = VideoForm将覆盖CreateView的表单设置。

    我建议查看执行流程如何为CreateView工作,它将在GET和POST情况下为模板上下文设置表单。

    另一种解决方法是允许VideoForm在init中接受上传器:

    class VideoForm(forms.ModelForm):
        def __init__(self, uploader, *args, **kwargs):
            self.uploader = uploader
            super().__init__(*args, **kwargs)
    
        def save(self, commit=True):
            instance = super().save(commit=False)
            instance.uploader = self.uploader
            if commit:
               instance.save()
            return instance
    

    并提供表单上传

     class UploadView(..., CreateView):
         def get_form_kwargs(self):
             kwargs= super().get_form_kwargs()
             kwargs['uploader'] = self.request.user
             return kwargs
    

    希望它有所帮助。

    查看编辑3

    它说'category_id'的原因是django模型中的外键会自动以'_id'为后缀作为数据库中的列名。 Documentation

    视频模型可能具有Category的外键。

    如果在数据库中找到id_category,您可能与迁移/模型不同步?您应该尝试清除数据库和/或重新运行makemigrations / migrate

答案 1 :(得分:0)

您当然不需要context['form'] = VideoForm行。除了其他任何内容之外,它还会覆盖现有表单,以便您的模板永远不会出现任何错误。