Skip to content

Instantly share code, notes, and snippets.

@LeoHeo
Last active June 4, 2016 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeoHeo/7813b55c455220a7136b01291097aef0 to your computer and use it in GitHub Desktop.
Save LeoHeo/7813b55c455220a7136b01291097aef0 to your computer and use it in GitHub Desktop.

Django에서 url 주소 느슨한 의존성

기존에는 의존성이 강해서 주소변경시 아래와 같이 변경을 많이 해야 했다. urls.py, header.html, 총 2개를 변경해 주어야만 했다.

기존주소 news -> watcha로 바뀌어야 할 때

urls.py

urlpatterns = [
  #url(r'^news/$', news)
  url(r'^watcha/$', news)
]

header.html

<!-- <li><a href="/news">News</a></li> -->
<li><a href="/watcha">News</a></li>

이렇게 매번 바꿔줘야만 했다. 작업이 비효율적이다.

그래서 Django에서는 아래와 같은 Template Language를 사용할수가 있다. urls.py에 name으로 값을 정해주면 header.html을 바뀔때마다 바꿀 필요가 없다

urls.py

#url(r'^news/$', news)
url(r'^news/$', news, name="news")

header.html

<!-- <li><a href="/news">News</a></li> -->
<li><a href="{% url "news"%}">News</a></li>

django_extensions

기본적으로 shell을 불러오면 settingsmodel를 import시켜줘야 했음

이런 불편한 점을 해소하기 위해 django_extensions shell_plus를 사용하면 됨

python wpsblog/manage.py shell_plus로 사용한다.

alias msp="python wpsblog/manage.py shell_plus"로 간편하게 사용

shell_plus를 사용하면 기본적으로 아래것들이 import가 됨

# Shell Plus Model Imports
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
# 현재까지 만들어진 model들
from wpsblog.models.naver_post import NaverPost
from wpsblog.models.posts import Post
# Shell Plus Django Imports
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.db.models import Avg, Count, F, Max, Min, Sum, Q, Prefetch, Case, When
from django.db import transaction
from django.core.cache import cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment