我安装了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)
forms.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/')
答案 0 :(得分:0)
将
通过管理界面访问新添加的应用