简体   繁体   中英

im having trouble Django materialize css form when a render the form i get the variable does not exist from field.html from django-materializecss-form

I'm new to Django. whenever I render the form I get "Exception has occurred: VariableDoesNotExist Failed lookup for key [required_css_class] in ". I don't understand this error if anybody explain or tell me what I'm doing wrong will be much appreciated

thank you in advance

this is my view

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} )

this is my modelform

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']

this is my model

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)

here is where the error points to. this is what it said "Exception has occurred: VariableDoesNotExist Failed 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>

this is my 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' }}

I think the problem is that you are overriding the variable "form" by passing it to your template with the line:

return render(request, 'b2b/B2B_notes.html',{'form':form} )

Try renaming your variable to something other than 'form', such as:

return render(request, 'b2b/B2B_notes.html',{'my-form':form} )

Make sure to also rename the variable in your html in the last 3 lines where you call {{ form }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM