AI웹 개발자 과정 공부 (팀스파르타)/Django (장고)
24.04.18_TIL (3) (Django 다중 앱과 URL)
티아(tia)
2024. 4. 18. 19:58
728x90
++ 장고 공식 문서는 항상 확인하기
https://docs.djangoproject.com/
++ 파이썬 가상환경 실행 끄려면 ctrl + c 누르면 꺼지고 터미널 화면이 돌아옴
1. user 의 profile 만들기
- urls.py 에 path 만들기
path("users/<str:username>/", views.profile),
#str을 적지 않아도 기본적으로 str을 하지만 적어주는게 좋다.
- views.py 에 def 만들기
def profile(request, username):
context = {
"username" : username,
}
return render(request, "profile.html", context)
- profile.html 만들기
{% extends 'base.html' %}
{% block content %}
<h1> {{ username }} Profile Page </h1>
{% endblock content %}
- profile 에서의 화면 다르게하고 홈으로 돌아가기(index) 를 만들어보자.
{% extends 'base.html' %}
{% block content %}
<h1> {{ username }} 의 프로필 페이지 </h1>
<div>
<h2>username : {{ username }}</h2>
</div>
<a href="/index/">홈으로 돌아가기</a>
{% endblock content %}
2. Multiple Apps 만들기
- 폴더를 생성해준다.
python manage.py startapp users
- 기본 세팅을 해준다.
- users와 articles의 폴더에 urls.py 를 각각 만들어 주고 안에 기본 세팅을 한다.
- settings.py에 'users'를 추가해준다.
urlpatterns = []
- 각각의 파일을 정리
- my_first_pjt 의 urls.py 에 include 를 포함하여 다른 곳에 있는 urls로 갈 수 있게 만들어준다.
from django.contrib import admin
from django.urls import path, include
from articles import views
# urlpatterns는 어떤 path로 들어왔을 때 어디로 보낼지
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index),
path('articles/', include('articles.urls')),
path('users/', include('users.urls')),
]
- articles 에 urls.py 에 만들어준다.
from django.urls import path
from . import views #.은 내 위치
urlpatterns = [
path('hello/', views.hello),
path('data_throw/', views.data_throw),
path('data_catch/', views.data_catch),
]
- users 파일의 urls.py 를 만들어 준다.
from django.urls import path
from . import views
urlpatterns = [
path('', views.users),
path("<str:username>/", views.profile),
]
++ 여기서 path 의 빈 슬레시에 대한 부분은 경로를 잃을 수 있으니까 다시 설정해주어야 한다.
3. urls.py 정리하기
- articles 의 views.py에 있던 users 와 profile 를 옮겨준다.
from django.shortcuts import render
# Create your views here.
def users(request):
return render(request, "users.html")
def profile(request, username):
context = {
"username" : username,
}
return render(request, "profile.html", context)
- profile.html 과 users.html 을 users 의 templates 파일을 만들어 그 안으로 옮겨준다.
- 그럼 아래처럼 경로를 가게 되면 이동되는 것을 볼 수 있다.
- http://127.0.0.1:8000/articles/data_throw/로 갔다가 데이터를 입력하면 아래와 같은 에러가 뜬다.
- 경로가 articles/data_catch 로 가야하는데 그렇게 가지 못한것!
- 경로를 /articles/data_throw/ 로 바꾸어 주어야한다. data_catch에도 똑같이!
- 이렇게 계속 경로를 바꾸어주게 되면 힘드니까 별명을 지어주어서 이동을 쉽게 만들어 줄 수 있다.
from django.urls import path
from . import views #.은 내 위치
urlpatterns = [
path('hello/', views.hello, name='hello'),
path('data_throw/', views.data_throw, name='data_throw'),
path('data_catch/', views.data_catch, name='data_catch'),
]
{% extends 'base.html' %}
{% block content %}
<h1> data catch </h1>
<div>
<h2>Current Data</h2>
<p>Current data is: {{ message }}</p>
</div>
<a href = "{% url 'data_throw' %}"> 데이터 던지러 가기 </a>
{% endblock content %}
{% extends 'base.html' %}
{% block content %}
<h1> data throw </h1>
<form action="{% url 'data_catch' %}" method="GET">
<label for = 'message'> 데이터 입력 </label>
<input type = 'text' id = 'message' name='message'>
<button type = "submit"> 전송 </button>
</form>
{% endblock content %}
- urls.py의 각각의 경로 별명을 적어준다.
- index에 경로를 만들어 준다. (<br>은 한줄 띄우기)
{% extends 'base.html' %}
{% block content %}
<h1> INDEX </h1>
<a href = "{% url 'data_throw' %}"> throw 이동 </a><br>
<a href = "{% url 'hello' %}"> hello 이동 </a><br>
<a href = "{% url 'users' %}"> users 이동 </a>
{% endblock content %}
반응형