장고 - Pythonanywhere 이용한 배포

1. Github 업로드

  • Github - new repository 생성
  • 해당 repo 주소 복사
  • 경로 : 프로젝트 디렉토리 위치
    • git init
    • git add -A . (git add 하기 전에 .gitignore 폴더 생성하여 git에 올리지 않을 항목 입력)
    • git commit -m ‘message’
    • git remote add origin ‘repo 주소’ (해당 repo주소를 ‘origin’으로 설정)
    • git push -u origin master

2. Pythonanywhere 가입 및 배포

  • 파이썬 애니웨어 > sign-up & login
  • 파이썬 애니웨어 > ‘bash’
    • git clone ‘repo 주소’
    • cd ‘프로젝트 디렉토리’
    • virtualenv –python=python3.6 venv
    • source venv/bin/activate
    • pip install django
    • pip install -r requirements.txt
  • Pycharm > 프로젝트 > settings.py

    1
    2
    DEBUG = False
    ALLOWED_HOST = ['.pythonanywhere.com','localhost','127.0.0.1']
  • 파이썬 애니웨어 > ‘bash’

    • python manage.py migrate
    • python manage.py createsuperuser
  • 파이썬 애니웨어 > ‘web’

    • ‘Add a new web app’ 클릭
    • ‘Manual configuration’ 클릭
    • ‘Python 3.x’ 클릭
    • ‘code’ > ‘WSGI Configuration file’

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      import os
      import sys
      path = '/home/(아이디)/(프로젝트 폴더명)/'
      if path not in sys.path:
      sys.path.append(path)

      os.environ.['DJANGO_SETTINGS_MODULE'] = 'config.settings'
      from django.core.wsgi import get_wsgi_application
      from django.contrib.staticfiles.handlers import StaticFilesHandler

      application = StaticFilesHandler(get_wsgi_application())
    • ‘Virtualenv’ 경로 지정 (‘bash’에서 ‘pwd’ 입력하여 나온 경로 붙여넣기)
      ex) /home/pstman7/wps_blog/venv

    • ‘Reload (아이디).pythonanaywhere.com’ 클릭

    • ‘(아이디).pythonanywhere.com’ 접속
  • 장고 프로젝트 > settings.py
    • STATIC_ROOT = os.path.join(BASE_DIR, ‘staticfiles’)
  • 파이썬 애니웨어 > ‘bash’
    • python manage.py collectstatic
  • 파이썬 애니웨어 > ‘web’
    • Static files:
      • URL –> ‘/static/‘ 설정
      • Directory –> ‘/home/[아이디]/[프로젝트 명]/staticfiles’ 설정