我是Django的新手,每当我渲染表单时,我都会得到 "Exception has occurred.VariableDoesNotExistFailed lookup for key [required_css_class] in " 。VariableDoesNotExistFailed lookup for key [required_css_class] in " 。我不明白这个错误,如果有人解释或告诉我我做错了什么,将非常感激。
先谢谢你
我是这样认为的
def considerations(request):
if request.method == "POST":
form = B2bConsideration(request.POST)
v = form.is_valid()
if form.is_valid():
instance = form.save(commit=True)
#adding date to instance from request not in table but a good idea
#instance.date = request.date
instance.save()
return HttpResponseRedirect(reverse('b2b:TypeOfTitle'))
else:
return HttpResponse(form.errors)
else:
form = B2bConsideration()
return render(request, 'b2b/B2B_notes.html',{'form':form} )
这是我的模型表格
class B2bConsideration(ModelForm):
CHOICES = [('yes_title_under_name', 'yes'),('No_title_under_name','no'),]
under_name_title = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect())
class Meta:
model = Consideration
fields = ['under_name_title','salvage_title','is_title_paidoff']
这是我的模型
under_Name_choices = [('yes_title_under_name', 'yes'),('No_title_under_name','no'),]
salvage_title_choices =[('yes_salvage_title','yes'),('no_salvage_title','no'),]
is_title_paidoff_choices = [('yes_title_paidoff', 'yes'),('no_title_paidoff','no'),]
class Consideration (models.Model):
under_name_title = models.CharField(max_length=21, choices=under_Name_choices)
salvage_title = models.CharField(max_length=18, choices=salvage_title_choices)
is_title_paidoff = models.CharField(max_length=21, choices=is_title_paidoff_choices)
这里是错误指向的地方,它是这样说的 "Exception has occurred.VariableDoesNotExistFailed lookup for key [required_css_class] in " 异常发生。VariableDoesNotExistFailed lookup for key [required_css_class] in " 变量不存在,查找失败。
<label class="control-label {{ classes.label }} {% if field.field.required %}{{ form.required_css_class }}{% endif %}">{{ field.label }}</label>
这是我的HTML
{% load static %}
{% load materializecss %}
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Titlemax B2B</title>
{% block css %}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
{% endblock css %}
{% block javascript %}
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
{% endblock javascript %}
<script src="{% static 'B2B_index.js' %}"></script>
<link rel="stylesheet" href="{% static 'B2B_index.css' %}">
</head>
{{ form.under_name_title.0}}
{{ form.under_name_title.1}}
{{ form|materializecss:'m6' }}
我认为问题在于你通过将变量 "form "传递到你的模板中,覆盖了它。
return render(request, 'b2b/B2B_notes.html',{'form':form} )
试着把你的变量重命名为 "form "以外的其他变量,比如:
return render(request, 'b2b/B2B_notes.html',{'my-form':form} )
确保在你的html中重命名变量 在你调用{{form }}的最后三行中。