#05-Django MTV

Kalau anda pernah menggunakan Framework MVC biasanya metode ini disebut Controller, tetapi kalau di sini fungsi Controller diletakan pada file views.py, mungkin ini perbedaan konsep MVC dengan MTV pada Django.

Contoh sederhana

View

Buat fungsi View pada polls/views.py, lalu tambahkan baris berikut :

def detail(request, question_id):
    return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
    response = "You're looking at the results of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

Buka file polls/ulrls.py, lalu tambahkan baris berikut :

from django.urls import path

from . import views

urlpatterns = [
    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'),
]

Buka browser seperti gambar di bawah ini:

details
results
vote

Gambar di atas menunjukan bahwa views menampilkan text sesuai kode pada route urls.py dan views.py

Memanggil Model

Buka views.py Edit def index sbb:

from django.http import HttpResponse

from .models import Question

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ', '.join([q.question_text for q in latest_question_list])
    return HttpResponse(output)

# Biarkan def di bawahnya (detail, results, vote)

Lalu lihat hasilnya

Memanggil Template

ubah def index polls/views.py

from django.http import HttpResponse
from django.template import loader

from .models import Question

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

# Biarkan def di bawahnya (detail, results, vote)

polls/templates/polls/index.html

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

Lihat hasilnya

view + template html
Shortcut: render() juga dapat dipersingkat sbb :
from django.shortcuts import render

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)

Error Exeption

Buka kembali polls/template/detail.html lalu ubah isinya sbb :

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

choice in question.choice_set.all digunakan untuk memanggil keselurah isi Choice dengan question_id=question.id

Buka kembali polls/views.py lalu ubah fungsi detail sbb :

def detail(request, question_id):
    try:
        question = Question.objects.get(pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request, 'polls/detail.html', {'question': question})

Perhatikan kode di atas, fungsi akan mencoba mendapatkan object yg dicari, jika object kosong maka akan menampilkan halaman error 404.

Error 404
Shortcut: get_object_or_404()

Selain menggunakan Exeption kita juga dapat menggunakan get_object_or_404()

Buka kembali polls/views.py lalu ubah fungsi detail sbb :

from django.shortcuts import get_object_or_404, render

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

Jika object kosong maka akan tampil Error 404

Trik menuliskan nama url pada template

Buka polls/urls.py tambahkan baris berikut:

from django.urls import path

from . import views

app_name="polls"
urlpatterns = [
    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'),
]

Panggil dari polls/template/detail.html

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

Perhatikan kode yang di tandai di atas

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>