我正在尝试通过forms.py编辑表单的布局。但是,如果我尝试进行设置,则不会发生任何事情,并且表单布局会保留默认布局。
这是我的models.py:
class AssetReceived(models.Model):
asset_user = models.ForeignKey(no_models.MyUser,on_delete=models.CASCADE, related_name='my_user_profile2')
asset_received = models.ForeignKey(Asset, on_delete=models.CASCADE)
asset_servicetag = models.CharField(blank=True, max_length=7)
asset_date = models.DateField(default=datetime.date.today)
asset_status = models.ForeignKey(AssetStatus, on_delete=models.CASCADE)
asset_note = models.CharField(blank=True, max_length=250)
asset_order_type = models.ForeignKey(AssetOrderType, on_delete=models.CASCADE)
asset_order_refresh = models.ForeignKey(AssetStoreMaster, on_delete=models.CASCADE, blank=True, null=True, related_name='sc_refresh')
asset_order_install = models.ForeignKey(AssetStoreMaster, on_delete=models.CASCADE, blank=True, null=True, related_name='sc_install')
asset_order_deinstall = models.ForeignKey(AssetStoreMaster, on_delete=models.CASCADE, blank=True, null=True, related_name='sc_deinstall')
这是我的views.py:
class ReceivedAssetCreate(generic.CreateView):
template_name = 'received_asset/received-asset-new.html'
form_class = ReceivedAssetForm
def get_success_url(self):
return reverse('received_asset:received-asset-new')
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
form.save(commit=True)
messages.success(request, f'Received asset has been received successfully')
return render(request, self.template_name, {"form": form})
这是我的forms.py:
class ReceivedAssetForm(forms.ModelForm):
asset_user = forms.ModelChoiceField(queryset=no_models.MyUser.objects.all(), widget=autocomplete.ModelSelect2())
asset_servicetag = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Service Tag'}))
asset_date = forms.DateInput
asset_note = forms.CharField(required=False)
asset_order_type = forms.ModelChoiceField(queryset=AssetOrderType.objects.all())
asset_order_refresh = forms.ModelChoiceField(queryset=AssetStoreMaster.objects.all(), required=False)
asset_order_install = forms.ModelChoiceField(queryset=AssetStoreMaster.objects.all(), required=False)
asset_order_deinstall = forms.ModelChoiceField(queryset=AssetStoreMaster.objects.all(), required=False)
asset_received = forms.ModelChoiceField(queryset=Asset.objects.all(), widget=autocomplete.ModelSelect2())
asset_status = forms.ModelChoiceField(queryset=AssetStatus.objects.all(),widget=autocomplete.ModelSelect2())
class Meta:
model = AssetReceived
fields = ['asset_user', 'asset_received', 'asset_servicetag', 'asset_date', 'asset_status', 'asset_note',
'asset_order_type', 'asset_order_refresh', 'asset_order_install', 'asset_order_deinstall']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
'asset_user',
Row(
Column('asset_received', css_class='form-group col-md-6 mb-0'),
Column('asset_servicetag', css_class='form-group col-md-4 mb-0'),
Column('asset_date', css_class='form-group col-md-2 mb-0'),
css_class='form-row'
),
Row(
Column('asset_status', css_class='form-group col-md-6 mb-0'),
Column('asset_order_type', css_class='form-group col-md-6 mb-0'),
css_class='form-row'
),
Row(
Column('asset_order_refresh', css_class='form-group col-md-6 mb-0'),
Column('asset_order_install', css_class='form-group col-md-6 mb-0'),
Column('asset_order_deinstall', css_class='form-group col-md-6 mb-0'),
css_class='form-row'
),
'asset_note',
Submit('submit', 'Sign in')
)
模板:
{% load crispy_forms_tags %}
{% block title %}New Asset Rceived{% endblock %}
{% block body %}
<div class="container">
<center><h1>New Asset Received</h1></center>
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
{{ form.media }}
<input type="submit">
</form>
</body>
</html>
</div>
您知道为什么布局不会改变吗?我尝试通过模板更改布局,但结果相同。它仍然保持相同的默认布局。
最佳答案
我相信您遇到了一个晦涩/未记录的使用错误。在您的模板中,尝试将{{ form|crispy }}
更改为{% crispy form %}
。我建议基于以下三点:
From their docs:
使用{% crispy %}
标签,因为它会晃动
与|crispy
过滤器一样方便,将其视为内置方法:as_table
,as_ul
和as_p
。您无法调整输出。使表格清晰的最佳方法是使用{% crispy %}
标记。它将改变您在Django中处理表格的方式。
Stackoverflow帖子:"Make sure you are using the crispy form tag, not the crispy form filter...."
另一个Stackoverflow帖子:"What bit me was I left {{ form|crispy }}
instead of using {% crispy form %}
. The former will only emit Django's generic class based views."
同样,我很难找到有关此特定问题的任何其他文档,但我很确定这就是答案。
关于python - Python Django-脆皮表格布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57507336/