Форма Django отображается в шаблоне как имя переменной формы

Я создал пользовательскую модель пользователя в Django и пытаюсь создать форму входа в систему. Вместо формы, отображаемой в шаблоне Django, отображается login_form, имя переменной.

forms.py

class ProfileLogin(forms.ModelForm):
    password = forms.CharField(label="Password", widget=forms.PasswordInput)

    class Meta:
        model = Profile
        fields = ("username", "password")

    def clean(self):
        username = self.cleaned_data.get('username', None)
        password = self.cleaned_data.get('password', None)
        if not authenticate(username=username, password=password):
            raise forms.ValidationError("Invalid username and/or password.")
        return None

views.py

def login_views(request):
    context = {}
    user = request.user
    if user.is_authenticated:
        return redirect('index')

    if request.POST:
        login_form = ProfileLogin(request.POST)
        if login_form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(username=username, password=password)

            if user:
                login(request, user)
                return redirect('index')

    else:
        login_form = ProfileLogin()

    context['login_form'] = ['login_form']
    return render(request, "capstone/login.html", context)

html

<form method="post">
        {% csrf_token %}
        {% for field in login_form %}
        <h5>{{ field.label_tag }}
            {{ field }}
            {% if field.help_text %}
                <span>{{ field.help_text }}</span>
            {% endif %}

                {% for error in field.errors %}
                    <p>{{ error }}</p>
                {% endfor %}
            {% if login_form.non_field_errors %} 
                <div>{{ login_form.non_field_errors }}</div>
            {% endif %}
        </h5>
    {% endfor %}
        <button type="submit">Login</button>
      </form>

Я попробовал изменить

{% for field in login_form %}
        <h5>{{ field.label_tag }}
            {{ field }}

на {{ login_form }}, но происходит то же самое. На веб-странице появляется имя login_form, а не реальная форма.

Вам нужно изменить представление, чтобы добавить форму в контекст, в настоящее время вы передаете одну строку в списке.

context['login_form'] = ['login_form']

Необходимо:

context['login_form'] = login_form
Вернуться на верх