繁体   English   中英

/accounts/register/ 处的 RuntimeError 线程 'Thread-1' 中没有当前事件循环

[英]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. 在我的 view.py 文件中,我正在检查邮件是否有效,然后创建了一个用户,但是问题是 function verify_email() 需要一些时间才能获得结果。 我认为我们需要使用某种 await 语句来等待结果,但是当我尝试正常使用它时出现这样的错误。

是 pypi 项目链接。

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')

错误描述:

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'.

有没有办法使用 asyncio 或任何其他方式来处理这种错误?

你真的认为即使没有 email 验证用户也应该继续? 在任何网站上验证 email 通常至少需要几秒钟,所以这应该不是问题。 对于并发请求,您无需担心,因为 django 可以非常有效地处理它们。

抱歉,我没有足够的声誉发表评论:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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