我有一个名为“ Accounts”的Django应用程序,该应用程序扩展了Django默认用户模型。我希望每个用户都能够更新其帐户,该帐户是使用generic.UpdateView称为UserProfile的模型。转到UpdateView URL时,出现以下错误:
BootstrapError,位于/ accounts / user-profile / 5 / edit / 参数“ form”应包含有效的Django Form。
我在下面包含了我的代码。谢谢!
models.py
from django.db import models
from django.contrib import auth
# Create your models here.
class User(auth.models.User,auth.models.PermissionsMixin):
readonly_fields = ('id','pk')
def __str__(self):
return self.username
class UserProfile(models.Model):
user = models.OneToOneField(auth.models.User,on_delete=models.CASCADE)
join_date = models.DateTimeField(auto_now=True)
profile_pic = models.ImageField(upload_to='profile_pics',default='media/block-m.png')
skills = models.TextField()
major = models.CharField(max_length=128)
grad_year = models.CharField(max_length=4)
clubs = models.TextField() #make FK to Clubs
opt_in = models.BooleanField(default=True)
def __str__(self):
return self.user.username
forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import UserProfile
from django import forms
class UserCreateForm(UserCreationForm):
class Meta:
model = get_user_model()
fields = ('first_name','last_name','username','email','password1','password2')
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
# self.fields['username'].label = 'Username'
# self.fields['email'].label = 'Email Address'
# self.fields['password1'].label = 'Password'
# self.fields['password2'].label = 'Confirm Password'
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('profile_pic','grad_year','opt_in')
views.py
from django.shortcuts import render,redirect
from django.contrib import messages
from django.contrib.auth.mixins import(
LoginRequiredMixin,
PermissionRequiredMixin
)
from django.urls import reverse,reverse_lazy
from django.db import IntegrityError
from django.shortcuts import get_object_or_404
from django.views import generic
from .models import User,UserProfile
from .forms import UserCreateForm,UserProfileForm
import easygui
from . import models
from . import forms
# Create your views here.
class ListAccounts(generic.ListView):
model = models.UserProfile
class DetailAccounts(generic.DetailView):
model = models.UserProfile
class UpdateAccounts(generic.UpdateView):
fields = ('grad_year',)
model = models.UserProfile
def SignUp(request):
registered=False
if request.method == "POST":
user_create_form = UserCreateForm(data=request.POST)
user_profile_form = UserProfileForm(data=request.POST)
if user_create_form.is_valid() and user_profile_form.is_valid():
user = user_create_form.save()
user_profile = user_profile_form.save(commit=False)
user_profile.user = user
if 'profile_pic' in request.FILES:
user_profile.profile_pic = request.FILES['profile_pic']
user_profile.save()
registered = True
return redirect('/accounts/login/')
else:
print(user_create_form.errors,user_profile_form.errors)
else:
user_create_form = UserCreateForm()
user_profile_form = UserProfileForm()
return render(request,'accounts/signup.html',
{'user_create_form':user_create_form,
'user_profile_form':user_profile_form,
'registered':registered})
urls.py
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
path('login/',auth_views.LoginView.as_view(template_name='accounts/login.html'),name='login'),
path('logout',auth_views.LogoutView.as_view(),name='logout'),
path('',views.ListAccounts.as_view(),name='all'),
path('signup/',views.SignUp,name='signup'),
path('user-profile/<int:pk>/',views.DetailAccounts.as_view(),name='detail'),
path('user-profile/<int:pk>/edit/',views.UpdateAccounts.as_view(),name='user_update'),
path('match/',views.Match.as_view(),name='match'),
]
userprofile_form.html
{% extends 'base.html' %}
{% load bootstrap4 %}
{% block content %}
<div class="container login">
<h2>Update</h2>
<form action="{% url 'accounts:all' %}" enctype="multipart/form-data" method="POST">
{% csrf_token %}
{% bootstrap_form user_profile_form layout='inline' %}
<input type="submit" class="btn" style="background:#f86041; color:white;" value="Update">
</form>
</div>
{% endblock %}
答案 0 :(得分:2)
我将尝试解释为解决此问题而采取的步骤,以便您更好地了解如何自己解决问题:
首先,您需要了解的是为什么会引发错误。这是一个BootstrapError
而不是标准错误,因此您需要查看django-bootstrap4
的源代码以了解为什么会引发该错误。查看源代码(只需搜索),您会发现:
if not isinstance(form, BaseForm):
raise BootstrapError('Parameter "form" should contain a valid Django Form.')
尝试并遍历从调用模板标签的位置到出现此错误的代码(在这里查看回溯可能也对您有所帮助),以了解所有各个变量都相等。
在您的情况下,form
等于user_profile_form
,并且由于user_profile_form
不是BaseForm
的实例(从{{1}导入)而引发错误}。因此,您可以返回查看django.forms.forms
。好吧,它是user_profile_form
的实例,而该实例又是UserProfileForm
的子类。但是ModelForm
是ModelForm
的子类。嗯...这似乎有点奇怪。为什么BaseForm
不是user_profile_form
的实例。
在这一点上,首先检查拼写错误之类的东西吗?也许检查您的中间件是否可以完全使用该表单。另外,尝试简化事物,仅渲染表单BaseForm
...您会发现这不起作用。所以问题似乎出在{{ user_profile_form }}
上?
啊哈...我们已经将user_profile_form
传递给视图user_profile_form
中的模板。 但这不是我们在这里看到的视图!我们正在考虑的视图是SignUp
视图。这是UpdateAccounts
,如果我们查看django文档(总是一个好主意)并查看示例,我们将看到表单像generic.UpdateView
一样传递到模板。
替换行:
form
使用
{% bootstrap_form user_profile_form layout='inline' %}
在模板 {% bootstrap_form form layout='inline' %}
中,它应该都可以顺畅地工作:)
如您所见,在偶然发现(也许更明显)正确的解决方案之前,我走了几条小巷。但我希望这会有所帮助。