Fakultas Ilmu Komputer UI

views.py 1.94 KB
Newer Older
Samuel Dimas's avatar
Samuel Dimas committed
1
2
3
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
4
5
6
7
8
9
10
11
12
13
14
15
16
from django.views.generic import TemplateView 
from django.core import serializers
from .models import Materi

class DaftarKatalog(TemplateView):
    template_name = "app/katalog_materi.html"

    def get_context_data(self, **kwargs):
        return super().get_context_data(**kwargs)

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        context["materi_list"] = Materi.objects.all()
17
18
19
20
21
22
23
24
25
26
        return self.render_to_response(context=context)

class DetailMateri(TemplateView):
    template_name = "app/detail_materi.html"

    def get_context_data(self, **kwargs):
        return super().get_context_data(**kwargs)

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
27
28
29
30
        context = super().get_context_data(**kwargs)
        print(context['slug'])
        context["materi_data"] = get_object_or_404(Materi, pk=kwargs['slug'])
        print(context["materi_data"])
31
32
33
34
35
36
37
38
39
40
41
42
        return self.render_to_response(context=context)


# class DetailMateri(TemplateView):
#     template_name = "app/detail_materi.html"

#     def get_context_data(self, **kwargs):
#         return super().get_context_data(**kwargs)

#     def get(self, request, *args, **kwargs):
#         context = self.get_context_data(**kwargs)
#         return self.render_to_response(context=context)
Samuel Dimas's avatar
Samuel Dimas committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

def pages(request):
    context = {}
    # All resource paths end in .html.
    # Pick out the html file name from the url. And load that template.
    try:

        load_template = request.path.split('/')[-1]
        template = loader.get_template(load_template)
        return HttpResponse(template.render(context, request))

    except:

        template = loader.get_template('error-404.html')
        return HttpResponse(template.render(context, request))