본문 바로가기

728x90
반응형

Python

(100)
Python: moviepy # duration # 비디오 파일 재생시간 가져오기 Python moviepy library To get the total playing time of a video in Django, you can use the moviepy library, which is a powerful library for video editing in Python. First, you need to install the library using pip: pip install moviepy Then, you can use the VideoFileClip class from the moviepy.editor module to read the video file and obtain its duration: from moviepy.editor import VideoFileClip d..
Django: bulk create Djnago ORM query 중 bulk_create를 통해서 list에 class instance들을 담아서 한번에 생성할 수 있다. 한번의 쿼리로 여러 row를 생성 할수 있어서 DB에 overhead를 발생시키지 않는다. 주의할 점은, 데이터 무결성은 보장이 안되니 bulk_create 를 하려면 데이터 전처리나 검증을 확실히 해야 한다. ------------- 사용 예제 models.py from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=200) author = ..
Django 도메인 설정 후에 CSRF 설정 EC2 등에 Django web application을 등록하고 도메인을 설정한 경우, csrf 문제로 403 forbidden이 뜨는 경우가 있다. CSRF_TRUSTED_ORIGINS = [ "https://mysite.com", ] 위와 같은 방식으로 settings.py에 설정을 추가해줘야 한다. ALLOWED_HOST에서 "*" 모두 허용 했던걸로 해보면, 안먹힌다. ssl 인증을 받아서 http로 접속시 https로 redirect 되는 식으로 nginx를 설정해놨다면, http://mysite.com 으로 등록할 경우에도 403 forbidden 문제가 발생. 꼭 redirect 되어지는 https:// 로 CSRF_TRUSTED_ORIGINS에 추가해야 함 mysite.com 도 안되었음
isinstance() # check is instance of class # check type # datetime.date check type 예시 isinstance 활용 - datetime.date의 instance인지 확인하는 예시 import datetime updated = row.get('updated') if isinstance(row.get('updated'), datetime.date) else None 기본적인 내용 + 그 외 추가적인 내용은 아래 포스팅 https://taltal-dev-note.tistory.com/8
Nginx Django static file 경로 설정하기 실습 운영체제: Linux(Ubuntu) Nginx를 이용해서 Django web app을 배포할시에, static file 들을 경로를 설정해줘야 합니다. STATIC_URL, STATIC_ROOT 를 설정하고 python manage.py collectstatic STATIC_ROOT는 배포용에 쓰이는 static 경로이고 STATIC_DIRS는 개발단계에서 쓰이는 static 경로라 보면 됩니다. 2개를 가각 다르게 설정해줘야 합니다. 예를 들면 다음과 같이 하면 됩니다. STATIC_URL = '/static/' STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),] STATIC_ROOT = os.path.join(BASE_DIR, "var", "stat..
Django Deployment with Nginx and Gunicorn 1. Nginx 설치, 설정 설치 sudo apt-get update sudo apt-get install nginx 설정 sudo nano /etc/nginx/sites-available/default server { listen 80; server_name your_domain_name.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 설정후 nginx 재시작 sudo systemctl restart nginx 2. Gu..
Python, Django: request QueryDict 다루기 # AttributeError: This QueryDict instance is immutable Django 프로젝트에서, front에서 요청받은 request.POST의 query dictionary를 unpack해서 data를 생성할 때 (예: MODEL.objects.create(**request.POST)) request.POST에서 특정값만 없애고 unpack을 해서 사용하려는 경우, 다음과 같이 시도해보면 not_null_list = ['name','amount', 'period'] missing_item = next((item for item in not_null_list if not request.POST.get(item)), None) del request.POST['action'] Fruit.objects.create(**request.POST) raise AttributeErro..
Django: DB migrations 기록 지우기 Django를 통해 DB 설계를 한 경우 DB의 table에 django_migrations라는 table 생성됨 migrate를 하는 경우에 DB에 그 기록이 저장이 되는 table (migration 오류가 나는 경우에) 해당 table의 특정 app 내의 migration 기록을 지우는 방법 1. Django shell 이용 (터미널) $ python manage.py shell $ from django.db.migrations.recorder import MigrationRecorder $ from django.db import connection $ recorder.Migration.objects.filter(app='').delete() 2. Script로 지우기 from django.db.m..

728x90
반응형