Returning to a web page after submitting a contact form.

Hi,

I have created a contact page which works well,

And sends an email via mailgun with the correct infomation.

All good, email quickly arrives in gmail inbox.

But then get a server 500 145 error, (on the internet or internet disconnected)

How does one redirect back to a webpage / contact page after submit has been pressed ?

Here’s my code in views.py - finger typed.


def contact(request):

  …… etc 

   ……etc 

    …….

    Send_mail(data[‘subject’],message,’’,[‘x.gmail.com’])
    return httpResponse(‘thanks’)
return render(request,’webs/contact.html’)
#return httpResponseReDirect(‘webs/contact.html’) 

Thanks in advance Steve

When requesting help with an error, please post the complete error from the server, with the full traceback. (Don’t post what is shown in the browser.)

Also, copy/paste the view generating the error - don’t retype it - it’s too easy to make a mistake that way.

Finally, as a side note, enclose the complete block of code between one pair of lines of three backticks. Do not enclose each individual line between them. (I took the liberty of correcting your post for this.)

To directly address your question:

Every view must return some type of HttpResponse. It can be an instance of one of the Django-provided classes you create directly, or one created by a shortcut function such as render. Either way, you must ensure that every path through the code in your view ends up returning one.

Full Trace back from cmd window running server.(laptop screen unable to copy) - email delivered successfully

“GET /contact/HTTP/1.1”  200 2285
“GET  /favicon.ico HTTP/1.1” 404 179
{‘name’:’dave tomlins’,’email’: ‘x@gmail.com’, ‘subject’:’tests’,’message’:’from contact page’}
POST /contact/HTTP/1.1” 500 145

Web browser is left blank page?

Without being able to see the full traceback and source code, I don’t think we’re going to be able to help you here.

How do you get the full trace back?

the server 500 error has gone away! by the removing the following line.

 return httpResponse(‘thanks’) 

Browser remains open on the contact page after submit button has been pressed, the user has to click to go to another page.

Is there an alternative way to say ‘thanks’ that can be displayed after message has been sent in html?

That depends upon your environment and how you’re running your server.

This is potentially an invalid line. There is no Django-provided class or function named httpResponse. There is a class HttpResponse, which may be what you meant here. Otherwise, unless you’ve defined your own function named httpResponse, this would be an error.

You might want to take a look at The messages framework.

Thanks, all working.

Will look at the messaging framework.

It was the lower case H that was the issue causing server error 500.

Currently after submitting contact details and does email, web page now goes back to index page.

Great,

is it possible to show a thankyou first for a few seconds before going to index page?

Or is it not possible to do delays in html?

Yes, but not from the Django side.

You can return a page that includes some JavaScript to issue a request for the next page after some defined time interval.