I’m finally tormented with the feedback form. As a result, it worked for me, but it only works on a separate page of the project, and I need to implement it in each page of the site so that it is displayed together with the rest of the content. This is not the first time I have encountered such a problem; I cannot display several models on one page at the same time.
How this is implemented in the case of a separate page:
views.py:
def contact(request):
form_class = ContactForm
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
contact_name = request.POST.get(
'contact_name'
, '')
contact_email = request.POST.get(
'contact_email'
, '')
form_content = request.POST.get('content', '')
# Email the profile with the
# contact information
template = get_template('app/contact/contact_template.txt')
context = {
'contact_name': contact_name,
'contact_email': contact_email,
'form_content': form_content,
}
content = template.render(context)
email = EmailMessage(
"New contact form submission",
content,
'', ['mail@mailer.com'],
headers = {'Reply-To': contact_email }
)
email.send()
return redirect('contact')
return render(request, 'app/contact.html', {
'form': form_class,
})
in urls.py:
...
path('contact/', views.contact, name='contact'),
...
contact.html:
<h1>Contact</h1>
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
forms.py:
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True)
content = forms.CharField(
required=True,
widget=forms.Textarea
)
# the new bit we're adding
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
self.fields['contact_name'].label = "Your name:"
self.fields['contact_email'].label = "Your email:"
self.fields['content'].label =
"What do you want to say?"
Maybe there is some better solution for the form to work on every page?
and
– Shan May 22 '20 at 10:37