Encoding with 'idna' codec failed (UnicodeError: label empty or too long)

Hello Django community!

I am trying to solve the problem i see on two places.

  1. Password reset
  2. Email account verification

In both cases i receive this error
Encoding with ‘idna’ codec failed (UnicodeError: label empty or too long)

This is Stack overflow link where i posted. But i could post the same here maybe?

Anyhow, here i will post the problem for password reset, because i get the same error.
Here is the main urls.py (i have an accounts app, but i put the paths in this main urls file)

password management

from django.contrib.auth import views as auth_views

urlpatterns = [
path(‘admin/’, admin.site.urls),
# PASSWORD MANAGEMENT VIEWS

# submit email form
path('reset_password/', auth_views.PasswordResetView.as_view(template_name='accounts/password/password-reset-form.html'), name='reset_password'),

# success message stating the email was sent to click on the link
path('password_reset_done/', auth_views.PasswordResetDoneView.as_view(template_name='accounts/password/password-reset-done.html'), name='password_reset_done'),

# password reset link
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='accounts/password/password-reset-confirm.html'), name='password_reset_confirm'),

# success message stating that our password was reset
path('password_reset_complete/', auth_views.PasswordResetCompleteView.as_view(template_name='accounts/password/password-reset-complete.html'), name='password_reset_complete'),

]

Then i have created the templates. But when i visit a page to enter the email and click submit, i get the error

UnicodeError at /reset_password/

encoding with ‘idna’ codec failed (UnicodeError: label empty or too long)

I hope someone can help :slight_smile:

Thanks!

Please post the complete traceback from the console in which you’re running the server.

Thank you, i will send the screenshot.

I am trying to paste to whole text, but the system is not allowing me :confused:

I was actually asking for the output from the console where you’re running the server, not the browser output. But in this case, I think the browser output will be good enough.

Oh, okay, here it is, pasted from the console.

