Skip to content

FEAT: Write views #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Start Django ✨
# Getting started with Django ✨
[Django Documentation Tutorial](https://docs.djangoproject.com/ko/3.2/intro/)을 따라가며 코드 및 배운 내용을 기록합니다.

- **기간** : 2021.08.23 ~ 2021.08.27
Expand All @@ -22,11 +22,11 @@

## Docs

| # | title | date |
| :--: | :-------------------------------------------------: | :--------: |
| 01 | [Start Django](docs/01-start_django.md) | 2021-08-23 |
| 02 | [Database and Admin](docs/02-database_and_admin.md) | 2021-08-24 |

| # | title | date |
| :--: | :----------------------------------------------: | :--------: |
| 01 | [Start Django](docs/01-start_django.md) | 2021.08.23 |
| 02 | [Database and Admin](docs/02-database_and_admin.md) | 2021.08.24 |
| 03 | [Views](docs/03-views.md) | 2021.08.24 |


## Reference
Expand Down
77 changes: 77 additions & 0 deletions docs/03-views.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 03. Views

## Views
뷰는 Django 어플리케이션이 특정 기능과 템플릿을 제공하는 웹페이지의 종류

### `polls/views.py`
```python
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404
from django.template import loader
from .models import Question


def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)


def detail(request, question_id):
# 객체가 존재하지 않으면 Http404 에러 발생하는 메서드
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})


def results(request, question_id):
response = f"You're looking at the results of question {question_id}"
return HttpResponse(response)


def vote(request, question_id):
return HttpResponse(f"You're voting on question {question_id}")
```

### `polls/urls.py`
```python
from django.urls import path
from . import views

app_name = 'polls' # URL의 이름공간
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),

# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),

# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),

# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
```

### `polls/templates/polls/index.html`
```html
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
```

### `polls/templates/polls/detail.html`
```html
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
```
Binary file modified mysite/mysite/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/mysite/__pycache__/settings.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/mysite/__pycache__/urls.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/mysite/__pycache__/wsgi.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/polls/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/polls/__pycache__/admin.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/polls/__pycache__/apps.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/polls/__pycache__/models.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/polls/__pycache__/urls.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/polls/__pycache__/views.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/polls/migrations/__pycache__/0001_initial.cpython-39.pyc
Binary file not shown.
Binary file modified mysite/polls/migrations/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions mysite/polls/templates/polls/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
9 changes: 9 additions & 0 deletions mysite/polls/templates/polls/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
11 changes: 11 additions & 0 deletions mysite/polls/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from django.urls import path
from . import views

app_name = 'polls'
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),

# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),

# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),

# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
26 changes: 23 additions & 3 deletions mysite/polls/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404
from django.template import loader
from .models import Question


def index(request):
return HttpResponse("Hello, world! You're at the polls index.")
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
# HttpResponse 객체와 자주 쓰는 표현 단축 : render
return render(request, 'polls/index.html', context)


def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})


def results(request, question_id):
response = f"You're looking at the results of question {question_id}"
return HttpResponse(response)


def vote(request, question_id):
return HttpResponse(f"You're voting on question {question_id}")