본문 바로가기
Django

[Django] path 및 앱 생성 명령어

by 기록하는_사람 2022. 12. 6.

path

📌 path([요청 url], [함수])

: 127.0.0.1:8000/ + 요청 url로  요청이 들어오면, 함수에서 처리함.

 

📄 config/urls.py

"""config URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

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

> 127.0.0.1:8000/admin/으로 요청이 들어오면, admin.site.urls에서 처리함. 

 

💡 url 조작이 일어난 후에는 로게트 페이지가 뜨지 않음. 

 

앱 생성 명령어

📌 앱 생성 명령어

python manage.py startapp

 

http:127.0.0.1:8000/hello/ 입력하면 'Hello:)' 띄우기

📄 http:127.0.0.1:8000/hello/ 입력하면 'Hello:)' 띄우기

① 'python manage.py startapp hello' 입력해 'hello' app 생성.

② 'http:127.0.0.1:8000/hello/'으로 요청이 들어오면, hello의 views의 hello 함수 호출되도록 함.

"""config URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from hello import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('hello/', views.hello),
]

③ hello의 views의 hello 함수 구현.

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def hello(requests):
    return HttpResponse("Hello:)")  # 화면에 Hello:) 띄움.

④ 'python manage.py runserver' 입력 후 서버 구동.

⑤ http:127.0.0.1:8000/hello/에 들어가면 'Hello:)'  확인 가능. 

'Django' 카테고리의 다른 글

[Django] ORM과 DB 등록하기  (0) 2022.12.12
[Django] HTML 파일로써 Client에게 응답 주기  (0) 2022.12.12
[Django] Django 설치 및 프로젝트 시작  (0) 2022.12.06
[Django] 가상 환경 설치  (0) 2022.12.06
[Django] Django  (0) 2022.12.06

댓글