BootstrapError,参数 “form” 应包含有效的 Django 表单

huangapple go评论59阅读模式
英文:

BootstrapError, Parameter "form" should contain a valid Django Form

问题

在你的signup.html文件中,你尝试使用{% bootstrap_form form layout='inline' %}来设置Bootstrap表单的内联布局,但遇到了错误。要解决这个错误,你可以尝试以下步骤:

  1. 确保你的项目已经正确集成了Bootstrap 4,并且你的bootstrap4模板标签库已经加载。你可以检查base_generic.html文件,确保Bootstrap的CSS和JavaScript文件已经正确引入。

  2. 确保你的form变量在模板中是有效的Django表单。在signup.html中,确保form是来自SignupForm的实例。你可以在模板中添加一个调试输出来确认form的内容:

    {{ form }}
    

    这将显示表单的内容,以便你确认它是一个有效的Django表单。

  3. 如果你仍然遇到问题,可以尝试在form标签中明确指定form的布局,而不是在模板标签中设置。在<form>标签中添加class属性来设置内联布局,如下所示:

    <form method="post" class="form-inline">
    

    这将为表单应用内联布局。

遵循这些步骤应该能够帮助你解决这个错误,并在signup.html中使用Bootstrap 4的内联表单布局。

英文:

Views.py

from django.shortcuts import redirect, render
from .forms import SignupForm
from django.contrib.auth import authenticate, login


# Create your views here.
def sign_up(request):
    if request.method == &#39;POST&#39;:
        form = SignupForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data(&#39;username&#39;)
            password = form.cleaned_data(&#39;password1&#39;)
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect(&#39;/accounts/profile&#39;)
    else:
        form = SignupForm()
        
    context = {form: &#39;form&#39;}
    return render(request, &#39;registration/signup.html&#39;, context)

form.py

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


class SignupForm(UserCreationForm):
    class Meta:
        model = User
        fields = (&#39;username&#39;, &#39;email&#39;, &#39;password1&#39;, &#39;password2&#39;)

signup.html

{% extends &quot;base_generic.html&quot; %}
{% load bootstrap4 %}

{% block content %}

  &lt;form method=&quot;post&quot; &gt;
    {% csrf_token %}
    {% bootstrap_form form %}
    &lt;input type=&quot;submit&quot; value=&quot;signup&quot; class=&quot;boxed-btn3 w-100&quot;&gt;
  &lt;/form&gt;

{% endblock %}

why get error: raise BootstrapError('Parameter "form" should contain a valid Django Form.' , although it works on the login.html page

login.html

{% extends &quot;base_generic.html&quot; %}
{% load bootstrap4 %}

{% block content %}

  {% if form.errors %}
    &lt;p&gt;Your username and password didn&#39;t match. Please try again.&lt;/p&gt;
  {% endif %}

  {% if next %}
    {% if user.is_authenticated %}
      &lt;p&gt;Your account doesn&#39;t have access to this page. To proceed,
      please login with an account that has access.&lt;/p&gt;
    {% else %}
      &lt;p&gt;Please login to see this page.&lt;/p&gt;
    {% endif %}
  {% endif %}
  
  &lt;form method=&quot;post&quot; action=&quot;{% url &#39;login&#39; %}&quot;&gt;
    {% csrf_token %}
    {% bootstrap_form form %}
    &lt;input type=&quot;submit&quot; value=&quot;login&quot; class=&quot;boxed-btn3 w-100&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;next&quot; value=&quot;{{ next }}&quot;&gt;
  &lt;/form&gt;

  {# Assumes you setup the password_reset view in your URLconf #}
  &lt;p&gt;&lt;a href=&quot;{% url &#39;password_reset&#39; %}&quot;&gt;Lost password?&lt;/a&gt;&lt;/p&gt;

{% endblock %}

I tried use {% bootstrap_form form layout='inline' %} but not worked.
How can I solve this error
I use bootstrap 4

答案1

得分: 1

更新context字典,你混淆了键和值,所以:

context = {'form': form}
英文:

Update context dict, you confused between key and values so:

context = {&#39;form&#39;: form}

huangapple
  • 本文由 发表于 2023年2月27日 06:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定