0819 SeSAC(새싹) 3기 7일차

Django -> 라우팅 설정

장고에서 서버를 킬려면

  1. 프로젝트 폴더로 이동하세요(그 안에 manage.py가 있어요)
    manage.py가 있는 폴더가 프로젝트 폴더

python파일을 실행할 땐 python3로 입력

python3 manage.py runserver 0.0.0.0:(8000)
() 자리에 함부로 넣으면 안되는 숫자도 잇음 -> 80


filezilla 연결

호스트 27.96.135.73
사용자명 root
비밀번호 qa2213886*
포트 22


root@yookeunbyul:~# ls
b.html ssac-django
root@yookeunbyul:~# cd ssac-django
root@yookeunbyul:~/ssac-django# cd first_homepage/
root@yookeunbyul:~/ssac-django/first_homepage# ls
db.sqlite3 first_homepage manage.py member
root@yookeunbyul:~/ssac-django/first_homepage# python3 manage.py runserver 0.0.0.0:8000


프로젝트랑 동일한 이름에 앱 폴더에서 설정을 할 수 있음

root@yookeunbyul:~# ls
b.html ssac-django
root@yookeunbyul:~# cd ssac-django/
root@yookeunbyul:~/ssac-django# ls
first_homepage
root@yookeunbyul:~/ssac-django# cd first_homepage/
root@yookeunbyul:~/ssac-django/first_homepage# ls
db.sqlite3 first_homepage manage.py member
root@yookeunbyul:~/ssac-django/first_homepage# cd first_homepage/
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# ls
asgi.py init.py pycache settings.py urls.py wsgi.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# vi settings.py


routing 설정 -> 경로 설정

urls.py는 settings.py와 같은 곳에 있다.

!!우리만의 경로설정!!

include -> 다른 파일을 첨부할 수 있게 해줌!!

yookeunbyul이라는 경로로 들어오면 member 안에 urls로 갈거야

member 안에는 없지만 vi urls.py로 urls 파일을 만들자


프로젝트 폴더의 urls.py -> 최상단 접속 주소

1234543534543534535/yookeunbyul/dddsdddf/dddddd

yookeunbyul가 최상단 접속 주소


path("경로", 보여질 내용)

root@yookeunbyul:~# ls
b.html ssac-django
root@yookeunbyul:~# cd ssac-django
root@yookeunbyul:~/ssac-django# ls
first_homepage
root@yookeunbyul:~/ssac-django# cd first_homepage/
root@yookeunbyul:~/ssac-django/first_homepage# ls
db.sqlite3 first_homepage manage.py member
root@yookeunbyul:~/ssac-django/first_homepage# cd first_homepage/
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# ls
asgi.py init.py pycache settings.py urls.py wsgi.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# vi urls.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# vi urls.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# vi urls.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# vi urls.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# cd ..
root@yookeunbyul:~/ssac-django/first_homepage# ls
db.sqlite3 first_homepage manage.py member
root@yookeunbyul:~/ssac-django/first_homepage# cd memeber/
-bash: cd: memeber/: No such file or directory
root@yookeunbyul:~/ssac-django/first_homepage# cd member/
root@yookeunbyul:~/ssac-django/first_homepage/member# ls
admin.py init.py models.py tests.py
apps.py migrations pycache views.py
root@yookeunbyul:~/ssac-django/first_homepage/member# vi urls.py
root@yookeunbyul:~/ssac-django/first_homepage/member# cd ..
root@yookeunbyul:~/ssac-django/first_homepage# ls
db.sqlite3 first_homepage manage.py member
root@yookeunbyul:~/ssac-django/first_homepage# cd first_homepage/
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# ls
asgi.py init.py pycache settings.py urls.py wsgi.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# vi urls.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# cd ..
root@yookeunbyul:~/ssac-django/first_homepage# ls
db.sqlite3 first_homepage manage.py member
root@yookeunbyul:~/ssac-django/first_homepage# cd member/
root@yookeunbyul:~/ssac-django/first_homepage/member# ls
admin.py init.py models.py tests.py views.py
apps.py migrations pycache urls.py
root@yookeunbyul:~/ssac-django/first_homepage/member# vi urls.py
root@yookeunbyul:~/ssac-django/first_homepage/member# cd ..
root@yookeunbyul:~/ssac-django/first_homepage# ls
db.sqlite3 first_homepage manage.py member
root@yookeunbyul:~/ssac-django/first_homepage# cd first_homepage/
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# ls
asgi.py init.py pycache settings.py urls.py wsgi.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# vi urls.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# ls
asgi.py init.py pycache settings.py urls.py wsgi.py
root@yookeunbyul:~/ssac-django/first_homepage/first_homepage# cd ..
root@yookeunbyul:~/ssac-django/first_homepage# ls
db.sqlite3 first_homepage manage.py member
root@yookeunbyul:~/ssac-django/first_homepage# cd member/
root@yookeunbyul:~/ssac-django/first_homepage/member# ls
admin.py init.py models.py tests.py views.py
apps.py migrations pycache urls.py
root@yookeunbyul:~/ssac-django/first_homepage/member# vi urls.py


urls.py

from . import views
-> . (모든 기능 / 내 위치)

path('ok', views.hello)
-> views에 hello로 가겟다

