2017-05-09 11 views
1

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

リクエストメソッドがPOSTでない場合はどうなりますか? –

+0

@DanielRoseman問題は、私はフォームフィールドを持っているページurl.htmlを持っています。今、ページを開くと、ビューの処理が開始され、コマンドプロンプトで結果が表示され、ページは読み込みを続けます。私の前提は、ビューの処理を開始するようにGETリクエストを見ているということです。だから、なぜ、私は、この要求がPOSTの場合は、このすべてのビューを処理するこの条件があります。 – Neo1995

答えて

1
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} 
     else: 
      form_url = WebURLForm() 
      context = {"form_url":form_url, "title":None} 
    return render(request,'url.html',context) 

あなたはおそらくGETメソッドを使用して、ビューと呼ばれるので

+0

それはまだ同じエラーを与えて、インデントについてはありません。 – Neo1995

+0

okをチェックさせてください。必要に応じて答えを更新します – Exprator

+0

は答えを更新しました。お問合せください – Exprator

0

一度これを試してみてください。ここでは、コードです。また、PythonチュートリアルとPEP-8をお読みください。あなたのコードは醜いフォーマットであり、PEP-8標準に準拠していません。

関連する問題