본문 바로가기
개발 일지

TIL (24.05.01)

by 만식 2024. 5. 1.

 

출처 : https://spartacodingclub.kr/

 

스파르타코딩클럽

5주 완성! 코딩을 전혀 모르는 비개발자 대상의 웹개발 강의

spartacodingclub.kr

 

URL Namespace

  • Django는 서로 다른 앱에서 동일한 URL Name을 사용하는 경우, 고유하게 구분할 수 있도록 namespace를 지원한다. 
from django.urls import path
from . import views

app_name = "articles"

urlpatterns = [
		...
    path("hello/", views.hello, name="hello"),
    ...
]

 - url에 이름 공간을 만들어주고 나면, namespace:url_name 형태로 사용한다. 

{% url 'articles:hello' %}

redirect('articles:hello')

 

이전의 형태로 사용시, 에러가 발생함

수정하기

articles

url들의 Name을 참조하고 있는 모든 곳들을 수정해야 함

> 다음 프로젝트 만들 때는 처음부터 app_name을 지정하고 만들면 좋음!

 

{% url 'create' %}  -> {% url 'articles:create' %}

redirect('create') -> redirect('articles:create')

users

{% url 'profile' %}  -> {% url 'users:profile' %}

redirect('profile') -> redirect('users:profile')

 

Templates 구조

 

- "index.html"만 적어주면 django가 알아서 찾아옴

  • 만약, users앱에도 index.html이 있고 articles앱에도 index.html 이 있다면 어떻게 될까?
{% extends "base.html" %}


{% block content %}
    <h1>여기는 Users App의 INDEX</h1>
{% endblock %}
#users/templates/index.html

 


 

'개발 일지' 카테고리의 다른 글

TIL(24.05.03)  (0) 2024.05.07
TIL(24.05.02)  (1) 2024.05.02
TIL (24.04.30)  (0) 2024.04.30
TIL (24.04.29)  (0) 2024.04.29
TIL (24.04.26)  (1) 2024.04.28