No connection could be made because the target machine actively refused it (Django)
Asked Answered
C

8

15

I have followed the Django Book up until chapter seven, and I am currently messing around with forms, GET, POST and all that goodness. At one point, the guide made me figure out the reaction, after a form is filled out and sent, but when I send the form data, I get this error:

error at /contact/
[Errno 10061] No connection could be made because the target machine actively refused itRequest Method: POST
Request URL:    http://127.0.0.1:8000/contact/
Django Version: 1.3
Exception Type: error
Exception Value:    [Errno 10061] No connection could be made because the target machine actively refused it
Exception Location: C:\Python27\lib\socket.py in create_connection, line 571
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.2

In other words, I haven’t had any port problems up until this point. I just opened port 10061 explicitly in Windows Firewall but to no avail by the look of it. (I closed and opened the runserver, after I had changed the rules.)

I am running Windows 7, and the gist of my question is what exactly this error message means more so than how to deal with it (both are preferable, of course).

EDIT: I have also forwarded port 8000 in Windows Firewall (apply to all profiles, TCP), but I still get what looks like the same error.

Columbary answered 21/7, 2011 at 21:1 Comment(0)
C
30

I managed to find out what the problem was (no thanks to the error message). As it turns out, I needed to set up my e-mail server:

Note that in order to send e-mail using send_mail(), your server must be configured to send mail, and Django must be told about your outbound e-mail server. See http://docs.djangoproject.com/en/dev/topics/email/ for the specifics.

I guess I thought little of what was meant by it, but I was pointed in the direction of this guide, and things eventually started to click.

Thanks to everyone for chiming in with their advice. That is one useless error message, and I can only assume the people who helped me out only knew the answer because they had experienced the exact same thing.

Columbary answered 24/7, 2011 at 23:54 Comment(3)
wow, well if I ever run into working with this framework, it'll save me time having to figure whats wrong! glad you found the answer. +1 for sharing. (I thought only the big named company gave out useless error messages; guess its getting more common now!)Theodolite
Boy am I glad I found this so rapidly. Could have cost me a few hours of sleep and a few hairs.Oospore
I can’t believe this oddness is still around. I still can’t quite wrap my head around the how and why of it.Columbary
A
11

I got this error attempting to use django.core.mail.send_mail. I only need this for running through some tutorials. I don't need it to actually send mail.

The solution for me was to add this single variable to settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

It sends the content of the email to console output, which is perfect given what I'm doing.

Docs: https://docs.djangoproject.com/en/1.7/topics/email/#django.core.mail.backends.smtp.EmailBackend

Alamode answered 4/8, 2014 at 2:20 Comment(0)
H
2

10061 isn't the port number, that's the error number. You want to open port 8000.

See this answer for someone who was having the same problem because they were using the wrong port: No connection could be made because the target machine actively refused it

Hydrocarbon answered 21/7, 2011 at 21:42 Comment(2)
you can also start the server with port 10061 with runserver 10061 and go to url http:127.0.0.1:10061 ... instead of port 8000.Bohn
I forwarded port 8000 in Windows Firewall, but the problem persists by the look of it.Columbary
N
2

In settings.py, For console output, you need

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

For smtp

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

Also refer this doc here https://docs.djangoproject.com/en/1.6/topics/email/

Nally answered 6/12, 2014 at 7:44 Comment(0)
B
0

The code tries to connect to http://127.0.0.1:8000/contact/ - the port 8000 is not reachable (firewall) or there is no server running.

Bolling answered 21/7, 2011 at 21:3 Comment(2)
the 'actively refused it' error strongly suggests the nobody is listening on that port (as opposed to firewall - which would typically trigger a timeout)Means
I forwarded port 8000 in Windows Firewall, but the problem persists by the look of it.Columbary
T
0

I've never used DJango myself but judging by the error, there is no server listening at port 8000 (or some firewall/server restriction is blocking port 8000 on localhost)

Theodolite answered 21/7, 2011 at 21:4 Comment(3)
I forwarded port 8000 in Windows Firewall, but the problem persists by the look of it.Columbary
do a netstat -a -p TCP -n on command prompt and see if you can find something along the lines of "0.0.0.0:8000" in second column of listing that has State "LISTENING" (last column) If you can't find any entry that ends with :8000 in second column, then no server is listening at that port and you need to either start the webserver or configure the webserver to listen on that port or access the URL on the port that your webserver is listening on Since your server has been fine upto now, I'd guess your webserver isn't actually running (hence you won't find the :8000 entry in netstat output)Theodolite
I have a 127.0.0.1:8000 0.0.0.0:0 LISTENING running according to the netstat: pastebin.com/raw.php?i=EVcgjNeV.Columbary
W
0

If you are using gmail, you have to unlock Captcha to enable Django to send it for you.https://accounts.google.com/displayunlockcaptcha

I could solve my issue of this type by unlocking Captcha for the gmail account I used in the app.You can go to this link in other to unlock Captcha.

I hope this's gonn'be useful :)

Whitehot answered 23/5, 2016 at 4:49 Comment(0)
A
0

This is probably a noob mistake but I hit this error because I was trying to call as_view() on a Function-Based View.

Error case:

urlpatterns = [
  path('scratch/echo/', views.scratch_echo.as_view()),
]

Working case:

urlpatterns = [
  path('scratch/echo/', views.scratch_echo),
]

I think I just copy-pasted the setup for a Class-Based View from a different tutorial and my server stopped running.

Audiophile answered 25/6, 2022 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.