Computer Science/BackEnd 57

Docker compose로 Django, Nginx, Mysql 띄우기

docker compose로 django, nginx, mysql를 띄우면, 다음 두 개의 명령어만으로 서버를 띄울 수 있다. docker compose build docker compose up step 1. 디렉토리 구조 설정 나 같은 경우는 다음과 같이 구조를 설정했다. django, mysql, nginx를 따로 폴더를 만들어주고, 각 폴더에 Dockerfile을 생성했다. - django/ - mysql/ - nginx/ docker-compose.yaml step 2. django Dockerfile 작성 FROM python:3.8 # 컨테이너 내부 기준 디렉토리 생성 WORKDIR /app # django/requirements.txt를 컨테이너 app/requirements.txt로 복사..

에러 해결 | ‘bash\r’: No such file or directory 해결하기

window로 개발을 하다보면 에러가 생기는 경우가 많은데 특히 맥 쓰는 분이랑 협업할 때 문제가 많이 생긴다. 오늘은 docker-compose up으로 서버를 실행하려다가 ‘bash\r’: No such file or directory 에러가 발생했다. api-1 | /usr/bin/env: ‘bash\r’: No such file or directory api-1 exited with code 127 에러 로그는 위와 같다. 이 에러는 맥 OS은 \n을 이용해서 줄 구분을 하는데 윈도우는 \r\n을 이용해서 생기는 에러다. dos2unix pip 모듈을 설치해서 .sh 파일을 변환해주면 에러는 금방 해결된다.

FastAPI를 이용한 기본적인 CRUD API

참고 : 초보자를 위한 FastAPI 과정 - YouTube from fastapi import FastAPI from fastapi import Path from typing import Optional from pydantic import BaseModel app = FastAPI() students = { 1: { "name":"John", "age":17, "year":"year 12" } } class Student(BaseModel): name: str age: int year: str class UpdateStudent(BaseModel): name: Optional[str] = None age: Optional[int] = None year: Optional[str] = None @app...

[에러 해결] AttributeError: WebDriver object has no atribute find_element_by_class

page_num = 2 url = f"https://tomatolife.tistory.com/{page_num}" driver = webdriver.Chrome(executable_path="chromedriver") driver.get(url=url) try: element = WebDriverWait(driver, 5).until( EC.presence_of_element_located((By.CLASS_NAME, "contents_style")) ) search_box = driver.find_element_by_class_name("contents_style") print(search_box) finally: driver.quit() 셀레니움에서 dwebdriver.find_element_by_c..

장고에서 Gunicorn 사용하기 | 장고 공식 문서 번역

How to use Django with Gunicorn | Django documentation | Django (djangoproject.com) Django The web framework for perfectionists with deadlines. docs.djangoproject.com How to use Django with Gunicorn Gunicorn(Green Unicorn)은 UNIX를 위한 파이썬 WSGI 서버이다. Gunicorn은 python -m pip install gunicorn으로 바로 설치할 수 있다. Django를 generic WSGI 애플리케이션으로써 Gunicorn에서 실행하는 법 pip로 Gunicorn을 설치하고 나면, Gunicorn 서버 프로세스를 시작하..

장고 WSGI를 이용하여 배포하기 | 장고 공식 문서 번역

How to deploy Django | Django documentation | Django (djangoproject.com) Django The web framework for perfectionists with deadlines. docs.djangoproject.com How to deploy Django with WSGI WSGI는 장고의 주요한 배포 인터페이스이다. Django의 startproject 명령에서 WSGI configuration을 많은 부분 디폴트 값으로 설정해놓는다. 만약 필요하다면 프로젝트 상황에 맞게 수정하면 된다. The application object WSGI로 배포할 때 핵심은 application callable이라는 개념이다. application callab..

장고 배포하기(WSGI, Gunicorn, ASGI) | 장고 공식 문서 번역

How to deploy Django | Django documentation | Django (djangoproject.com) Django The web framework for perfectionists with deadlines. docs.djangoproject.com How to deploy Django 장고로 개발한 모든 것은 배포를 한 뒤에야 의미가 있다. 장고는 웹 프레임워크이기 때문에 운영을 위해서는 웹 서버가 필요하다. 그리고 대부분의 웹 서버가 파이썬과 소통할 수 없기 때문에 웹서버와 장고를 연결하는 인터페이스가 필요하다. 장고에는 두 가지 인터페이스가 존재한다 : WSGI, ASGI WSGI : Web Server Gateway Interface 웹 서버와 웹 애플리케이션이 소통하..

Django | SQL 쿼리 로그로 Django QuerySet 이해하기

이어지는 글 Django | Django ORM QuerySet 사용하기 — HappyTomatoLife (tistory.com) step 1. QuerySet 함수의 SQL문 이해하기 .filter >>> from post.models import Post, Comment, Tag, Clapse >>> queryset = Comment.objects.filter(post=1) >>> str(queryset.query) 'SELECT "post_comment"."id", "post_comment"."post_id", "post_comment"."created_by_id", "post_comment"."created_at", "post_comment"."updated_at", "post_comment"."..

Django | Django ORM QuerySet 사용하기

발단 Django로 작은 블로그를 만들고 있다. User, Post, Notification 세 개의 앱으로 구성된 프로젝트다. 앱 별로 주요 기능은 다음과 같다. User Follow/Following Profile (마이페이지) Post Post (블로그 포스트) Comment Clapse (좋아요/공감 격) Tag (#해시태그) Notification Notification (팔로우, 좋아요, 댓글 등을 알림으로 남겨줌) 기본적인 기능과 권한 설정, 페이지네이션 등은 어렵지 않게 구현하였는데, 데이터베이스에 데이터가 별로 없어도 리퀘스트 하나에 3~5초씩 걸리는 문제가 발생했다. 문제는 QuerySet에 있을 것이라 판단하여 QuerySet 성능 개선을 시도했다. 내가 사용한 쿼리는 .filter,..

Django for APIs | #6 Permissions

Permissions create a new user localhost:8000/admin 에서 user를 직접 추가해준다. add log in to the browsable API from django.contrib import admin from django.urls import path, include urlpatterns = [ path('api-auth/', include('rest_framework.urls')), path('api/v1/', include('posts.urls')), path('admin/', admin.site.urls), ] AllowAny 디폴트로 설정되어있는 것 REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': [ 'rest_fra..