I hope this text finds you well. I'm experiencing an issue in my django project (code) and I'm in need of your help.
When I filled the form and clicked the submit button, I expect to be directed to the success page, but I keep getting "Method Not Allowed (POST):/success/" in my terminal and "This page isn’t working If the problem continues, contact the site owner. HTTP ERROR 405" display on my screen (browser).
Thanks.
Here's my Contact Form HTML code:
<form class="p-6 flex flex-col justify-center bg-gray_like rounded-lg"
method="POST" action="{% url 'base:success' %}"> {% csrf_token %}
<div class="flex flex-col">
<label for="name" class="hidden">Full Name</label>
<input type="text" name="name" id="name" placeholder="Full Name"
class="w-100 mt-2 py-3 px-3 rounded-lg bg-white border border-blue text-black font-md focus:border-blue focus:outline-none"></div>
<div class="flex flex-col mt-2">
<label for="email" class="hidden">Email</label>
<input type="email" name="email" id="email" placeholder="Email" class="w-100 mt-2 py-3 px-3 rounded-lg bg-white 0 border border-blue text-black font-md focus:border-blue focus:outline-none"></div>
<div class="flex flex-col mt-2">
<label for="subject" class="hidden">Subject</label>
<input type="text" name="subject" id="subject" placeholder="Subject"
class="w-100 mt-2 py-3 px-3 rounded-lg bg-white border border-blue text-black font-md focus:border-blue focus:outline-none"></div>
<div class="flex flex-col mt-2">
<label for="message" class="hidden">Message</label>
<textarea type="text" name="message" id="message" placeholder="Message"
class="w-100 mt-2 py-3 px-3 rounded-lg bg-white border border-blue text-black font-md focus:border-blue focus:outline-none"></textarea></div>
<button type="submit" class="md:w-35 bg-blue hover:bg-blue-dark text-white font-bold py-3 px-10 rounded-lg mt-3 hover:bg-indigo-500 transition ease-in-out duration-300">Send Message</button>
</form>
Here's my veiw.py
#Contact Form view
class ContactView(FormView):
def post(self, request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid(): #Form validation
#extract data from the form
name = form.cleaned_data['name']
email = form.cleaned_data['email']
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
# send email to recipient
send_mail(
'CONTACT FORM SUBMITTED',
f'Name: {name}\nEmail: {email}\nSubject: {subject}\nMessage: {message}',
'[email protected]',
['[email protected]','[email protected]'],
fail_silently= False)
return render(request,'/success.html')
else:
form = ContactForm()
return render(request,'contact.html/',{'form':form})
Here's my base app url code:
app_name = 'base'
urlpatterns = [
path('', views.HomePageView.as_view(), name='home'),
path('services/', views.ServicesPageView.as_view(), name='services'),
path('about_us/', views.About_UsPageView.as_view(), name='about_us'),
path('price/', views.PricePageView.as_view(), name='price'),
path('portfolio/', views.PortfolioPageView.as_view(), name='portfolio'),
path("<int:pk>/", views.ProjectDetailView.as_view(), name='project_detail'),
path('contact', views.ContactView.as_view(), name='contact'),
path('success/', views.SuccessPageView.as_view(), name='success'),
]
Error Message in the Terminal :
"Method Not Allowed (POST): /success/ Method Not Allowed: /success/ [22/Aug/2023 05:59:19] "POST /success/ HTTP/1.1" 405 0"