简体   繁体   中英

RuntimeError at /accounts/register/ There is no current event loop in thread 'Thread-1'

I want to implement an email verification in my django app so, I am using an external module verify_email in that there is a function called verify_email() to check whether an entered email address exists or not. In my view.py file I am checking if the mail is valid then a user is created but, the problem is the function verify_email() takes some time to get the result. I think we need to use some kind of await statement to wait for the result but I am getting errors like this when I try to use it normally.

This is the pypi project link.

def register(request):
    if(request.method == 'POST'):
        email = request.POST['email']
        password = request.POST['pass']
        username = request.POST['username']
        check = verify_email(email)
        
        if check:
            user = User.objects.create_user(username,email,password)
            user.save()
            return redirect('/')
        else:
            messages.info(request,'email invalid')
            return redirect('register')

Description of the error:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/register/

Django Version: 3.2.3
Python Version: 3.8.5

Traceback (most recent call last):
  File "/home/akshith/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/akshith/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/akshith/Desktop/django-app/testing/final (copy)/accounts/views.py", line 55, in register
    check = verify_email(email)
  File "/home/akshith/.local/lib/python3.8/site-packages/verify_email/verify_email.py", line 103, in verify_email
    loop = asyncio.get_event_loop()
  File "/usr/lib/python3.8/asyncio/events.py", line 639, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'

Exception Type: RuntimeError at /accounts/register/
Exception Value: There is no current event loop in thread 'Thread-1'.

Is there a way to use asyncio or any other ways to deal with this kind of error?

Do you really think that the user should proceed even without email verification? It usually takes atleast few seconds to verify email on any website, So that should not be a problem. And for Concurrent requests you don't need to worry because django handles them very efficiently.

And sorry, I didn't had enough reputation to comment:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM