본문 바로가기

카테고리 없음

Django installiation 장고 설치

 

 

sudo apt-get update

sudo apt-get upgrade

 

sudo apt install python-pip

또는 python3용 

sudo apt install python3-pip

 

 

#장고 2.0 버전 설치

sudo pip3 install django

또는

sudo pip3 install django~=2.0.0

sudo apt-get install python3-venv

#가상환경 파일

python3 -m venv myvenv

#가상환경 활성화

source myvenv/bin/activate

#가상환경 중지(여기서는 스킵)

deactivate

 

django-admin startproject blog

 

 

 

 

 

 

 

 

기존의 Php 경우에 파일 하나에 database 입력, 출력, html코드 작성 출력이 모두 들어가 있어서 동시에 한개의 프로젝트를 여러명이서 작업할 경우 혼란이 발생할 가능성이 높았으나 Django 경우에 Model, View, Templete(=Control)  분리되어 있어서 database 입출력, 정리, 디자인 파트별로 관리가 수월하다.

  

 

 

 

 


 

python3 manage.py runserver 192.168.1.216:8000

 

 

 

 


https://tutorial.djangogirls.org/ko/django/

https://tutorial-extensions.djangogirls.org/ko/

 

#가상환경 설치

sudo apt install python3-venv

#가상환경 파일(폴더?) 생성

python3 -m venv myvenv

#가상환경 실행, 가상환경에서는 python 입력하면 python3 실행됨, 설치를 python3 해서 그런 같음.

source myvenv/bin/activate

#강상환경 종료는 $ deactivate

 

#최신버전 pip 업그레이드

python3 -m pip install --upgrade pip

 

#장고 2.0 버전 설치

sudo pip install django

또는

sudo pip install django~=2.0.0

 

 

#프로젝트(mysite) 생성

django-admin startproject mysite

 

cd /mysite/mysite

nano settings.py

#_url 및에 _root 추가

STATIC_URL = '/static/'

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

 

  

#allow host 변경

ALLOWED_HOSTS = ['192.168.1.216','127.0.0.1']

#또는 모두 공개

ALLOWED_HOSTS = [‘*’]

    ['www.example.com']  # Fully qualified domain

    ['.example.com']  # Subdomain wildcard, matches example.com and www.example.com

    ['*']  # Matches anything

 

 

cd ..

#설정 변경사항 저장

python manage.py migrate

 

# 앱폴더 생성

python manage.py startapp blog

 

# 앱추가에 따라 설정 다시 변경
nano ./mysite/settings.py

 

INSTALLED_APPS = [

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'blog',

]

 

# 모델 변경

nano ./blog/models.py

 

from django.db import models

from django.utils import timezone

 

 

class Post(models.Model):

    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)

    title = models.CharField(max_length=200)

    text = models.TextField()

    created_date = models.DateTimeField(

            default=timezone.now)

    published_date = models.DateTimeField(

            blank=True, null=True)

 

    def publish(self):

        self.published_date = timezone.now()

        self.save()

 

    def __str__(self):

        return self.title

 

 

python manage.py makemigrations blog

 

python manage.py migrate blog

 

nano ./blog/admin.py

from django.contrib import admin

from .models import Post

 

admin.site.register(Post)

 

 

#관리자 계정 생성

python manage.py createsuperuser


#
서버실행

python3 manage.py runserver 192.168.1.53:8000

 

 

 

# http://192.168.1.216:8000/admin/ 접속하면 관리창 접속

 


 

 

 

mysite/

    manage.py

    mysite/

        __init__.py

        settings.py

        urls.py

        wsgi.py

These files are:

  • The outer mysite/ root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details aboutmanage.py in django-admin and manage.py.
  • The inner mysite/ directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.

 

 

 

 

 Reference

https://blog.naver.com/roboholic84/221033386937

https://blog.hannal.com/category/start-with-django-lectures/

https://knowledgeofthings.com/django-on-raspberry-pi/2/