添加一个“+”按钮,在我的 Django 模型表单中打开一个弹出窗口

Add a "+" button that opens a popup inside my django model form

我想添加一个“+”按钮,用于在我的 Django 模型表单中打开一个弹出窗口。对此有什么想法吗?

我的代码在models.py

class library(models.Model):
    book_name = models.CharField(max_length=50, null=True, blank=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, limit_choices_to={'is_superuser': True}, null=True,
                               blank=True)
    description = models.TextField(null=True, blank=True)
    duration = models.DurationField(null=True, blank=True)
    date = models.DateField()
    book_image = models.ImageField(upload_to='lib/book_img', null=True, blank=True)

与作者字段平行,我想添加一个“+”按钮(添加新作者)以打开弹出模式表单。

我的forms.py是

class library_details(forms.ModelForm):
    class Meta:
        model = library
        fields = "__all__"

您可以使用 bootstrap 模式。

https://getbootstrap.com/docs/4.0/components/modal/

您只需添加一个空白的表单域,但设置打开模式所需的属性。

类似下面的内容

注意:我没有测试过这个。

class library_details(forms.ModelForm):
    checkbox = forms.CheckboxInput()

    class Meta:
        model = library
        fields = "__all__"

    def __init__(self, *args, **kwargs):
        super(library_details, self).__init__(*args, **kwargs)
        self.fields['checkbox'].widget.attrs['data-toggle'] = "modal"
        self.fields['checkbox'].widget.attrs['data-target'] = "#exampleModal"