4

Good, I have in Django this configuration in urls.py:

urlpatterns = [
    path('', views.index),
]

and in views.py the index function where I pass through the variable a url, I have with requests_html:

from django.http import HttpResponse
from django.core.validators import URLValidator
from requests_html import HTMLSession
from bs4 import BeautifulSoup
from .main import *

def index(request):
    url = request.GET.get('url')
    # URLValidator...
    session = HTMLSession()
    resp = session.get(url)
    resp.html.render()
resp.html.render()
  File "/home/noname/.local/lib/python3.8/site-packages/requests_html.py", line 586, in render
    self.browser = self.session.browser  # Automatically create a event loop and browser
  File "/home/noname/.local/lib/python3.8/site-packages/requests_html.py", line 727, in browser
    self.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.'

RuntimeError: There is no current event loop in thread 'Thread-1'.

But I get a bug when calling render() and I'm not using threads, any ideas?

hmn Falahi
  • 730
  • 5
  • 22
Javi
  • 188
  • 1
  • 9
  • 1
    Show us the full error traceback! – Klaus D. Sep 28 '21 at 14:06
  • Show us your imports too (what is `HTMLSession`? - I don't think that is from django..?) – thebjorn Sep 28 '21 at 14:13
  • The current error message might be more informative "Cannot use HTMLSession within an existing event loop. Use AsyncHTMLSession instead." (from https://github.com/psf/requests-html/blob/026c4e5217cfc8347614148aab331d81402f596b/requests_html.py#L797) – thebjorn Sep 28 '21 at 14:32
  • If you're running `manage.py runserver` then it is multithreaded by default. You might get around it by using `--nothreading` (https://docs.djangoproject.com/en/3.2/ref/django-admin/#runserver) – thebjorn Sep 28 '21 at 14:37
  • with --nothreading obtain "There is no current event loop in thread 'django-main-thread'" ... – Javi Sep 28 '21 at 14:41
  • With AsyncHTMLSession obtain the same ... :( – Javi Sep 28 '21 at 14:41
  • having the same issue...have you ever solved this?? @Javi – itsmehemant7 Jan 17 '23 at 02:19

0 Answers0