我寫了一段代碼,當用戶在頁面的表單字段中輸入url時,它將解析頁面:「url.html」。當用戶在表單字段中輸入url時,它會出現在這個視圖中(在instance.web_url中)。但即時獲取HTTPResponse對象的這個錯誤。下面的代碼:ValueError at/url /視圖frontend.views.url沒有返回HttpResponse對象。它返回None而不是
def url(request):
if request.method == 'POST':
form_url = WebURLForm(request.POST or None)
title = "Search via URL here"
instance = form_url.save(commit = False)
instance.save()
if instance.web_url == "":
instance.web_url = "http://www.facebook.com/"
print instance.web_url
html = urllib.urlopen(instance.web_url).read()
soup = BeautifulSoup(html,"lxml")
lines = []
# kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
# get text
text = soup.get_text()
#break into lines and remove leading and trailing space on each
lines = (line.strip() for line in text.splitlines())
#break multi-headlines into a line each
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
#drop blank lines
text = '\n'.join(chunk for chunk in chunks if chunk)
text=text.encode('utf-8')
words = text.split(".")
count=0
terrorism_level=0
for i in words:
print count
if not words[count]:
words[count] = "this was empty before."
json_result = natural_language_classifier.classify('90e7b7x198-nlc-50734',words[count])
classes = json_result['classes']
result = json.dumps(classes, indent=2)
if (classes[0]['confidence'] > 0.98 and classes[0]['class_name'] == "nhate."):
print words[count]
print result
terrorism_level +=1
count=count+1
context = {"form_url":form_url, "title":title}
return render(request,'url.html',context)
如果請求方法不是POST,會發生什麼情況? –
@DanielRoseman問題是,我有一個頁面url.html,其中我有一個表單域。現在,當我打開頁面時,它開始處理視圖並在命令提示符下給出結果,並且頁面繼續加載。我的假設是它看到GET請求,因此它開始處理視圖。所以這就是爲什麼,我有這個條件,如果請求是POST,那麼處理所有這個視圖。 – Neo1995