如何将滚动div直接添加到Django中的ModelForm中?

时间:2022-03-28 02:17:09

I have the following code, but it doesn't put the scrolling div anywhere. All the other combinations I've tried (such as placing the div around the table) results in some or other incorrect placement of the scroll bars. This is the html:

我有以下代码,但它没有将滚动div放在任何地方。我尝试过的所有其他组合(例如将div放在表的周围)都会导致滚动条的一些或其他错误位置。这是html:

<form action="" method="post">
  <table>
    <div style="overflow: auto; width : 200px; height : 50px;">
      {{ form }}
    </div>
    <tr><td colspan="2"><input type="submit" value="Save"/></td></tr>
  </table>
</form>

My forms look like this:

我的表格是这样的:

class GroupForm(forms.ModelForm):
    class Meta:
        model = Group

    def __init__(self, *args, **kwargs):
        super(GroupForm, self).__init__(*args, **kwargs)

        self.fields['permissions'].widget = forms.CheckboxSelectMultiple()
        self.fields['permissions'].choices = ((p.id, p.name) for p in Permission.objects.order_by("name"))

My views.py looks like this:

我的观点。py是这样的:

def group_edit(request, group_id, template_name="group_edit.html", extra_context=None, form_class=None):

    edit_group = get_object_or_404(Group, pk=group_id)
    extra_context = extra_context or {}
    form_class = form_class or GroupForm
    extra_context["edit_group"] = edit_group

    if request.method == "POST":
        form  = form_class(instance=edit_group, data=request.POST)
        if form.is_valid():
            form.save()
            return redirect(reverse("global_group_list"))
    else:
        form = form_class(instance=edit_group)

    extra_context["form"] = form

    return direct_to_template(
        request,
        template_name,
        extra_context=extra_context
    )

2 个解决方案

#1


2  

Put the DIV inside a table row like this

将DIV放在这样的表行中

<form action="" method="post">
  <table>
    <tr><td><div style="overflow: auto; width : 200px; height : 50px;">
      {{ form.as_p }}
    </div></td></tr>
    <tr><td colspan="2"><input type="submit" value="Save"/></td></tr>
  </table>
</form>

#2


0  

This isn't really a django problem, so the python code you posted really makes no difference. I think the issue is you want the table to have scroll bars, but keep the submit button separate?

这并不是一个真正的django问题,所以您发布的python代码实际上没有什么区别。我认为问题是你想要表格有滚动条,但是把提交按钮分开?

You have two immediate options. you can wrap {{ form }} in a second table, so you have two nested tables. Alternately you can move the div and submit button to be outside your table, so that the table is wrapped with the div.

你有两个直接的选择。可以在第二个表中包装{{form},这样就有了两个嵌套的表。另一种方法是将div和submit按钮移到表之外,以便表与div一起包装。

Either way what you have now won't work. any immediate child tag of a table tag, other than a tbody or a tr will be ignored.

不管怎样,你现在拥有的东西都行不通。除了tbody或tr之外,表标记的任何直接子标记都将被忽略。

#1


2  

Put the DIV inside a table row like this

将DIV放在这样的表行中

<form action="" method="post">
  <table>
    <tr><td><div style="overflow: auto; width : 200px; height : 50px;">
      {{ form.as_p }}
    </div></td></tr>
    <tr><td colspan="2"><input type="submit" value="Save"/></td></tr>
  </table>
</form>

#2


0  

This isn't really a django problem, so the python code you posted really makes no difference. I think the issue is you want the table to have scroll bars, but keep the submit button separate?

这并不是一个真正的django问题,所以您发布的python代码实际上没有什么区别。我认为问题是你想要表格有滚动条,但是把提交按钮分开?

You have two immediate options. you can wrap {{ form }} in a second table, so you have two nested tables. Alternately you can move the div and submit button to be outside your table, so that the table is wrapped with the div.

你有两个直接的选择。可以在第二个表中包装{{form},这样就有了两个嵌套的表。另一种方法是将div和submit按钮移到表之外,以便表与div一起包装。

Either way what you have now won't work. any immediate child tag of a table tag, other than a tbody or a tr will be ignored.

不管怎样,你现在拥有的东西都行不通。除了tbody或tr之外,表标记的任何直接子标记都将被忽略。