gpt4 book ai didi

python - Django - 脆皮表格可以分成两列吗?

转载 作者:行者123 更新时间:2023-12-01 01:01:28 26 4
gpt4 key购买 nike

我试图将一个脆脆的布局表单分成两列,以尝试消除滚动的需要,我尝试在 forms.py 中使用 formhelper 将 2 个问题放入 DIV 中,但这并没有改变任何东西。有人有什么想法吗?

forms.py

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Field, Row, Submit, Button, Column

class ProfileUpdateForm(forms.ModelForm):
address = forms.CharField()
dob = forms.DateField(
widget=forms.TextInput(
attrs={'type': 'date'}
), label='Date of Birth'
)
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.layout = Layout(
Div(
Div(Field('fullname'), css_class='col-md-6',),
Div(Field('dob'), css_class='col-md-6',),
css_class='row',
),
Div(
Div(Field('address'), css_class='col-md-6',),
Div(Field('city'), css_class='col-md-6',),
css_class='row',
),
Div(
Div(Field('country'), css_class='col-md-6',),
Div(Field('profilephoto'), css_class='col-md-6',),
css_class='row',
),
)
super(ProfileUpdateForm, self).__init__(*args, **kwargs)
class Meta:
model = Profile
fields = ['fullname', 'dob', 'address', 'city', 'country', 'profilephoto']
labels = {
'fullname': 'Full Name',
'address': 'Address',
'city': 'City',
'country': 'Country',
'profilephoto': 'Profile Photo',
}

views.py

@login_required
def profile(request):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)

if u_form.is_valid and p_form.is_valid:
u_form.save()
p_form.save()
messages.success(request, f'Your account has been successfully updated!')
return redirect('profile')
else:
u_form = UserUpdateForm(instance = request.user)
p_form = ProfileUpdateForm(instance = request.user.profile)

context = {
'u_form': u_form,
'p_form': p_form
}
return render(request, 'users/profile.html', context)

profile.html

{% load crispy_forms_tags %}

<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ u_form|crispy }}

{{ p_form|crispy }}

</form>

当前状态

Current look of the page

编辑将 __init__ 添加到 forms.py

最佳答案

事实上,我什至不确定你的 helper 是否被接走了。基于documentation here formhelper 应添加到 __init__ 处。如果您不使用 {% Crispy your_cool_form_name %} ,您的问题也可能出在您的模板中。

你就快到了。你是looking for Field .

from crispy_forms.layout import Field

[...]


Div(
Div(Field('fullname'), css_class='col-md-6',),
Div(Field('dob'), css_class='col-md-6',),
css_class='row',
),

关于python - Django - 脆皮表格可以分成两列吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55765703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com