所以,您的问题是,您需要调用Beautiful Soup,但当用户最初加载页面并且没有提交表单时,您不需要这样做。
最简单的解决方案是将调用
bs
变成一个
if
只有当你
POST
:
# Define these so that they have a value to be called in the template
text = submitbutton = ''
if request.method == 'POST':
text = request.POST.get('text')
submitbutton = request.POST.get('Submit')
m = str(text)
print(m)
print(text)
# Descarga el contenido del HTML
with urllib.request.urlopen('http://www.elmundo.es/espana/2018/06/05/5b162c3b468aebd9678b4695.html') as response:
page = response.read()
soup = BeautifulSoup(page, 'html.parser')
p = soup.find_all('p')
# This needs to be outside your 'if' clause, so that the page will render if the method isn't POST
context = {'text': text,
'submitbutton': submitbutton,
'type':q1}
return render(request, 'form/form.html', context)
从你的样本来看,我不确定
qt
得到定义或获取捕获
submitbutton
,但这或多或少会起作用。