I have a contact form that I'm trying to use in one of my Django templates. I created a class for it in forms.py:
class ContactForm(forms.Form):
name = forms.CharField(max_length=100)
email_address = forms.EmailField(max_length=150)
message = forms.CharField(widget = forms.Textarea,max_length=2000)
and added it to my views.py:
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
subject = "Website Inquiry"
body = {
'name': form.cleaned_data['name'],
'email': form.cleaned_data['email_address'],
'message':form.cleaned_data['message'],
}
message = "\n".join(body.values())
try:
send_mail(subject, message, '[email protected]', ['[email protected]'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect ("BobbleHead:home")
form = ContactForm()
return render(request, "BobbleHead/contact.html", {'form':form})
and am now trying to get the form to render with specific html formatting in my template (contact.html), but am having difficulties.
Previously, I was using the built in Django capabilities to render, like this:
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
That worked fine, but I don't like the way it looks on my webpage and wanted to clean up the formatting. I want the form to render with specific formatting, like this:
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<div class="w3-col m6">
<form action="/home" target="_blank">
{% csrf_token %}
<div class="w3-row-padding" style="margin:0 -16px 8px -16px">
<div class="w3-half">
<div class="fieldWrapper">
<input class="w3-input w3-border" type="text" placeholder="Name" required name="name">
</div>
</div>
<div class="w3-half">
<input class="w3-input w3-border" type="text" placeholder="Email" required name="email_address">
</div>
</div>
<input class="w3-input w3-border" type="text" placeholder="Message" required name="message">
<button class="w3-button w3-black w3-section w3-right" type="submit">SEND</button>
</form>
</div>
But I'm not sure how to change the <input> elements in the stylized block to use my django form.