728x90
반응형

Django에서 일정 거리내의 데이터를 조회하려면 다음과 같은 API를 사용하면되는데 데이터베이스에 따라 사용할 수 있는 것이 제한되어 있다.

 

SpatiaLite를 이용하는 경우에 distance_gt, distance_gte, distance_lt, distance_lte는 사용 가능하지만 해당 데이터베이스에서는 직교 좌표계에서 두점 간의 거리를 계산하는 방식으로 하기에 위도, 경도를 직교 좌표계에 맞게 바꿔서 저장해야 한다.

x = R * cos(위도) * cos(경도)
y = R * cos(위도) * sin(경도)

R 은 지구 반경 근사치로 약 6,371Km이다.

 

위와 같은 식으로 변환한 뒤 조회를 하면 일정 반경 이내 혹은 이외의 데이터를 조회할 수 있다.

반응형

'Python' 카테고리의 다른 글

[Python] Django logging  (0) 2020.01.25
[Python] nginx + gunicorn + Django  (0) 2020.01.25
[Python] nginx + Django  (0) 2020.01.25
728x90
반응형

Django에서 로그를 남기려면 다음과 같이 하면 된다.

 

settings.py에 다음을 추가한다.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/path/to/log/debug.log',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
        ‘myproject': {
            'handlers': ['file'],
            'level': 'DEBUG',
        },
    },
}

다음과 같이 작성하여 사용자 로그를 남길 수 있다.

# logging 라이브러리를 추가한다.
import logging

# logger 인스턴스를 가져온다.
# settings.py의 myproject에서 설정된 대로 로그를 남길 수 있다.
logger = logging.getLogger(‘myproject.{}’.format(__name__))

def my_view(request, arg1, arg):
    ...
    if bad_mojo:
        # 에러 메시지를 남긴다.
        logger.error('Something went wrong!')

__name__은 아래와 같은 구조에서 users/views.py 인 경우 users.views를 뜻한다.

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    users/
        __init__.py
        models.py
        views.py
        urls.py

 

참고문헌

  1. https://docs.djangoproject.com/en/1.8/topics/logging/

반응형

'Python' 카테고리의 다른 글

[Python] GeoDjango  (0) 2020.01.25
[Python] nginx + gunicorn + Django  (0) 2020.01.25
[Python] nginx + Django  (0) 2020.01.25
728x90
반응형

Django 프로젝트의 settings.py에 다음을 추가한다.

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

그리고 다음 명령어를 이용하여 Django에 내장된 static 파일을 지정한 디렉토리에 저장한다.

$ python manage.py collectstatic

gunicorn 관련된 설정은 공식 문서와 예제 파일을 참고하여 작성한다. 그리고 다음과 같은 명령어로 실행한다.

$ gunicorn -c gunicorn.conf.py {Django 프로젝트 이름}.wsgi &

nginx.conf에 다음과 같이 추가한다.

http {
 include /etc/nginx/mime.types;
 default_type application/octet-stream;

 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" "$http_x_forwarded_for"';

 access_log /var/log/nginx/access.log main;

 sendfile on;
 #tcp_nopush on;

 #keepalive_timeout 0;
 keepalive_timeout 65;

 #gzip on;

 # Load modular configuration files from the /etc/nginx/conf.d directory.
 # See http://nginx.org/en/docs/ngx_core_module.html#include
 # for more information.
 include /etc/nginx/conf.d/*.conf;

 index index.html index.htm;

 upstream app_server {
     server 127.0.0.1:8000;
     # For a TCP configuration:
     # server 192.168.0.7:8000 fail_timeout=0;
 }

 server {
     listen 80;
     server_name localhost;
     root /usr/share/nginx/html;

     #charset koi8-r;

     #access_log /var/log/nginx/host.access.log main;

     location /static {
         alias /path/static; # static 파일 위치
     }

     location / {
         # root html;
         # index index.html index.htm;
         try_files $uri @proxy_to_app;
     }

     location @proxy_to_app {
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_redirect off;
         proxy_pass http://app_server;
     }
}

위와 같이 수정한 설정 파일을 다시 읽는다.

$ sudo /etc/init.d/nginx reload

혹은

$ service nginx reload

static 파일을 제대로 불어오지 못하는 경우 nginx.conf에서 다음을 수정하면 된다.

# user nginx
# 기본으로 설정된 user가 nginx인 경우 root로 변경하거나
# static 파일 접근 권한이 있는 user로 변경한다.
user root

 

반응형

'Python' 카테고리의 다른 글

[Python] GeoDjango  (0) 2020.01.25
[Python] Django logging  (0) 2020.01.25
[Python] nginx + Django  (0) 2020.01.25
728x90
반응형

Django와 nginx를 연동하기 위해서 flup 라이브러리를 설치한다.

$ pip install flup

Django 실행은 다음과 같다.

$ python manage.py runfcgi method=prefork pidfile=PID_FILE host=127.0.0.1 port=8000

nginx.conf 에 다음과 같이 추가한다.

server {
    listen 80
    server_name localhost;

    location /static/ { #static url
        autoindex on;
        root /static/; #static 파일들이 저장된 디렉토리
    }
    location / { #사용할 url
        #root /usr/share/nginx/html;
        #index index.html index.htm;
        fastcgi_pass 127.0.0.1:8000;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
    }
}

static url을 설정할 때 /static/으로 해서 파일을 불러오지 못할 경우 앞이나 뒤의 /를 제거하면 된다.

 

위와 같이 수정한 설정 파일을 다시 읽는다.

$ sudo /etc/init.d/nginx reload

 

반응형

'Python' 카테고리의 다른 글

[Python] GeoDjango  (0) 2020.01.25
[Python] Django logging  (0) 2020.01.25
[Python] nginx + gunicorn + Django  (0) 2020.01.25

+ Recent posts