Django: Difference between revisions
(38 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=installing django in debian jessie with python3, mysql, apache2 and mod-wsgi directly= | |||
Packages | ==Packages== | ||
aptitude install python3-django python3 libapache2-mod-wsgi-py3 python3-mysql.connector apache2 | aptitude install python3-django python3 libapache2-mod-wsgi-py3 python3-mysql.connector apache2 | ||
Line 8: | Line 8: | ||
aptitude install mysql-server | aptitude install mysql-server | ||
Make sure the | Make sure the wsgi module is enabled | ||
dpkg-reconfigure libapache2-mod-wsgi-py3 | dpkg-reconfigure libapache2-mod-wsgi-py3 | ||
Create workspace | ==Create workspace, in my case instance zealot is installed in /var/django/zealot== | ||
mkdir -p /var/django && cd /var/django | mkdir -p /var/django && cd /var/django | ||
/usr/lib/python3/dist-packages/django/bin/django-admin.py startproject zealot . | /usr/lib/python3/dist-packages/django/bin/django-admin.py startproject zealot . | ||
==Create your database (log into mysql first as a user that can do this)== | |||
CREATE DATABASE database; | |||
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; | |||
GRANT ALL ON database.* TO 'newuser'@'localhost'; | |||
(obviously these are placeholder values, use real names and a secure password!) | |||
==example apache config== | ==example apache config== | ||
Below apache config works with python 3 and the debian django repository (specifically, the static alias is on a separate location). Outside of that the django instance zealot is stored in /var/django/zealot. | Below apache config works with python 3 and the debian django repository (specifically, the static alias is on a separate location). Outside of that in my example the django instance named zealot is stored in /var/django/zealot. | ||
Apache config file location in debian: | |||
/etc/apache2/sites-available/000-default.conf | |||
Create a symlink to redirect to the distro provided static files: | |||
mkdir /var/django/media/ | |||
cd /var/django/media/ | |||
ln -s /usr/share/python-django-common/django/contrib/admin/static/admin | |||
<pre><VirtualHost *:80> | <pre><VirtualHost *:80> | ||
ServerName fuego.lippmann.us | ServerName fuego.lippmann.us | ||
Alias /media/ /var/django/media/ | Alias /media/ /var/django/media/ | ||
Alias /static/ / | Alias /static/ /var/django/static/ | ||
<Directory /var/django/static> | <Directory /var/django/static> | ||
Line 46: | Line 62: | ||
</VirtualHost></pre> | </VirtualHost></pre> | ||
==example settings.py== | |||
example settings | In my example /var/django/zealot holds this file and again the project is named zealot so settings match that. Adjust the database and other parameters (timezone, domain, etc.) according to your own environment. | ||
<pre>import os | <pre>import os | ||
Line 83: | Line 99: | ||
'USER': 'user', | 'USER': 'user', | ||
'PASSWORD': 'password', | 'PASSWORD': 'password', | ||
'HOST': 'localhost', | |||
'PORT': '3306', | |||
} | } | ||
} | } | ||
Line 91: | Line 109: | ||
USE_TZ = True | USE_TZ = True | ||
STATIC_URL = '/static/'</pre> | STATIC_URL = '/static/'</pre> | ||
=installing django in debian jessie with python, mysql, apache2 and mod-wsgi inside a virtualenv= | |||
==Packages== | |||
aptitude install python-virtualenv python libapache2-mod-wsgi apache2 python-pip virtualenv | |||
==create and activate the virtualenv== | |||
We create a virtualenv repository in /var, and create a virtualenv there specifically for our django instance. | |||
Then we initialize it. | |||
mkdir -p /var/virtualenv/ | |||
cd /var/virtualenv/ | |||
virtualenv django | |||
source /var/virtualenv/django/bin/activate | |||
==update pip and then install django== | |||
/var/virtualenv/django/bin/pip install -U pip | |||
/var/virtualenv/django/bin/pip install django | |||
django requires the right version of mysql-connector-python. It's a nightmare to find it, but below steps currently work: | |||
aptitude install git | |||
/var/virtualenv/django/bin/pip install --egg --allow-all-external git+git://github.com/multiplay/mysql-connector-python | |||
==Create workspace, in my case instance zealot is installed in /var/virtualenv/django/zealot== | |||
cd /var/virtualenv/django && bin/django-admin.py startproject zealot | |||
==add some optional packages== | |||
/var/virtualenv/django/bin/pip install termcolor | |||
/var/virtualenv/django/bin/pip install pytz | |||
/var/virtualenv/django/bin/pip install prettytable | |||
=model.py tricks= | |||
==enforce uniqueness of multiple fields in a model table== | |||
class SomeModel(models.Model): | |||
class Meta: | |||
unique_together = (("field1", "field2"),) |
Latest revision as of 05:30, 10 January 2016
installing django in debian jessie with python3, mysql, apache2 and mod-wsgi directly
Packages
aptitude install python3-django python3 libapache2-mod-wsgi-py3 python3-mysql.connector apache2
(optionally add a local mysql server if you need one locally)
aptitude install mysql-server
Make sure the wsgi module is enabled
dpkg-reconfigure libapache2-mod-wsgi-py3
Create workspace, in my case instance zealot is installed in /var/django/zealot
mkdir -p /var/django && cd /var/django /usr/lib/python3/dist-packages/django/bin/django-admin.py startproject zealot .
Create your database (log into mysql first as a user that can do this)
CREATE DATABASE database; CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL ON database.* TO 'newuser'@'localhost';
(obviously these are placeholder values, use real names and a secure password!)
example apache config
Below apache config works with python 3 and the debian django repository (specifically, the static alias is on a separate location). Outside of that in my example the django instance named zealot is stored in /var/django/zealot. Apache config file location in debian:
/etc/apache2/sites-available/000-default.conf
Create a symlink to redirect to the distro provided static files:
mkdir /var/django/media/ cd /var/django/media/ ln -s /usr/share/python-django-common/django/contrib/admin/static/admin
<VirtualHost *:80> ServerName fuego.lippmann.us Alias /media/ /var/django/media/ Alias /static/ /var/django/static/ <Directory /var/django/static> Require all granted </Directory> <Directory /var/django/media> Require all granted </Directory> WSGIDaemonProcess zealot python-path=/var/django:/usr/lib/python3.4 WSGIProcessGroup zealot WSGIScriptAlias / /var/django/zealot/wsgi.py <Directory /var/django/zealot/> <Files wsgi.py> Require all granted </Files> </Directory> </VirtualHost>
example settings.py
In my example /var/django/zealot holds this file and again the project is named zealot so settings match that. Adjust the database and other parameters (timezone, domain, etc.) according to your own environment.
import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) SECRET_KEY = 'key' DEBUG = False TEMPLATE_DEBUG = False ALLOWED_HOSTS = [ '.lippmann.us.', # Allow domain and subdomains ] CSRF_COOKIE_DOMAIN = ".lippmann.us" INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) ROOT_URLCONF = 'zealot.urls' WSGI_APPLICATION = 'zealot.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'mysql.connector.django', 'NAME': 'zealot', 'USER': 'user', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '3306', } } LANGUAGE_CODE = 'en-us' TIME_ZONE = 'America/Los_Angeles' USE_I18N = True USE_L10N = True USE_TZ = True STATIC_URL = '/static/'
installing django in debian jessie with python, mysql, apache2 and mod-wsgi inside a virtualenv
Packages
aptitude install python-virtualenv python libapache2-mod-wsgi apache2 python-pip virtualenv
create and activate the virtualenv
We create a virtualenv repository in /var, and create a virtualenv there specifically for our django instance. Then we initialize it.
mkdir -p /var/virtualenv/ cd /var/virtualenv/ virtualenv django source /var/virtualenv/django/bin/activate
update pip and then install django
/var/virtualenv/django/bin/pip install -U pip /var/virtualenv/django/bin/pip install django
django requires the right version of mysql-connector-python. It's a nightmare to find it, but below steps currently work:
aptitude install git /var/virtualenv/django/bin/pip install --egg --allow-all-external git+git://github.com/multiplay/mysql-connector-python
Create workspace, in my case instance zealot is installed in /var/virtualenv/django/zealot
cd /var/virtualenv/django && bin/django-admin.py startproject zealot
add some optional packages
/var/virtualenv/django/bin/pip install termcolor /var/virtualenv/django/bin/pip install pytz /var/virtualenv/django/bin/pip install prettytable
model.py tricks
enforce uniqueness of multiple fields in a model table
class SomeModel(models.Model): class Meta: unique_together = (("field1", "field2"),)