Answer the question
In order to leave comments, you need to log in
How to hide label for individual form field in template?
Good evening.
Tell me how to make only the label for the slug field hidden?
In forms, I hide the field, but how to remove the label? I suspect that you can hide it in the template with a filter or use as_hidden for the slug, but while I’m weak in such nuances, I can’t figure out how to make it hidden individually. Help, plz...
models.py:
class Article(models.Model):
category = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True, blank=False)
slug = models.SlugField('Slug', max_length=150, unique=True, null=True, blank=True)
title = models.CharField('Название статьи', max_length=150, help_text='Название статьи в текущем разделе')
content = models.TextField('Описание статьи', db_index=True, null=True, blank=True)
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = '__all__'
widgets = {
'slug': forms.HiddenInput(),
'category': forms.Select(attrs={'class': 'form-control'}),
'title': forms.TextInput(attrs={'class': 'form-control'}),
'content': forms.Textarea(attrs={'class': 'form-control','rows': 15}),
}
{% for field in form %}
{% if field.errors %}
<div class="alert alert-danger">
{{ field.errors }}
</div>
{% endif %}
{{ field.label }}
{{ field }}
{% endfor %}
Answer the question
In order to leave comments, you need to log in
class ArticleForm(forms.ModelForm):
slug = forms.CharField(widget=forms.HiddenInput(), label='')
class Meta:
model = Article
fields = '__all__'
widgets = {
'category': forms.Select(attrs={'class': 'form-control'}),
'title': forms.TextInput(attrs={'class': 'form-control'}),
'content': forms.Textarea(attrs={'class': 'form-control','rows': 15}),
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question