英文:
BootstrapError, Parameter "form" should contain a valid Django Form
问题
在你的signup.html
文件中,你尝试使用{% bootstrap_form form layout='inline' %}
来设置Bootstrap表单的内联布局,但遇到了错误。要解决这个错误,你可以尝试以下步骤:
-
确保你的项目已经正确集成了Bootstrap 4,并且你的
bootstrap4
模板标签库已经加载。你可以检查base_generic.html
文件,确保Bootstrap的CSS和JavaScript文件已经正确引入。 -
确保你的
form
变量在模板中是有效的Django表单。在signup.html
中,确保form
是来自SignupForm
的实例。你可以在模板中添加一个调试输出来确认form
的内容:{{ form }}
这将显示表单的内容,以便你确认它是一个有效的Django表单。
-
如果你仍然遇到问题,可以尝试在
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 == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data('username')
password = form.cleaned_data('password1')
user = authenticate(username=username, password=password)
login(request, user)
return redirect('/accounts/profile')
else:
form = SignupForm()
context = {form: 'form'}
return render(request, 'registration/signup.html', 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 = ('username', 'email', 'password1', 'password2')
signup.html
{% extends "base_generic.html" %}
{% load bootstrap4 %}
{% block content %}
<form method="post" >
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="signup" class="boxed-btn3 w-100">
</form>
{% 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 "base_generic.html" %}
{% load bootstrap4 %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="login" class="boxed-btn3 w-100">
<input type="hidden" name="next" value="{{ next }}">
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% 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 = {'form': form}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论