http://27.96.135.73:8000/yookeunbyul/ok


views.py

render -> 파일을 만든다

from django.shortcuts import render

#Create your views here.

def hello( req ) :
return render( req, 'a.html')


views -> 백엔드
templates -> 프론트엔드

root@yookeunbyul:~/ssac-django/first_homepage/member# mkdir templates
root@yookeunbyul:~/ssac-django/first_homepage/member# ls
admin.py init.py models.py templates urls.py
apps.py migrations pycache tests.py views.py


head의 자식태그로 넣으시면돼요
태그안닫아도됩니당


views.py

from django.shortcuts import render

#Create your views here.

def hello( req ) :
a = 5
b = 8
c = a + b

return render( req, 'a.html', {'parameter1':c, 'parameter2':'육은별'})

a. html

hello
    <body>
            {{ parameter1 }}

            <div style="color:red;">
                    {{ parameter2 }}
            </div>
    </body>

  1. 장고 프로젝트 생성
  2. 장고 앱 생성
  3. 경로 설정
  4. 본인이 지정한 경로로 접속하면 동적인 페이지 출력

root@yookeunbyul:~# ls
b.html ssac-django
root@yookeunbyul:~# mkdir my-django
디렉토리 생성

root@yookeunbyul:~# ls
b.html my-django ssac-django
root@yookeunbyul:~# cd my-django/
root@yookeunbyul:~/my-django# ;s
-bash: syntax error near unexpected token `;'
root@yookeunbyul:~/my-django# ls
root@yookeunbyul:~/my-django# cd ..
root@yookeunbyul:~# ls
b.html my-django ssac-django
root@yookeunbyul:~# cd ssac-django/
root@yookeunbyul:~/ssac-django# ls
first_homepage
root@yookeunbyul:~/ssac-django# cd ..
root@yookeunbyul:~# ls
b.html my-django ssac-django
root@yookeunbyul:~# cd my-django/

root@yookeunbyul:~/my-django# django-admin startproject second_homepage
프로젝트 생성

root@yookeunbyul:~/my-django# ls
second_homepage
root@yookeunbyul:~/my-django# cd second_homepage/
프로젝트 폴더에 들어가서

root@yookeunbyul:~/my-django/second_homepage# ls
manage.py second_homepage
root@yookeunbyul:~/my-django/second_homepage# django-admin startapp member
앱 생성

root@yookeunbyul:~/my-django/second_homepage# ls
manage.py member second_homepage
root@yookeunbyul:~/my-django/second_homepage# cd second_homepage/
(프로젝트 폴더와 이름이 똑같은)앱 폴더에 들어가서

root@yookeunbyul:~/my-django/second_homepage/second_homepage# ls
asgi.py init.py settings.py urls.py wsgi.py
root@yookeunbyul:~/my-django/second_homepage/second_homepage# vi settings.py
셋팅 들어가서 ALLOWED_HOSTS = ["27.96.135.73"] 입력하고 INSTALLED_APPS에 'member' 작성

root@yookeunbyul:~/my-django/second_homepage/second_homepage# ls
asgi.py init.py settings.py urls.py wsgi.py
root@yookeunbyul:~/my-django/second_homepage/second_homepage# vi urls.py
urls.py 들어가서 최상단 접속 주소 입력

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('yookbyul/', include('member.urls'))
]

member에 urls로 가

root@yookeunbyul:~/my-django/second_homepage/second_homepage# ls
asgi.py init.py settings.py urls.py wsgi.py
root@yookeunbyul:~/my-django/second_homepage/second_homepage# cd ..
root@yookeunbyul:~/my-django/second_homepage# ls
manage.py member second_homepage
root@yookeunbyul:~/my-django/second_homepage# cd member/
앱에 들어가서

root@yookeunbyul:~/my-django/second_homepage/member# ls
admin.py apps.py init.py migrations models.py tests.py views.py
root@yookeunbyul:~/my-django/second_homepage/member# vi urls.py

from django.urls import path
from . import views

urlpatterns = [
path('no', views.hello)

]

두번째 경로 작성 views에 hello로 갈게

root@yookeunbyul:~/my-django/second_homepage/member# vi views.py

from django.shortcuts import render

#Create your views here.

def hello(req) :
return render(req, 'c.html')

hello는 c.html로 갈게

root@yookeunbyul:~/my-django/second_homepage/member# ls
admin.py init.py models.py urls.py
apps.py migrations tests.py views.py
root@yookeunbyul:~/my-django/second_homepage/member# mkdir templates

member에서 templates라는 디렉토리 생성

root@yookeunbyul:~/my-django/second_homepage/member# ls
admin.py init.py models.py tests.py views.py
apps.py migrations templates urls.py

root@yookeunbyul:~/my-django/second_homepage/member# cd templates/

templates로 들어가서

root@yookeunbyul:~/my-django/second_homepage/member/templates# ls
root@yookeunbyul:~/my-django/second_homepage/member/templates# vi c.html

c.html 작성

secondhomepage
    <body>
            hello
    </body>

root@yookeunbyul:~/my-django/second_homepage/member/templates#

작성하고 나와서 장고 서버키고

http://27.96.135.73:8000/yookbyul/no 이 링크로 들어가면 성공!

좋은 웹페이지 즐겨찾기