[27/Apr/2023 14:46:08] “GET /reset_password/ HTTP/1.1” 200 1151
Internal Server Error: /reset_password/
Traceback (most recent call last):
File “C:\Users\Dell\AppData\Local\Programs\Python\Python311\Lib\encodings\idna.py”, line 163, in encode
raise UnicodeError(“label empty or too long”)
UnicodeError: label empty or too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\core\handlers\exception.py”, line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\core\handlers\base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\views\generic\base.py”, line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\utils\decorators.py”, line 46, in _wrapper
return bound_method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\utils\decorators.py”, line 134, in _wrapper_view
response = view_func(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\contrib\auth\views.py”, line 242, in dispatch
return super().dispatch(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\views\generic\base.py”, line 143, in dispatch
return handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\views\generic\edit.py”, line 153, in post
return self.form_valid(form)
^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\contrib\auth\views.py”, line 255, in form_valid
form.save(**opts)
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\contrib\auth\forms.py”, line 368, in save
self.send_mail(
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\contrib\auth\forms.py”, line 309, in send_mail
email_message.send()
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\core\mail\message.py”, line 298, in send
return self.get_connection(fail_silently).send_messages([self])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\core\mail\backends\console.py”, line 36, in send_messages
self.write_message(message)
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\core\mail\backends\console.py”, line 17, in write_message
msg = message.message()
^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\core\mail\message.py”, line 279, in message
msg[“Message-ID”] = make_msgid(domain=DNS_NAME)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Dell\AppData\Local\Programs\Python\Python311\Lib\email\utils.py”, line 193, in make_msgid
msgid = ‘<%d.%d.%d%s@%s>’ % (timeval, pid, randint, idstring, domain)
^~~~~~~~~~~~~~~~~~~~~~~~~
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\core\mail\utils.py”, line 14, in str
return self.get_fqdn()
^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\core\mail\utils.py”, line 18, in get_fqdn
self._fqdn = punycode(socket.getfqdn())
^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\2023\Projekti\main\mainvenv\Lib\site-packages\django\utils\encoding.py”, line 213, in punycode
return domain.encode(“idna”).decode(“ascii”)
^^^^^^^^^^^^^^^^^^^^^
UnicodeError: encoding with ‘idna’ codec failed (UnicodeError: label empty or too long)
[27/Apr/2023 14:46:10] “POST /reset_password/ HTTP/1.1” 500
147781

I’m going to guess it’s because you’re accessing your site using the 127.0.0.1 address and you don’t have a fully-qualified DNS name for your host. In fact, it appears that you might not even have a hostname defined.

It’s trying to get the hostname from the Python getfqdn function in the socket library. See socket — Low-level networking interface — Python 3.11.3 documentation

Thank you very much for your help. I will read that doc, but can you please tell me, is there any solution i can implement in this moment for local host? Or should i want to upload it and then test?

Please describe your runtime environment, are you running this natively in windows? Or are you using something like a docker container or a virtual machine?

Thank you, okay so maybe you would understand better if i say i am a beginner, because i basically am. So no docker, just a virtual env and localy, os is windows.

Open up a command line window, activate the virtual environment, and run the python command to get into the python shell.

In the shell, run the following commands and report back what the results are:

(No output is expected from this first command)
import socket

socket.gethostname()

socket.getfqdn()

socket.getfqdn('127.0.0.1')

Thank you, here you go.

import socket

socket.gethostname()
‘DESKTOP-2OGR8E0’

socket.getfqdn()
‘DESKTOP-2OGR8E0…home’

socket.getfqdn(‘127.0.0.1’)
‘DESKTOP-2OGR8E0’

The problem is with the fqdn as reported here. The punycode function can’t handle the multiple-consecutive periods in the name.

You can look in your \Windows\System32\drivers\etc\hosts file to see if there’s anything that isn’t right in it, but if you’re in an environment where that file is managed for you, I’m not sure there’s anything you can do about it.

Okay, thank you. So, i can access that hosts file, but i don’t really understand what do you mean by “to see if there’s anything that isn’t right in it”

Should i or can i do something in that file?

Also, can you please help explain “but if you’re in an environment where that file is managed for you”?

Thank you for your help

You’d have to post the contents of the file here if you want me to look at it - basically, you’re looking for an entry with the consecutive periods in the name, but I’m not sure what else might be in that file that could create this situation.

Don’t know yet. The first step is to identify whether a change is necessary.

If you’re working in a corporate environment, or on a computer that isn’t yours (e.g. school), you may not have the permissions or authority necessary to edit that file.

Okay i will in a bit, thank you

I am trying to send the whole text from the hosts file, but i get the error “An error occurred: Sorry, new users can only put 2 links in a post.” when i click on the Reply button :confused:

Surround the text between the lines of three backtick - ` characters. This means you’ll have a line of ```, then the contents of the file, then another line of ```. This should allow you to post the complete text as text, and not as links.

Here it is, thank you.

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
#0.0.0.1 scinstallcheck.mcafee.com


#0.0.0.1 mssplus.mcafee.com
0.0.0.1	mssplus.mcafee.com

# unchecky_begin
# These rules were added by the Unchecky program in order to block advertising software modules
0.0.0.0 0.0.0.0 # fix for traceroute and netstat display anomaly
0.0.0.0 tracking.opencandy.com.s3.amazonaws.com
0.0.0.0 media.opencandy.com
0.0.0.0 cdn.opencandy.com
0.0.0.0 tracking.opencandy.com
0.0.0.0 api.opencandy.com
0.0.0.0 api.recommendedsw.com
0.0.0.0 rp.yefeneri2.com
0.0.0.0 os.yefeneri2.com
0.0.0.0 os2.yefeneri2.com
0.0.0.0 installer.betterinstaller.com
0.0.0.0 installer.filebulldog.com
0.0.0.0 d3oxtn1x3b8d7i.cloudfront.net
0.0.0.0 inno.bisrv.com
0.0.0.0 nsis.bisrv.com
0.0.0.0 cdn.file2desktop.com
0.0.0.0 cdn.goateastcach.us
0.0.0.0 cdn.guttastatdk.us
0.0.0.0 cdn.inskinmedia.com
0.0.0.0 cdn.insta.oibundles2.com
0.0.0.0 cdn.insta.playbryte.com
0.0.0.0 cdn.llogetfastcach.us
0.0.0.0 cdn.montiera.com
0.0.0.0 cdn.msdwnld.com
0.0.0.0 cdn.mypcbackup.com
0.0.0.0 cdn.ppdownload.com
0.0.0.0 cdn.riceateastcach.us
0.0.0.0 cdn.shyapotato.us
0.0.0.0 cdn.solimba.com
0.0.0.0 cdn.tuto4pc.com
0.0.0.0 cdn.appround.biz
0.0.0.0 cdn.bigspeedpro.com
0.0.0.0 cdn.bispd.com
0.0.0.0 cdn.bisrv.com
0.0.0.0 cdn.cdndp.com
0.0.0.0 cdn.download.sweetpacks.com
0.0.0.0 cdn.dpdownload.com
0.0.0.0 cdn.visualbee.net
# unchecky_end