I am using a bootstrap variant to help style a model form. There is a certain class I would like one of the fields to be and I have read around on the subject and the general consensus is to add a widget to the ModelForm’s meta, like I tried below:
forms.py
JavaScript
x
8
1
class EmailForm(forms.ModelForm):
2
class Meta:
3
model = MarketingEmails
4
fields = ['messageid','subject','body','name','altsubject','utm_source','utm_content','utm_campaign',]
5
widgets = {
6
'body': Textarea(attrs={'class': 'summernote'}),
7
}
8
However this doesn’t seem to render onto my template, which is:
JavaScript
1
30
30
1
<div class="row">
2
<div class="col-sm-6">
3
<form method="POST" class="post-form" action ="">
4
{% csrf_token %}
5
<p><label for="id_subject">Subject</label>
6
<input class="form-control" id="id_subject" type="text" name="subject" maxlength="1000" value="{{rows.subject}}"required /></p>
7
8
<p><label for="id_name">Name</label>
9
<input class="form-control" id="id_name" type="text" name="name" maxlength="1000" value="{{rows.name}}"required /></p>
10
11
<p><label for="id_body">Body</label>
12
<input class="form-control" id="id_body" type="text" name="body" maxlength="1000" value="{{rows.body}}"required /></p>
13
14
<p><label for="id_altsubject">Alt Subject</label>
15
<input class="form-control" id="id_altsubject" type="text" name="altsubject" maxlength="1000" value="{{rows.altsubject}}"required /></p>
16
17
<p><label for="id_utm_source">utm_source</label>
18
<input class="form-control" id="id_utm_source" type="text" name="utm_source" maxlength="1000" value="{{rows.utm_source}}"required /></p>
19
20
<p><label for="id_utm_content">utm_content</label>
21
<input class="form-control" id="id_utm_content" type="text" name="utm_content" maxlength="1000" value="{{rows.utm_content}}"required /></p>
22
23
<p><label for="id_utm_campaign">utm_campaign</label>
24
<input class="form-control" id="id_utm_campaign" type="text" name="utm_campaign" maxlength="1000" value="{{rows.utm_campaign}}"required /></p>
25
26
<button type="submit" class="save btn btn-default">Save</button>
27
28
</form>
29
</div>
30
Is there another way to do this or is there something I have done wrong in my code?
UPDATE I have followed the suggested of Jacek and now it is styled but the information is no longer displaying, this is my new code:
forms.py:
JavaScript
1
15
15
1
class EmailForm(forms.ModelForm):
2
subject = forms.CharField(
3
label = 'Subject',
4
max_length = 2000,
5
required = True,
6
widget = forms.TextInput(
7
attrs = {'class': 'summernote', 'name': 'subject'}
8
)
9
)
10
11
12
class Meta:
13
model = MarketingEmails
14
fields = ['messageid','subject','body','name','altsubject','utm_source','utm_content','utm_campaign',]
15
views.py:
JavaScript
1
17
17
1
def emailinfo(request, pk):
2
if request.session.has_key('shortname'):
3
shortname = request.session['shortname']
4
form = MarketingEmails.objects.filter(messageid =pk).get()
5
if request.method == 'GET':
6
form = EmailForm(instance=form)
7
return render(request, 'marketingemails/emailinfo.html',{'shortname': shortname, 'form': form})
8
9
else:
10
form = EmailForm(request.POST,instance=form)
11
if form.is_valid():
12
return redirect('marketingemails:emailinfo', pk = form.messageid)
13
14
return render(request, 'marketingemails/emailinfo.html',{'shortname': shortname, 'form': form})
15
else:
16
return HttpResponseRedirect(reverse('common:login'))
17
html:
JavaScript
1
22
22
1
<div class="row">
2
<div class="col-sm-6">
3
<form method="POST" action ="">
4
{% csrf_token %}
5
{% for field in form %}
6
{{ field.label_tag }}
7
{{ field }}
8
9
{% if field.help_text %}
10
{{ field.help_text }}
11
{% endif %}
12
13
{% for error in field.errors %}
14
{{ error }}
15
{% endfor %}
16
17
{% endfor %}
18
<button type="submit" class="btn btn-primary">Submit</button>
19
20
</form>
21
</div>
22
Advertisement
Answer
Try this:
forms.py
JavaScript
1
25
25
1
class EmailForm(forms.ModelForm):
2
3
subject = forms.CharField(
4
label = 'Subject',
5
max_length = 1000,
6
required = True,
7
widget = forms.TextInput(
8
attrs = {'class': 'summernote', 'name': 'subject'}
9
)
10
)
11
12
body = forms.CharField(
13
label = 'Body',
14
max_length = 1000,
15
required = True,
16
widget = forms.TextInput(
17
attrs = {'class': 'summernote', 'name': 'body'}
18
)
19
)
20
21
22
class Meta:
23
model = MarketingEmails
24
fields = ('messageid','subject','body','name', )
25
view.py
JavaScript
1
12
12
1
from django.shortcuts import render
2
from your_app_path.forms import EmailForm
3
4
def fname(request):
5
6
marketing = MarketingEmails.objects.get( )
7
8
form = EmailForm(instance=marketing)
9
10
11
return render(request, 'yourview.html', { 'form': form })
12
yourview.html
JavaScript
1
18
18
1
<form action="" method="post">
2
{% csrf_token %}
3
{% for field in form %}
4
{{ field.label_tag }}
5
{{ field }}
6
7
{% if field.help_text %}
8
{{ field.help_text }}
9
{% endif %}
10
11
{% for error in field.errors %}
12
{{ error }}
13
{% endfor %}
14
15
{% endfor %}
16
<button type="submit" class="btn btn-primary">Submit</button>
17
</form>
18