Frage

I am trying to use customize the output of my form by this method. For example:

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

If I render the form as form.as_p, everything works fine, but rendering the fields individually does not work.

I have some fields that I want to be hidden so am trying to render each individually. The fields I am trying to hide are given null=True, blank=True attributes in the model class, and are therefore not required.

No errors are being shown, rather the page is refreshed and the data is not updated. I'm not sure what I could be missing given these factors. Here is the view:

The view is from 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)

I am including the html to render the forms using {% include 'my-template.html' %}. What could be preventing me from updating the profile object? Thanks for any ideas!

War es hilfreich?

Lösung

Try adding

{{ form.errors }}

somewhere to your template to see if there are any non-field errors !

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top