自定义 Django 表单不保存

Posted

技术标签:

【中文标题】自定义 Django 表单不保存【英文标题】:Custom Django Form Not Saving 【发布时间】:2013-10-15 19:32:56 【问题描述】:

我正在尝试使用this method 自定义表单的输出。例如:

<div class="fieldWrapper">
     form.message.errors 
    <label for="id_message">Your message:</label>
     form.message 
</div>

如果我将表单呈现为form.as_p,一切正常,但单独呈现字段不起作用。

我有一些想要隐藏的字段,所以我尝试单独渲染每个字段。我试图隐藏的字段在模型类中具有null=True, blank=True 属性,因此不是必需的。

没有显示错误,而是刷新了页面并且没有更新数据。考虑到这些因素,我不确定我可能会错过什么。这是视图:

观点来自userena:

@secure_required
@permission_required_or_403('change_profile', (get_profile_model(), 'user__username', 'username'))
def profile_edit(request, username, edit_profile_form=EditProfileForm,
                 template_name='userena/profile_form.html', success_url=None,
                 extra_context=None, **kwargs):
"""
Edit profile.

Edits a profile selected by the supplied username. First checks
permissions if the user is allowed to edit this profile, if denied will
show a 404. When the profile is successfully edited will redirect to
``success_url``.

:param username:
Username of the user which profile should be edited.

:param edit_profile_form:

Form that is used to edit the profile. The :func:`EditProfileForm.save`
method of this form will be called when the form
:func:`EditProfileForm.is_valid`. Defaults to :class:`EditProfileForm`
from userena.

:param template_name:
String of the template that is used to render this view. Defaults to
``userena/edit_profile_form.html``.

:param success_url:
Named URL which will be passed on to a django ``reverse`` function after
the form is successfully saved. Defaults to the ``userena_detail`` url.

:param extra_context:
Dictionary containing variables that are passed on to the
``template_name`` template. ``form`` key will always be the form used
to edit the profile, and the ``profile`` key is always the edited
profile.

**Context**

``form``
Form that is used to alter the profile.

``profile``
Instance of the ``Profile`` that is edited.
"""
    user = get_object_or_404(get_user_model(),
                             username__iexact=username)

    profile = user.get_profile()

    user_initial = 'first_name': user.first_name,
                    'last_name': user.last_name

    form = edit_profile_form(instance=profile, initial=user_initial)

    if request.method == 'POST':
        form = edit_profile_form(request.POST, request.FILES, instance=profile,
                                 initial=user_initial)

        if form.is_valid():
            profile = form.save()

            if userena_settings.USERENA_USE_MESSAGES:
                messages.success(request, _('Your profile has been updated.'),
                                 fail_silently=True)

            if success_url:
                # Send a signal that the profile has changed
                userena_signals.profile_change.send(sender=None,
                                                    user=user)
                redirect_to = success_url
            else: redirect_to = reverse('userena_profile_detail', kwargs='username': username)
            return redirect(redirect_to)

    if not extra_context: extra_context = dict()
    extra_context['form'] = form
    extra_context['profile'] = profile
    return ExtraContextTemplateView.as_view(template_name=template_name,
                                            extra_context=extra_context)(request)

我包含了使用% include 'my-template.html' % 呈现表单的html。是什么阻止我更新配置文件对象?感谢您的任何想法!

【问题讨论】:

当您点击表单上的提交时,您的服务器日志会显示什么内容? 【参考方案1】:

尝试添加

form.errors

到您的模板某处查看是否有任何非字段错误!

【讨论】:

我之前不止一次遇到过同样的问题 :)

以上是关于自定义 Django 表单不保存的主要内容,如果未能解决你的问题,请参考以下文章

Django:使用自定义保存方法更新表单中的对象

在 Django 中使用自定义外键字段保存表单中的数据

使用注册表单添加到自定义用户字段(django)

如何使用django自定义表单保存多个到多个字段

Django 表单不为输入输出自定义类属性

使用 django 脆皮表单自定义非表单错误