T
T
tanyaa2017-07-26 18:19:30
Django
tanyaa, 2017-07-26 18:19:30

How to fix 'bool' object has no attribute 'startswith'' error when using django_smart_selects?

The Location model used to have fields of the CharField type (commented out in the code)
Continent=models.CharField
Country=models.CharField
In the form, everything was displayed correctly and everything worked.
After trying to use django-smart-selects
Error appeared:
'bool' object has no attribute 'startswith'
site-packages\crispy_forms\templates\bootstrap3\uni_form.html, error at line 4
Line 4:
{% if include_media % }
{{ form.media }}
{% endif %}
error in highlighted line.
Please help me figure out what happened and how to fix it?

Models.py
class ListOfContinents(models.Model):

    id_Continent = models.AutoField(primary_key=True)
    Continent = models.CharField(max_length=200,unique=True) 

    def __str__(self):
        return '%s' % self.Continent

class ListOfCountries(models.Model):

    id_Country = models.AutoField(primary_key=True)
    Continent = models.ForeignKey(ListOfContinents, default=0, to_field='Continent',db_column='Continent')
    Country=models.CharField(max_length=200,unique=True)

    def __str__(self):
        return '%s' % self.Country



class Location(models.Model):

    #Continent=models.CharField(max_length=200,null=True, verbose_name='Континент')
    #Country=models.CharField(max_length=200,null=True, verbose_name='Страна')
    Continent=models.ForeignKey(ListOfContinents,to_field='Continent',db_column='Continent',verbose_name='Континент',null=True)
    Country = ChainedForeignKey(ListOfCountries,chained_field="Continent",
        chained_model_field="Continent",
        show_all=False,
        auto_choose=True,
        sort=True, 
        verbose_name='Страна',
        db_column='Country',
        to_field='Country',
        null=True)


Forms.py

class LocationForm (forms.ModelForm):
    class Meta:
        model = Location
        fields = ('Continent','Country')

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_id = 'id-unitForm'
        # self.helper.form_class = 'blueForms'
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-1'
        self.helper.field_class = 'col-lg-4'
        self.helper.form_method = 'post'
        # self.helper.form_action = 'submit_survey'

        self.helper.add_input(Submit('submit', 'Submit', css_class='btn btn-primary'))
        self.helper.add_input(Button('cancel', 'Cancel', css_class='btn-default', onclick="window.history.back()"))

        super(LocationForm , self).__init__(*args, **kwargs)

view.py
   
def Loc(request):
    if request.method == "POST":
        form_add = LocationForm(request.POST)
        if form_add.is_valid():
            
            Loc.save()
           
            return redirect('Loc_edit_full')
    else:
        form_add = LocationForm()
    return render(request, 'utest_base/Loc_add.html', {'form_add': form_add})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question