私はページのフォームフィールドに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