0

So my problem is that even though I have created the forms from my model and provided my views with those forms, the related template is not displaying any form:

The following is my forms.py :

from django import forms
from django.contrib.auth.models import User
from .models import Account

class UserUpdateForm(forms.ModelForm):
    email = forms.EmailField(max_length=100)

    class Meta:
        model = User
        fields = ['username', 'email']


class AccountUpdateForm(forms.ModelForm):

    class Meta:
        model= Account
        fields = ['image']

And the next one is my views.py:

from .forms importUserUpdateForm, AccountUpdateForm

def account(request):
    user_update_form = UserUpdateForm()
    profile_update_form = AccountUpdateForm()

    return render(request, 'blog/profile.html', {
        'user_update_form':user_upate_form,
        'profile_update_form':profile_update_form
    })

But the following template does not show any form

{% extends './base.html' %}
{% load crispy_forms_tags %}

{% block content %}

<div class="row">
    <div class="col-12  d-flex flex-column justify-content-center align-items-start">
        <img src="{{ user.account.image.url }}" alt="" class="user-profile-pic">
        <label for="user-profile-pic-input">Choose an image</label>
        <input type="file" class="form-control-files w-100" id="user-profile-pic-input" name='user-profile-pic-input'>
    </div>
</div>
<div class="row">
    <div class="col-12">
        <form method="POST">
            {% csrf_token %}
            {{ user_update_form }}
            {{ profile_update_form }}
            <input type="submit" value="Save changes!" class="btn btn-info btn-block">
        </form>
    </div>
</div>

{% endblock %}
RZAMarefat
  • 133
  • 8

1 Answers1

0

I had this problem just yesterday. It worked when i called the form class without brackets ()

Try this:

user_update_form = UserUpdateForm
profile_update_form = AccountUpdateForm

If this doesn't work you should probably go down the formset path (inlineformset_factory). It's used to connect modelforms linked by a foreign key. It works with one-to-one and one-to-many relationships.

Beikeni
  • 832
  • 7
  • 17
  • That solution did not work. Actually if it did, it would be not useful because later on I will likely want to pass instance to them. How can I customise formset?Could you please demonstrate? – RZAMarefat Nov 29 '20 at 13:20
  • I have recently answered this very same question, you can find it here. At the end of my answer you'll find a couple of really useful links treating the same topic. Good luck! https://stackoverflow.com/questions/64998608/inlineformset-factory-saving-parent-without-child-and-not-displaying-validation/65005803#65005803 – Beikeni Nov 29 '20 at 13:23
  • Still no result after 2 days and nights of wasting time, staring at the screen, struggling for debuging – RZAMarefat Nov 29 '20 at 14:11
  • Do you see the rest of your html? Just the forms missing? Could you add print(user_update_form) in your views.py to debug step-by-step – Razenstein Nov 29 '20 at 14:34
  • 1
    Then next step: if you check in the browser your page source code, is there something within the
    ...
    ?
    – Razenstein Nov 29 '20 at 14:42