관리 메뉴

Leo's Garage

Django #1 프로젝트 셋업 및 기본 동작 확인 본문

Study/Django

Django #1 프로젝트 셋업 및 기본 동작 확인

LeoBehindK 2022. 12. 27. 23:58
728x90
반응형

Django 

그동안 이야기는 많이 들어왔지만, 사용해 본 적은 솔직히 없었다.

Python 공부할 겸 해서 간단한 프로젝트를 진행해볼까 한다. 

 

필자는 ubuntu 환경에서 개발한다. 

 

기본적으로 Python 프로젝트를 개발할 때면, 아나콘다를 통해 가상환경을 만들어서 사용하곤 했다. 

이번에는 poetry를 사용해볼까 한다. 

https://python-poetry.org/

 

Poetry - Python dependency management and packaging made easy

Dependency resolver Poetry comes with an exhaustive dependency resolver, which will always find a solution if it exists. And get a detailed explanation if no solution exists. Isolation Poetry either uses your configured virtualenvs or creates its own to al

python-poetry.org

python의 패키지 및 의존성 관리를 해주는 도구이다. 

poetry를 설치해보자 

curl -sSL https://install.python-poetry.org | python3 -

위와 같이 입력하게 되면, poetry가 설치된다. 

제대로 설치되었는지 확인해보려면 아래와 같이 입력해보면 된다.

poetry
Poetry (version 1.3.1)

Usage:
  command [options] [arguments]

Options:
  -h, --help                 Display help for the given command. When no command is given display help for the list command.
  -q, --quiet                Do not output any message.
  -V, --version              Display this application version.
      --ansi                 Force ANSI output.
      --no-ansi              Disable ANSI output.
  -n, --no-interaction       Do not ask any interactive question.
      --no-plugins           Disables plugins.
      --no-cache             Disables Poetry source caches.
  -C, --directory=DIRECTORY  The working directory for the Poetry command (defaults to the current working directory).
  -v|vv|vvv, --verbose       Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.

Available commands:
  about              Shows information about Poetry.
  add                Adds a new dependency to pyproject.toml.
  build              Builds a package, as a tarball and a wheel by default.
  check              Checks the validity of the pyproject.toml file.
  config             Manages configuration settings.
  export             Exports the lock file to alternative formats.
  help               Displays help for a command.
  init               Creates a basic pyproject.toml file in the current directory.
  install            Installs the project dependencies.
  list               Lists commands.
  lock               Locks the project dependencies.
  new                Creates a new Python project at <path>.
  publish            Publishes a package to a remote repository.
  remove             Removes a package from the project dependencies.
  run                Runs a command in the appropriate environment.
  search             Searches for packages on remote repositories.
  shell              Spawns a shell within the virtual environment.
  show               Shows information about packages.
  update             Update the dependencies as according to the pyproject.toml file.
  version            Shows the version of the project or bumps it when a valid bump rule is provided.

 cache
  cache clear        Clears a Poetry cache by name.
  cache list         List Poetry's caches.

 debug
  debug info         Shows debug information.
  debug resolve      Debugs dependency resolution.

 env
  env info           Displays information about the current environment.
  env list           Lists all virtualenvs associated with the current project.
  env remove         Remove virtual environments associated with the project.
  env use            Activates or creates a new virtualenv for the current project.

 self
  self add           Add additional packages to Poetry's runtime environment.
  self install       Install locked packages (incl. addons) required by this Poetry installation.
  self lock          Lock the Poetry installation's system requirements.
  self remove        Remove additional packages from Poetry's runtime environment.
  self show          Show packages from Poetry's runtime environment.
  self show plugins  Shows information about the currently installed plugins.
  self update        Updates Poetry to the latest version.

 source
  source add         Add source configuration for project.
  source remove      Remove source configured for the project.
  source show        Show information about sources configured for the project.

위와 같이 나온다면 poetry가 정상적으로 실행된 것이다.

하지만 만약에 실행이 되지 않는다면, PATH 설정이 제대로 되었는지 확인해야 한다. 

 

자 이제 poetry를 사용하여 가상 환경을 생성해보자

원하는 경로 내에서 아래와 같이 입력해보자. 

cd ./airbnb_clone_backend
poetry init

입력하게 되면 project 이름이나 description 등을 입력하라고 한다. 

입력하고 나면 pyproject.toml 파일이 생성된다. 

이제 해당 가상 환경 안으로 들어가야 한다. 

poetry shell

이제 가상환경으로 들어왔다. 

이제 필요한 패키지들을 추가 할 수 있다. 

poerty add django

django를 추가해보자 

그러면 toml 파일 내에 다음과 같이 나오게된다. 

[tool.poetry]
name = "aribnb-clone-backend"
version = "0.1.0"
description = ""
authors = 
license = "MIT"
readme = "README.md"
packages = [{include = "aribnb_clone_backend"}]

[tool.poetry.dependencies]
python = "^3.10"
django = "^4.1.4"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

 

 

자 이제 Django project를 생성해 볼 시간이다. 

django-admin startproject config .

위와 같이 입력하면 현재 내가 있는 폴더를 기준으로 Django 프로젝트를 생성하게 된다. 

파일 구성은 아래와 같다. 

myproject/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

 

자 이제 Django project를 실행해보도록 하자. 

python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
December 26, 2022 - 15:53:29
Django version 4.1.4, using settings 'config.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[26/Dec/2022 15:53:30] "GET /admin/ HTTP/1.1" 302 0

나온 주소를 들어가 보면 아래와 같이 서버가 켜지는 것을 확인할 수 있다. 

다시 콘솔에 나온 내용을 확인해보면 아래와 같은 문구를 확인할 수 있다. 

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

어떤 app들에 대해서 18가지 migration이 되지 않은 것들이 존재한다고 하는 것이다.

따라서 아래와 같이 입력해주면

python manage.py migrate

위의 에러가 사라진 것을 확인할 수 있다. 

위의 명령으로 Django에서 제공하는 기본적인 app들 admin, auth, contenttypes, sessions 등을 사용할 수 있다.

 

접속한 서버의 주소 뒤에 /admin을 입력하게 되면 아래와 같이 관리자 홈으로 들어가는 패널을 확인할 수 있다. 

해당 패널에 진입하기 위해서는 super user를 생성해줘야만 한다. 

관리자 계정은 다음의 명령어로 생성할 수 있다. 

python manage.py createsuperuser

username과 email, password를 입력하라고 한다. 

이렇게 생성된 계정을 가지고 admin 페이지에 가서 입력하고 접속하면 아래와 같은 관리자 페이지를 확인할 수 있다. 

Django는 거의 몇 번의 입력만에 관리자 페이지까지 생성해주었다. 

대단한 오픈소스이다. 

728x90
반응형

'Study > Django' 카테고리의 다른 글

Django #6 Make models more - 1  (0) 2023.01.02
Django #5 Users App custom -2  (0) 2023.01.02
Django #4 Users App custom -1  (0) 2022.12.30
Django #3 admin 기능 추가해보기  (0) 2022.12.28
Django #2 app 만들기  (0) 2022.12.28
Comments