我安装了 django-cities-light 和 django-autocomplete-light。一切都很顺利,直到我试图阻止用户选择布鲁塞尔作为地区,如果他们进入法国作为国家。
我在这里按照教程进行操作:http: //django-autocomplete-light.readthedocs.org/en/v2/dependant.html但我不知何故错过了一个重要的细节。
自动完成应用程序工作正常,但没有限制。由于我不知道类媒体应该是什么,我猜问题可能是这样。此外,文档说它用于加载一些额外的 javascript 代码,并且由于应用程序运行正常,它似乎没有被加载。
这是我的models.py
from django.db import models
from user.models import User
from cities_light.models import City, Country, Region
class Offer(models.Model):
publisher = models.ForeignKey(User)
content = models.TextField()
date = models.DateTimeField(auto_now_add=True, auto_now=False)
country = models.ForeignKey(Country)
region = models.ForeignKey(Region)
city = models.ForeignKey(City)
autocomplete_light_registry.py
import autocomplete_light
from offers.models import Offer
from cities_light.models import Country, Region, City
class CountryAutocomplete(autocomplete_light.AutocompleteModelBase):
attrs={'placeholder': 'Choisissez votre Pays'}
search_fields=('search_names', 'alternate_names',)
def choices_for_request(self):
q = self.request.GET.get('q', '')
country_id = self.request.GET.get('country_id', None)
choices = self.choices.all()
if q:
choices = choices.filter(name_ascii__icontains=q)
if country_id:
choices = choices.filter(country_id=country_id)
return self.order_choices(choices)[0:self.limit_choices]
autocomplete_light.register(Country, CountryAutocomplete)
class RegionAutocomplete(autocomplete_light.AutocompleteModelBase):
attrs={'placeholder': 'region name ..'}
search_fields=('search_names', 'alternate_names',)
def choices_for_request(self):
q = self.request.GET.get('q', '')
country_id = self.request.GET.get('country_id', None)
choices = self.choices.all()
if q:
choices = choices.filter(name_ascii__icontains=q)
if country_id:
choices = choices.filter(country_id=country_id)
return self.order_choices(choices)[0:self.limit_choices]
autocomplete_light.register(Region, RegionAutocomplete)
class CityAutocomplete(autocomplete_light.AutocompleteModelBase):
attrs={'placeholder': 'Choisissez votre ville'}
search_fields=('search_names', 'alternate_names',)
def choices_for_request(self):
q = self.request.GET.get('q', '')
city_id = self.request.GET.get('city_id', None)
choices = self.choices.all()
if q:
choices = choices.filter(name_ascii__icontains=q)
if city_id:
choices = choices.filter(country_id=country_id)
return self.order_choices(choices)[0:self.limit_choices]
autocomplete_light.register(City, CityAutocomplete)
表格.py
from django import forms
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from offers.models import Offer, LevelChoices, CoursChoices
import autocomplete_light
class SendOfferForm(autocomplete_light.ModelForm):
class Media:
js = ('dependant_autocomplete.js',)
class Meta:
model = Offer
autocomplete_fields = ("country", "region", "city")
fields = ('country', 'region', 'city', 'content')
编辑
我发现如果我删除 autocomplete_light_registry.py 中 attrs 字段之后的所有代码,则没有任何变化。django 无法识别某些内容。我检查了我的 settings.py,但似乎还可以
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '/media/')