當我更改模型「位置」中的兩個字段時,我收到錯誤'bool'對象沒有屬性'startswith'。我試圖用Django的智能選擇(ChainedForeignKey)錯誤:當我使用django-smart-select時,'bool'對象沒有屬性'startswith'
from smart_selects.db_fields import ChainedForeignKey
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)
完整的錯誤文本: 'BOOL' 對象有沒有屬性 'startswith' 站點包\ crispy_forms \模板\ bootstrap3 \ uni_form.html,第4行 4行錯誤:
{% if include_media %}
{{ form.media }}
{% endif %}
請幫我解決茨艾倫錯誤。 謝謝!
class Media(object):
def __init__(self, media=None, **kwargs):
if media:
media_attrs = media.__dict__
else:
media_attrs = kwargs
self._css = {}
self._js = []
for name in MEDIA_TYPES:
getattr(self, 'add_' + name)(media_attrs.get(name, None))
# Any leftover attributes must be invalid.
# if media_attrs != {}:
# raise TypeError("'class Media' has invalid attribute(s): %s" % ','.join(media_attrs.keys()))
def __str__(self):
return self.render()
def render(self):
return mark_safe('\n'.join(chain(*[getattr(self, 'render_' + name)() for name in MEDIA_TYPES])))
def render_js(self):
return [format_html('<script type="text/javascript" src="{0}"></script>', self.absolute_path(path)) for path in self._js]
def render_css(self):
# To keep rendering order consistent, we can't just iterate over items().
# We need to sort the keys, and iterate over the sorted list.
media = sorted(self._css.keys())
return chain(*[
[format_html('<link href="{0}" type="text/css" media="{1}" rel="stylesheet" />', self.absolute_path(path), medium)
for path in self._css[medium]]
for medium in media])
def absolute_path(self, path, prefix=None):
if path.startswith(('http://', 'https://', '/')):
return path
if prefix is None:
if settings.STATIC_URL is None:
# backwards compatibility
prefix = settings.MEDIA_URL
else:
prefix = settings.STATIC_URL
return urljoin(prefix, path)
def __getitem__(self, name):
"Returns a Media object that only contains media of the given type"
if name in MEDIA_TYPES:
return Media(**{str(name): getattr(self, '_' + name)})
raise KeyError('Unknown media type "%s"' % name)
def add_js(self, data):
if data:
for path in data:
if path not in self._js:
self._js.append(path)
def add_css(self, data):
if data:
for medium, paths in data.items():
for path in paths:
if not self._css.get(medium) or path not in self._css[medium]:
self._css.setdefault(medium, []).append(path)
def __add__(self, other):
combined = Media()
for name in MEDIA_TYPES:
getattr(combined, 'add_' + name)(getattr(self, '_' + name, None))
getattr(combined, 'add_' + name)(getattr(other, '_' + name, None))
return combined
試試吧'self.helper = FormHelper(self)' –
並在助手之前移動到第一個初始行 –
我做到了這一點:def __init __(self,* args,** kwargs): super(LocationForm,self).__ init __(* args,** kwargs) self.helper = FormHelper(self) - 不幸的是 – delka