Force django to use default language

class ForceDefaultLanguage(object):
    def process_request(self, request):
        if 'HTTP_ACCEPT_LANGUAGE' in request.META:
            del request.META['HTTP_ACCEPT_LANGUAGE']
Advertisements

Simple setup postgres db for django

sudo apt-get update
sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib
sudo su – postgres
psql
CREATE DATABASE uweb;
CREATE USER django WITH PASSWORD ‘django’;
ALTER ROLE django SET client_encoding TO ‘utf8’;
ALTER ROLE django SET default_transaction_isolation TO ‘read committed’;
ALTER ROLE django SET timezone TO ‘UTC’;
GRANT ALL PRIVILEGES ON DATABASE uweb TO django;

Render latest git date commit in django

Create file on server:

nano pull_repo.sh

#!/bin/sh

git –work-tree=/home/…web/ –git-dir=/home/…web/.git pull origin dev

git –work-tree=/home/…web/ –git-dir=/home/…web/.git log -1 –format=%cd > /home/…web/last.commit_date

chmod +x pull_repo.sh

Edit crontab:

crontab -e

Add line:

*/5 * * * * /home/…/pull_web.sh

In Django

decorator:

def prepare_commit_date(function):
    def wrap(request, *args, **kwargs):
        try:
            with open(os.path.join(BASE_DIR, "last.commit_date"), "r") as last_commit_date:
                request.commit_date = last_commit_date.readlines()[0].replace('\n', '')
        except Exception as ex:
            request.commit_date = str(ex.message)
        return function(request, *args, **kwargs)
    return wrap

view:

@prepare_commit_date
def index(request):
 return render(request, 'index.html', {})

template:

{{ request.commit_date }}

ubuntu, django, gunicorn, nginx

sudo apt-get install nginx python-pip libpq-dev supervisor

 

export LC_ALL=C

 

sudo pip install django gunicorn psycopg2

Open:

sudo nano /etc/nginx/sites-available/default

Paste:

server {
listen 80;
server_name 192.168.23.109;
access_log /var/log/nginx/access.log;

location /static/ {
alias /home/django/web/static;
}

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

 

For test guincorn:

gunicorn sg_web.wsgi:application

for re-test:

killall gunicorn

 

For autostart after reboot, open:

nano /etc/supervisor/conf.d/sg_web.conf

Paste:

[program:sg_web]

command=gunicorn sg_web.wsgi:application -c /home/django/sg_web/sg_web/gunicorn.conf.py

directory=/home/djangp/sg_web

user=nobody

autorestart=true

redirect_stderr=true

 

sudo systemctl enable supervisor

 

Tail -view logs

For example:

 

tail -f /var/log/upstart/gunicorn.log -n 500

tail -f /var/log/nginx/access.log -n 500

tail -f /var/log/nginx/error.log -n 500

For colored output:

sudo tail -f /var/log/nginx/access.log | sed -e ‘s/\(.* 200 .*\)/\o033[32m\1\o033[39m/’ -e ‘s/\(.* 301 .*\)/\o033[31m\1\o033[10m/’