[Django] Django 공부 노트 정리1
1. 템플릿 필터 {% url %} 에서 url 다음에 오는 변수 인자들이 여러개일때 어떻게 ?
https://docs.djangoproject.com/ko/4.0/intro/tutorial03/
첫 번째 장고 앱 작성하기, part 3 | Django 문서 | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
위의 주소상에서 제일 하단부에 url 용법이 나온다.
[ /polls/urls.py ]
1
2
3
4
5
6
7
8
9
10
11
|
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
|
cs |
[ /polls/templates/polls/index.html ]
1
|
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
|
cs |
1
|
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
|
cs |
2. 템플릿 필터 ' | ' 의 다양한 용법들
{% if cart|length %}
{% endif %}
cart 다음의 length는 cart 라는 List 데이터가 있다면 그 갯수를 리턴시켜준다.
cart.count 와 동일하다.
https://docs.djangoproject.com/en/4.0/ref/templates/language/#filters
The Django template language | Django documentation | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
https://docs.djangoproject.com/en/4.0/ref/templates/builtins/
Built-in template tags and filters | Django documentation | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
3. get_list_or_404() 와 get_object_or_404()
https://docs.djangoproject.com/ko/4.0/intro/tutorial03/
첫 번째 장고 앱 작성하기, part 3 | Django 문서 | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
https://docs.djangoproject.com/ko/4.0/topics/http/shortcuts/#django.shortcuts.get_object_or_404
Django shortcut functions | Django 문서 | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
4. view.py 상에서 redirect() 함수 용법 - 곧바로 해당 주소로 redirect 시킨다.
render() 함수도 비슷하게 각종 데이터와 함께 원하는 주소파일로 이동시킨다.
하지만, 상기 주소창에 변화하지는 않는다.
https://docs.djangoproject.com/ko/4.0/topics/http/shortcuts/
Django shortcut functions | Django 문서 | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
5. reverse() 내장함수 - 원하는 페이지로 이동시킨다.
관련 변수들도 인자로 같이 보낼수 있다.
인자는 리스트형(혹은 튜플)이나 딕셔너리 둘다 모두 가능하다.
아래의 소스중에서 args 는 리스트형 , kwargs 는 딕셔너리형이다.
reverse('blog:detail', args=[200])
reserve('blog:list', kwargs={'boards': boards})
https://docs.djangoproject.com/ko/4.0/ref/urlresolvers/
django.urls utility functions | Django 문서 | Django
Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate
docs.djangoproject.com
https://ugaemi.github.io/django/Django-reverse-and-resolve/
🔫 Django reverse()와 resolve()
reverse() python 코드 안에서 URL 템플릿 태그와 비슷하게 동작하는 기능이다. 에서 설정한 URL의 이나, viewname을 통해서 다시 URL로 되돌릴 수 있다. 인수가 있는 URL이라면 다음과 같이 를 포함할 수 있
ugaemi.github.io