python -m venv (가상환경 이름)
mkdir (프로젝트이름)
cd (폴더이름)
: 폴더 이동pip install django
conda install django
django-admin startproject (프로젝트이름) .
python manage.py migrate
python manage.py runserver
python manage.py startapp (app이름)
www.hojun.com/
www.hojun.com/about
www.hojun.com/contact
www.hojun.com/accounts/login
www.hojun.com/accounts/logout
www.hojun.com/blog
www.hojun.com/blog/1
www.hojun.com/blog/2
www.hojun.com/blog/3
URL, views, html 파일 등이 칼럼으로 있는 테이블 형태로 기획하고 넘어가는 것이 좋다.
# 프로젝트명 > urls.py에 반영!
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("main.urls")),
# ""URL로 들어오면 main앱에 urls.py로 연결하겠다.
# path('', index),
# path('about/', about),
# path('contact/', contact),
path("blog/", include("blog.urls")),
# "blog/"URL로 들어오면 blog앱에 urls.py로 연결하겠다.
# path("blog/<int:pk>/", blogdetails), # 여기서 처리하지 않고 blog앱에 urls.py에서 처리하면 됩니다!
# path('blog/', blog),
# path('blog/1', blog_1),
# path('blog/2', blog_2),
# path('blog/3', blog_3),
path("accounts/", include("accounts.urls")),
# path('accounts/login', login),
# path('accounts/logout', logout),
]
프로젝트에 대한 urls.py를 정의해 주었으면 똑같이 각각의 앱마다 urls.py를 정의해야 한다.
# main > urls.py
from django.urls import path
from .views import index, about, contact
urlpatterns = [
path("", index),
path("about/", about),
path("contact/", contact),
]
# main > views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("index")
def about(request):
return HttpResponse("about")
def contact(request):
return HttpResponse("contact")
# blog > urls.py
from django.urls import path
from .views import blog, blogdetails
urlpatterns = [
path("", blog),
path("<int:pk>/", blogdetails),
]
# blog > views.py
from django.shortcuts import render
from django.http import HttpResponse
def blog(request):
return HttpResponse("Blog Page")
def blogdetails(request, pk):
return HttpResponse(f"Blog Details Page: {pk}")
# accounts > urls.py
from django.urls import path
from .views import login, logout
urlpatterns = [
path("login/", login),
path("logout/", logout),
]
# accounts > views.py
from django.shortcuts import render
from django.http import HttpResponse
def login(request):
return HttpResponse("Login Page")
def logout(request):
return HttpResponse("Logout Page")
python manage.py runserver