So I am making a django project on Google Cloud, I have added a forgot password feature from Django's built-in feature and I already tried it locally. But when I tried it on the cloud it gave me this error....
OSError at /en/reset_password/
[Errno 101] Network is unreachable
Request Method: POST
Request URL: http://04.198.88.75:8000/en/reset_password/
Django Version: 5.0.3
Exception Type: OSError
Exception Value:
[Errno 101] Network is unreachable
Exception Location: /usr/lib/python3.10/socket.py, line 833, in create_connection
Raised during: django.contrib.auth.views.PasswordResetView
Python Executable: /home/rsa-key-20240516/env/SlangID/bin/python
Python Version: 3.10.12
Python Path:
['/var/www/html/SlangID/src',
'/usr/lib/python310.zip',
'/usr/lib/python3.10',
'/usr/lib/python3.10/lib-dynload',
'/home/rsa-key-20240516/env/SlangID/lib/python3.10/site-packages']
Server time: Sun, 02 Jun 2024 11:55:50 +0000
I really don't understand what to do I already tried adding a firewall rule to allow smtp connections like this image....
Even after that it still gave me the same error, chatGPT (I was desperate) reccomended to try ping IP 8.8.8.8. When I tried pinging it works well....
:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=122 time=1.91 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=122 time=0.552 ms
Then I tried pinging telnet smtp, like this...
~$ telnet smtp.gmail.com 587
Trying 74.125.130.108...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP d9443c01a7336-1f632356aa0sm47645545ad.74 - gsmtp
which I believed to be a success. Then the last two is to check the email settings through Django's command shell like the one below...
~$ python /var/www/html/SlangID/src/manage.py shell
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.conf import settings
>>>
>>> print(settings.EMAIL_HOST)
smtp.gmail.com
>>> print(settings.EMAIL_PORT)
25
then to Check firewall rules to ensure the SMTP port 587 is open for outbound traffic, but I didn't quite understand that. This is my urls.py for the forgot password pages...
from django.contrib.auth import views as auth_views
# Forgot Password Paths
path('reset_password/', auth_views.PasswordResetView.as_view(
template_name='forgot_pass/change_password.html',
success_url=reverse_lazy('password_reset_done'),
),
name="password_reset"
), #1
path('reset_password/done/', auth_views.PasswordResetDoneView.as_view(template_name='forgot_pass/sent_email.html'), name="password_reset_done"), #2
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(
template_name='forgot_pass/reset_pass.html',
success_url=reverse_lazy('password_reset_complete'),
),
name="password_reset_confirm"
), #3
path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(
template_name='forgot_pass/complete_pass.html',
),
name="password_reset_complete"
), #4
that's the only code I used to get it to work as well as this part in the settings.py....
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_POST = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER ='sussy@gmail.com'
EMAIL_HOST_PASSWORD ='aaaa aaaa aaaa aaaa'
I already desccribe everything in the details.
So apparently I had to just change the POST to PORT when I am hosting the django project on a Google Cloud server, so this is what my settings.py file looks like....
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER ='sussy@gmail.com'
EMAIL_HOST_PASSWORD ='aaaa aaaa aaaa aaaa'