我已经安装了" Django自动完成灯"现在学习本教程:http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html
到目前为止,这是我的代码:
setting.py
INSTALLED_APPS = [
'dal',
'dal_select2',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.gis',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'directory',
'geopy',
]
models.py
class Company(models.Model):
CATEGORY_CHOICES = (
('P', 'Parrucchiere'),
('Ce', 'Centro estetico'),
)
self_key = models.ForeignKey('self', null=True, blank=True, related_name='related_self_key_models')
city = models.ForeignKey(City, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
address = models.CharField(max_length=255)
telephone = models.CharField(max_length=200)
category = models.CharField(max_length=1, choices=CATEGORY_CHOICES, null=True)
email = models.CharField(max_length=255, null=True, blank=True)
vat = models.CharField(max_length=200, null=True, blank=True)
location = gis_models.PointField(u"longitude/latitude", geography=True, blank=True, null=True)
gis = gis_models.GeoManager()
objects = models.Manager()
slug = AutoSlugField(populate_from='name', null=True)
def __str__(self):
return self.name
views.py
class CompanyAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
# Don't forget to filter out results depending on the visitor !
if not self.request.user.is_authenticated():
return Company.objects.none()
qs = Company.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
forms.py
class AddCompanyForm(forms.ModelForm):
vat = forms.CharField(required=True)
class Meta:
model = Company
fields = ('name','city','address','telephone','email','vat','self_key')
widgets = {
'self_key': autocomplete.ModelSelect2(url='company-autocomplete')
}
def clean_vat(self):
vat = self.cleaned_data['vat']
if Company.objects.filter(vat__iexact=vat).count() > 1:
raise ValidationError("L'attività digitata esiste già...")
return vat
urls.py
url(
r'^company-autocomplete/$',
autocomplete.Select2QuerySetView.as_view(model=Company),
name='company-autocomplete'
),
HTML
{% extends 'base.html' %}
{% load static %}
{% block title %}
<title>Aggiungi azienda | ebrand directory</title>
{% endblock title %}
{% block upper_content %}
<br style="line-height:25px"/>
<div class="google-card-full" style="width:auto; height:auto; text-align:center;">
<br style="line-height:25px"/>
<h1 class='title'><img style="width:auto; height:auto;" src='{% static "images/new_company.png" %}'> Aggiungi azienda</h1>
<hr/>
<br/>
<form method="post">
{% csrf_token %}
<table class='form-group' style="width:25%; margin:auto;">
{% for field in form.visible_fields %}
<tr>
<th style='text-align:left'>{{ field.label|capfirst }}:</th>
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
<td style='text-align:right'>{{ field }}</td>
</tr>
{% endfor %}
</table>
<br/>
<button class="btn btn-raised btn-primary" type="submit"
style="background-color:#1A88B9;">Aggiungi</button>
</form>
<p style="height:5px"/>
<p><b>ATTENZIONE</b>: ebrand<sup>©</sup> si riserva di verificare l'autenticità dei dati<br/>
prima dell'inserimento dell'azienda all'interno del database.</p>
<p style="height:5px"/>
</div>
<h2 style='margin:0px 0px 15px 25px;'>Oppure...</h2>
<div class="google-card-full" style="width:auto; height:auto; text-align:center;">
<br style="line-height:25px"/>
<h1 class='title'><img style="width:auto; height:auto;" src='{% static "images/new_company.png" %}'> Reclama azienda esistente</h1>
<hr/>
<br/>
<form method="post">
{% csrf_token %}
<table class='form-group' style="width:25%; margin:auto;">
{% for field in form.visible_fields %}
<tr>
<th style='text-align:left'>{{ field.label|capfirst }}:</th>
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
<td style='text-align:right'>{{ field }}</td>
</tr>
{% endfor %}
</table>
<br/>
<button class="btn btn-raised btn-primary" type="submit"
style="background-color:#1A88B9;">Aggiungi</button>
</form>
<p style="height:5px"/>
</div>
{% endblock %}
{% block middle_content %} {% endblock %}
{% block content %} {% endblock %}
{% block bottom_content %} {% endblock %}
问题:&#39; self_key&#39;字段未填充,我也无法输入(不是输入)。
我已经看过这个问题而没有结果:Django autocomplete light: field not populated