我跟随这个Django部署在Openshift教程https://github.com/rancavil/django-openshift-quickstart/wiki/Tutorial-How-create-an-application-with-Django-1.6-on-Openshift
上但是,我收到了500 Server 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上一切正常,但在服务器上出了问题。哪里出错?如果您还需要更多信息,请说明。
更新:
调试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 :(得分:2)
您可以再检查两件事:
首先,ALLOWED_HOSTS
中的settings.py
设置如何?当您正在制作时(意味着DEBUG
设置为FALSE
),您需要正确设置它,否则您将为每个请求获得HTTP 500
。 See 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='example@example.com',
url='https://pypi.python.org/pypi',
install_requires=packages,
)
在推送到您的openshift回购后,它将按照这样处理:
当我们进行推送时,所有任务都会自动执行以便安装并在Openshift上配置Django。这为我们在远程Openshift上执行$ python setup.py install。
基本上任务序列是:
- 在setup.py文件的install_require中安装Django 1.6和所有软件包。
- 执行脚本部署(位于mysite / .openshift / action_hook中的文件)
- 执行python脚本secure_db.py(位于mysite / .openshift / action_hook中的文件)为admin用户创建密码。
醇>
希望这有帮助!
答案 1 :(得分: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