So I have a feedback form.
This form is to be applied to multiple pages, however, in the interests of the DRY concept of coding, I have created a template which just contains the form and used the {% include 'snippets/legal-feedback-form.html' %}
’ to insert this into every HTML template with which I want to include the form.
The legal-feedback-form.html
as the following:
<section class="page">
<div class="page-content">
<h1>Feedback</h1>
<h2>Was this page helpful and informative? Feel free to submit feedback.</h2>
<form method="POST">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.errors }}
<label>{{ field.label_tag }}</label>
<p>{{ field }}</p>
</div>
{% endfor %}
<div class="disclaimer">
{% include 'snippets/privacy-notice.html' %}
</div>
<button type="submit" class="btn btn--pink">Submit Feedback</button>
</form>
</div>
</section>
In the legal.forms.py
, I have the following:
def should_be_empty(value):
if value:
raise forms.ValidationError('Field is empty')
class FeedbackForm(forms.Form):
name = forms.CharField(required=True,max_length=80, widget=forms.TextInput(attrs={'placeholder': 'Enter Your Name'}))
message = forms.CharField(required=True,widget=forms.Textarea(attrs={'placeholder': 'Enter Your Message'}))
email = forms.EmailField(widget=forms.EmailInput(attrs={'placeholder': 'Enter your Email'}))
number = forms.CharField(max_length=12, required=True, widget=forms.TextInput(attrs={'placeholder' : 'Your Contact Number'}))
Feedback = forms.URLField(widget=forms.HiddenInput)
forcefield = forms.CharField(required=False, widget=forms.HiddenInput, label="Leave Empty", validators=[should_be_empty])
In the legal.views.py
file, I have the following:
from .forms import FeedbackForm
def legal__privacy_statement(request):
form = FeedbackForm()
if request.method == 'POST':
form = FeedbackForm(request.POST)
if form.is_valid():
subject = form.cleaned_data["name"]
message = form.cleaned_data["message"]
email = form.cleaned_data["email"]
number = form.cleaned_data["number"]
name = form.cleaned_data["name"]
body = "Message: " + message + "\n\nEmail: " + email + "\n\nContact Number: " +number
sender = name + " <from_email>"
recipients = ["<to_email>"]
try:
send_mail(subject, body, sender, recipients, fail_silently=True)
return render (request, 'success.html')
except:
return HttpResponse('There has been an error in submitting this form')
context = {
'form' : form
}
return render (request, 'privacy-statement.html', context)
All, very well, however, I’m having to apply this to every app.views.<method>
with which I wish to include the form. I don’t really want to do that, so how would I go about creating a separate def
function with which ANY webpage from within the app can draw this from.
In this, example, this is tied to the privacy-statement.html
template but - as expected - you include the template in another webpage with the {% include 'snippets/legal-feedback-form.html' %}
and - as expected - it doesn’t populate the form fields from the for
loop.
Thank you.