2014-06-23 111 views
0

我下面這個Django的上Openshift教程部署https://github.com/rancavil/django-openshift-quickstart/wiki/Tutorial-How-create-an-application-with-Django-1.6-on-Openshift的Django部署在Openshift錯誤500

不過,我收到500服務器erorr。這裏是我的代碼:

views.py

# -*- coding: utf-8 -*- 

from django.shortcuts import render 
from bs4 import BeautifulSoup 
from collections import namedtuple 
import requests 

def results(request): 
    final_data = [] 
    url ="http://www.15min.lt/cempionatas/futbolas/pasaulio-futbolo-cempionatas-2014?from=fifa-menu" 
    r = requests.get(url) 
    soup = BeautifulSoup(r.content) 
    tables = soup.find_all('table') 
    for table in tables: 
     row = table.find_all('tr') 
     data = [[td.text.strip().encode("utf-8") for td in tr.findAll("td")] for tr in row] 
     final_data += data 
    group1 = final_data[:7] 
    group2 = final_data[7:14] 
    group3 = final_data[14:21] 
    group4 = final_data[21:28] 
    group5 = final_data[28:35] 
    group6 = final_data[35:42] 
    group7 = final_data[42:49] 
    group8 = final_data[49:56] 
    return render(request, 'results.html', {"group1": group1, "group2": group2, "group3": group3, "group4": group4, "group5": group5, 
    "group6": group6, "group7": group7, "group8": group8 }) 

def news(request): 
    Link = namedtuple('Link', ['title', 'url'], verbose=True) 
    links = [] 
    url ="http://www.15min.lt/naujienos/sportas/fifa-2014/" 
    r = requests.get(url) 
    soup = BeautifulSoup(r.content) 
    titles = soup.findAll("div", attrs={"class": "vl-article-title"}) 
    for i in titles: 
     links.append(Link(i.find("h3").find("span").find("a").text, url=i.find("h3").find("span").find("a")["href"])) 
    return render(request, 'News.html', {"links": links}) 

def tables(request): 
    Datas = namedtuple('Datas', 'string url', verbose=True) 
    url ="http://www.uefa.com/worldcup/season=2014/standings/" 
    final_data = [] 
    r = requests.get(url) 
    soup = BeautifulSoup(r.content) 
    tables = soup.find_all('table') 
    for table in tables: 
     row = table.find_all('tr') 
     data = [[td.text.strip().encode("utf-8") for td in tr.findAll("td")] for tr in row] 
     head = [[th.text.strip().encode("utf-8") for th in tr.findAll("th")] for tr in row] 
     for i in data: 
       if i: 
      if i[1] == "Netherlands": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/ned.png")) 
      elif i[1] == "Cameroon": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/cmr.png")) 
      elif i[1] == "Spain": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/esp.png")) 
      elif i[1] == "Côte d'Ivoire": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/civ.png")) 
      elif i[1] == "Japan": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/jpn.png")) 
      elif i[1] == "Costa Rica": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/crc.png")) 
      elif i[1] == "Switzerland": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/sui.png")) 
      elif i[1] == "Bosnia and Herzegovina": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/bih.png")) 
      elif i[1] == "Iran": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/irn.png")) 
      elif i[1] == "Nigeria": i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/nga.png")) 
      else: 
       flag = i[1][:3] + ".png" 
        i.insert(1, Datas(i, "http://img.uefa.com/imgml/flags/18x18/" + flag)) 
     final_data += data 
     group1 = final_data[:5] 
     group2 = final_data[5:10] 
     group3 = final_data[10:15] 
     group4 = final_data[15:20] 
     group5 = final_data[20:25] 
     group6 = final_data[25:30] 
     group7 = final_data[30:35] 
     group8 = final_data[35:40] 

    return render(request, 'Groups.html', {"group1": group1, "group2": group2, "group3": group3, "group4": group4, "group5": group5, 
    "group6": group6, "group7": group7, "group8": group8 }) 

urls.py

from django.conf.urls import patterns, include, url 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^$', 'views.home', name='home'), 
    url(r'^tvarkarastis/', 'polls.views.results', name='results'), 
    url(r'^lenteles/', 'polls.views.tables', name='results'), 
    url(r'^naujienos/', 'polls.views.news', name='naujienos'), 
) 

在localhost一切正常,但在服務器出現問題。錯誤在哪裏?如果你需要更多信息,請說。

UPDATE:settings.py的

調試部分:

if ON_OPENSHIFT: 
    DEBUG = bool(os.environ.get('DEBUG', False)) 
    if DEBUG: 
     print("WARNING: The DEBUG environment is set to True.") 
else: 
    DEBUG = True 

更新(2):

我使用的virtualenv和我已經安裝在它beautifulsoup4和其他庫。這裏是我的requirements.txt內容:

Django==1.6 
YourAppName==1.0 
argparse==1.2.1 
beautifulsoup4==4.3.2 
requests==2.3.0 
wsgiref==0.1.2 

儘管如此,我收到此錯誤:

No module named bs4 
+0

請在settings.py中更改'DEBUG = TRUE'。 – Silwest

+1

爲什麼?在部署時不應該是錯誤的嗎? – user3767139

+0

如果我們沒有看到發生了什麼,我們怎麼能告訴你什麼是錯的? – Silwest

回答

0

由於您遇到了ImportError異常,假設您已經安裝了所有需求,它可能是一個虛擬環境問題。在Openshift上運行時,您應該手動激活它。基本上,根目錄中的wsgi.py可能包含如下內容:

#!/usr/bin/python 
import os 

virtenv = os.path.join(os.environ['OPENSHIFT_PYTHON_DIR'], 'virtenv') 
virtualenv = os.path.join(virtenv, 'bin', 'activate_this.py') 
try: 
    execfile(virtualenv, dict(__file__=virtualenv)) 
except IOError: 
    pass 

from yourproject.wsgi import application 
2

您可以檢查兩兩件事:

第一,怎麼樣在你ALLOWED_HOSTS設置settings.py?當您正在進行生產時(意味着DEBUG設置爲FALSE),您需要正確設置它,否則您會爲每個請求獲得HTTP 500See Django docs here

第二件事是爲了在生產環境中輕鬆地安裝正確的庫和包,您應該編輯您的setup.py,就像教程中提到的一樣。您的錯誤消息告訴您至少有一個模塊丟失。

已經有一個完整的例子在你tutorial

# Put here required packages 
packages = ['Django<=1.6',] 

# This is if you like use redis cloud w/Django... 
if 'REDISCLOUD_URL' in os.environ and 'REDISCLOUD_PORT' in os.environ and 
    'REDISCLOUD_PASSWORD' in os.environ: 
    packages.append('django-redis-cache') 
    packages.append('hiredis') 

setup(name='YourAppName',   # <= Put your application name, in this case 'mysite' 
     version='1.0', 
     description='OpenShift App', # <= Put your description if you want 
     author='Your Name',   # <= Your name!!!! 
     author_email='[email protected]', 
     url='https://pypi.python.org/pypi', 
     install_requires=packages, 
) 

它會像這樣推到你的openshift回購後進行處理:

When we make the push, all task are execute automatically for install and configure Django on Openshift. This executes $ python setup.py install on the remote Openshift for us.

Basically the tasks sequence are:

  1. Install Django 1.6 and all packages in install_require of the setup.py file.
  2. Execute the script deploy (file located in mysite/.openshift/action_hook)
  3. And execute the python script secure_db.py (file located in mysite/.openshift/action_hook) to create the password for the admin user.

希望這有助於!