Search code examples
djangodjango-formsdjango-widget-tweaks

Django formset not rendering with django-widget-tweaks


I am getting the error 'LoginForm' object has no attribute 'as_widget' whenever I use formset. I really do not know what is the problem as the forms renders properly with normal Django forms. I am trying to see what characteristics in the formset is giving this problem with django-widget-tweaks, but up until now it is hard to figure out. I am getting the error at {% render_field field class="form-control" placeholder=field.label %} in the HTML code.

forms.py:

class LoginForm(ModelForm):

user_login = forms.HiddenInput()

prefix = 'prefix_login'

class Meta:
    model = Usermie

    fields = ['email', 'password']
    widgets = {'password': forms.PasswordInput(),
               'email': forms.EmailInput()}

views.py

def manage_articles(request):
    article_formset = formset_factory(LoginForm)
    book_formset = formset_factory(SignUpForm)
    if request.method == 'POST':
        if 'login' in request.POST:
            login = article_formset(request.POST, request.FILES, prefix='login')

            if login.is_valid():
                email = request.POST.get('prefix_login-email', '')
                password = request.POST.get('prefix_login-password', '')

                # Return a user_obj object if the username and password are valid
                # otherwise it will return null, the null variable is called None in python
                user_obj = auth.authenticate(email=email, password=password)
                # return HttpResponse("inside form if condition")
                if user_obj is not None:
                    if user_obj.is_active:
                        login_usermie(request, user_obj)
                        return HttpResponseRedirect('/home/')
                    else:
                        # pass
                        return HttpResponse("Your account is inactive.")

        elif 'signup' in request.POST:
            signup = book_formset(request.POST, request.FILES)
            if signup.is_valid():
                pass
    else:
        login = article_formset
        signup = book_formset
        return render(request, 'usermie/formtest.html', {
            'login': login,
            'signup': signup,
        })

HTML:

<div class="navbar navbar-default nav-links navbar-static-top page-nav">
<div class="container">
    <a class="mini-navbar navbar-brand" href="/">
        <img src="http://i.imgur.com/GAQSCtB.png" width="25"
             alt="Driven Car Sales Logo"
             class="img-rounded logo-nav mini-navbar" />
    </a>
    <ul class="nav navbar-nav nav-form-out pull-right">
        <li>
            <form class="navbar-form navbar-form-out login" action="" method="POST">
                {% csrf_token %}
                {% load widget_tweaks %}
                {% for field in login %}
                    {% if login.errors %}
                        <div class="form-group">
                            <label class="sr-only" for="{{ field.auto_id }}">{{ field.label }}</label>

                            {% render_field field class="form-control" placeholder=field.label %}
                            {% if field == login.password %}
                                {% for hidden in field.hidden_fields %}
                                    {{ hidden }}
                                {% endfor %}
                            {% endif %}
                            <div class="help-block with-errors">{{ field.errors }}</div>
                        </div>
                    {% else %}
                        <div class="form-group">
                            <label class="sr-only" for="{{ field.auto_id }}">{{ field.label }}</label>

                            {% render_field field class="form-control" placeholder=field.label %}

                            {% if field == login.password %}
                                {% for hidden in field.hidden_fields %}
                                    {{ hidden }}
                                {% endfor %}
                            {% endif %}
                        </div>
                    {% endif %}
                {% endfor %}
                <div class="checkbox">
                    <label>
                        <input type="checkbox"> Remember me
                    </label>
                </div>
                <button type="submit" name="action" value="login" class="btn btn-default">Sign in</button>
            </form>
       </li>
    </ul>
</div>

<form class="signup" method="POST" action="">
{% csrf_token %}
{{ signup.as_p }}   
<button name='action' value='signup' type="submit">Sign up</button>
</form>

Solution

  • login variable in template is a formset. So when you do following:

    {% for field in login %}
         {% render_field field class="form-control" placeholder=field.label %}
    {% endfor %} 
    

    you have form as field value and not a field. Try do this:

    {% for form in login %}
         {% for field in form %}
             {% render_field field class="form-control" placeholder=field.label %}
         {% endfor %} 
    {% endfor %}