Reputation: 63
I was following this Django deploying on Openshift tutorial https://github.com/rancavil/django-openshift-quickstart/wiki/Tutorial-How-create-an-application-with-Django-1.6-on-Openshift
However, I am getting the 500 Server erorr. Here is my code:
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'),
)
On localhost everything is working fine, but on server something goes wrong. Where is mistake? If you need any more info just say.
UPDATE:
Debug part of 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
Update(2):
I am using virtualenv and I have installed in it beautifulsoup4 and other libraries. Here is my requirements.txt content:
Django==1.6
YourAppName==1.0
argparse==1.2.1
beautifulsoup4==4.3.2
requests==2.3.0
wsgiref==0.1.2
Despite that, I am getting this error:
No module named bs4
Upvotes: 0
Views: 792
Reputation: 1312
Since you are getting an ImportError
exception, assuming you have all requirements installed, it can be a virtualenv problem. You should manually activate it when running on Openshift. Basically, the wsgi.py
in the root directory may contain something like this:
#!/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
Upvotes: 0
Reputation: 3867
You may check two more things:
First, what about your ALLOWED_HOSTS
setting in settings.py
? When you're on production (what means DEBUG
set to FALSE
) you need to set it correctly, otherwise you'll get HTTP 500
for every request. See Django docs here.
The second thing is in order to easily install the correct libraries and packages in your production environment you should edit your setup.py
like mentioned in the tutorial. Your error message tells that there is at least one module missing.
There is already a full example at your 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,
)
And it will be processed like this after pushing to your openshift repo:
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:
- Install Django 1.6 and all packages in install_require of the setup.py file.
- Execute the script deploy (file located in mysite/.openshift/action_hook)
- And execute the python script secure_db.py (file located in mysite/.openshift/action_hook) to create the password for the admin user.
Hope this helps!
Upvotes: 2