【问题标题】:autocomplete-light for adding popup outside the adminautocomplete-light 用于在管理员之外添加弹出窗口
【发布时间】:2015-06-01 12:52:46
【问题描述】:

我正在使用 django-crispy-forms 并想使用 autocomplete-light 但无法启动。如果他们想要的设施不存在,我需要用户能够创建一个新设施。 我只是不知道如何使用 autocomplete-light,而且我已经苦苦挣扎了好几天。有人可以指出我正确的方向吗??

models.py

class CollectionFacility(TimeStampedModel):
    """
    Data collection facility.
    """

    facility_name = models.CharField(max_length=256, blank=False)
    address_line1 = models.CharField("Address line 1", max_length=45)
    address_line2 = models.CharField("Address line 2", max_length=45, blank=True)
    country = models.CharField(max_length=50, blank=False)
    state_province = models.CharField(max_length=100, blank=True)
    city = models.CharField(max_length=100, blank=False)
    postal_code = models.CharField("Postal Code", max_length=20, blank=True)
    facility_contact = models.ForeignKey('FacilityContact', related_name='collection_facilities', null=True, blank=True)

    def __unicode__(self):
        return "%s, %s" %  (self.facility_name, self.country)

    class Meta:
        ordering = ['country', 'facility_name', 'city', 'state_province']
        verbose_name = "Collection Facility"
        verbose_name_plural = "Collection Facilities"

class FacilityContact(TimeStampedModel):
    TITLES = (
        ('Mrs.', 'Mrs.'),
        ('Ms.', 'Ms.'),
        ('Mr.', 'Mr.'),
        ('Dr.', 'Dr.'),
    )

    first_name = models.CharField(max_length=256, blank=False)
    middle_initial = models.CharField(max_length=4, blank=True)
    last_name = models.CharField(max_length=256, blank=False)
    title = models.CharField(max_length=4, choices=TITLES, blank=True)
    email = models.EmailField(blank=False)

    def __unicode__(self):
        return "%s, %s" %  (self.last_name, self.first_name)

    class Meta:
        ordering = ['last_name', 'first_name']
        verbose_name = "Facility Contact"
        verbose_name_plural = "Facility Contacts" 

forms.py

class FacilityForm(autocomplete_light.ModelForm):
    class Meta:
        model = CollectionFacility

views.py

facility_form = FacilityForm()
# pass it in the context to template
....

模板.html

{% crispy facility_form %}

【问题讨论】:

  • django-autocomplete-light 是否在您不做脆皮表格时工作?您是否尝试过一个基本示例(例如 django-autocomplete-light 的文档)?

标签: django-forms django-crispy-forms django-autocomplete-light


【解决方案1】:

你检查non_admin_add_another example app了吗?

Docs about that one 尚未移植到 v2,这意味着文档中的代码可能无法正常工作。 但是请注意autocomplete_light.example_apps.non_admin_add_another 应该可以工作。

我建议您直接在 autocomplete_light 的 test_project 中开始摆弄该示例,请参阅:http://django-autocomplete-light.readthedocs.org/en/stable-2.x.x/demo.html

【讨论】:

猜你喜欢
  • 2016-09-10
  • 2023-04-09
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 2012-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多