Fakultas Ilmu Komputer UI

views.py 22.6 KB
Newer Older
1
2
3
4
import mimetypes
import os

from django.conf import settings
5
from django.contrib.auth.models import AnonymousUser
6
from django.contrib import messages
7
from django.core import serializers
8
from django.core.exceptions import PermissionDenied, ValidationError
9
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
Mika dabelza abi's avatar
Mika dabelza abi committed
10
from django.db.models import Q, Count
11
12
13
14
from django.http import (Http404, HttpResponse, HttpResponseRedirect,
                         JsonResponse)
from django.shortcuts import get_object_or_404, redirect, render
from django.template import loader
15
from django.urls import reverse
igor lestin sianipar's avatar
igor lestin sianipar committed
16
from django.views.generic import TemplateView, ListView
17
from .models import Category, Comment, Materi
igor lestin sianipar's avatar
igor lestin sianipar committed
18
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
19
from administration.models import VerificationReport
20
from app.forms import SuntingProfilForm, UploadMateriForm
21
from app.models import Category, Comment, Materi, Like, ViewStatistics, DownloadStatistics, ReqMaterial
22
from app.utils.fileManagementUtil import get_random_filename, remove_image_exifdata
23
from authentication.models import User
24
import django
25

26
27

class DaftarKatalog(TemplateView):
igor lestin sianipar's avatar
igor lestin sianipar committed
28
    paginate_by = 2
29
30
31
32
33
34
35
    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)
36
        context["kategori_list"] = Category.objects.all()
37

Mika dabelza abi's avatar
Mika dabelza abi committed
38
        lstMateri = Materi.objects.filter(status="APPROVE").order_by("date_modified")
Mika dabelza abi's avatar
Mika dabelza abi committed
39
40
        url = ""

41
        getSearch = request.GET.get('search')
42
        if getSearch:
Mika dabelza abi's avatar
Mika dabelza abi committed
43
            url = url + "&search={0}".format(getSearch)
Mika dabelza abi's avatar
Mika dabelza abi committed
44
            lstMateri = lstMateri.filter(
45
                Q(title__icontains=getSearch) |
46
                Q(author__icontains=getSearch) |
47
48
                Q(uploader__name__icontains=getSearch) |
                Q(descriptions__icontains=getSearch) |
49
                Q(publisher__icontains=getSearch)
50
            ).distinct()
Saul Andre's avatar
Saul Andre committed
51

52
53
        getKategori = request.GET.get("kategori")
        if getKategori:
Mika dabelza abi's avatar
Mika dabelza abi committed
54
55
            url = url + "&kategori={0}".format(getKategori)
            kategori = Category.objects.get(pk=getKategori)
Mika dabelza abi's avatar
Mika dabelza abi committed
56
            lstMateri = lstMateri.filter(categories=kategori.pk)
57

58
        getSort = request.GET.get("sort")
59
        if getSort:
Mika dabelza abi's avatar
Mika dabelza abi committed
60
            url = url + "&sort={0}".format(getSort)
61
            if(getSort == "judul"):
Mika dabelza abi's avatar
Mika dabelza abi committed
62
                lstMateri = lstMateri.order_by('title')
63
            elif(getSort == "penulis"):
Mika dabelza abi's avatar
Mika dabelza abi committed
64
                lstMateri = lstMateri.order_by('author')
65
            elif(getSort == "pengunggah"):
Mika dabelza abi's avatar
Mika dabelza abi committed
66
                lstMateri = lstMateri.order_by('uploader')
Mika dabelza abi's avatar
Mika dabelza abi committed
67
            elif(getSort == "terbaru"):
Mika dabelza abi's avatar
Mika dabelza abi committed
68
                lstMateri = lstMateri.order_by('-date_created')
Mika dabelza abi's avatar
Mika dabelza abi committed
69
            elif(getSort == "terlama"):
Mika dabelza abi's avatar
Mika dabelza abi committed
70
                lstMateri = lstMateri.order_by('date_created')            
Mika dabelza abi's avatar
Mika dabelza abi committed
71
            elif(getSort == "terpopuler"):
Mika dabelza abi's avatar
Mika dabelza abi committed
72
                lstMateri = lstMateri.annotate(count=Count('like__id')).order_by('-count')
Mika dabelza abi's avatar
Mika dabelza abi committed
73
                    
Mika dabelza abi's avatar
Mika dabelza abi committed
74
        context["materi_list"] = lstMateri
Mika dabelza abi's avatar
Mika dabelza abi committed
75
76
77
78
79
80
        paginator = Paginator(context["materi_list"], 15)
        page_number = request.GET.get('page')
        page_obj = paginator.get_page(page_number)
        context["materi_list"] = page_obj

        context["url"] = url
81
82
        return self.render_to_response(context=context)

83

84
85
86
87
class DetailMateri(TemplateView):
    template_name = "app/detail_materi.html"

    def get_context_data(self, **kwargs):
88
89
90
91
92
93
        context = super(DetailMateri, self).get_context_data(**kwargs)
        if not self.request.session or not self.request.session.session_key:
            self.request.session.save()
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
        context["session_id"] = self.request.session.session_key
        context["materi_data"] = materi
94
        context["report"] = VerificationReport.objects.filter(materi=materi)
95
96
97
        context["has_liked"] = Like.objects.filter(
            materi=materi, session_id=self.request.session.session_key).exists()
        return context
98
99
100

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
101
102
        query_set_for_comment = Comment.objects.filter(
            materi=context["materi_data"])
103
        context["comment_data"] = query_set_for_comment
104
105
        return self.render_to_response(context=context)

106
107
    def get_user_name(self, request):
        user_name = ""
108
109
110
        if isinstance(request.user, AnonymousUser):
            user_name = "Anonim"
        elif request.user.is_admin:
111
112
113
114
115
            user_name = "Admin"
        elif request.user.is_contributor:
            user_name = request.user.name
        return user_name

116
    def post(self, request, *args, **kwargs):
117
        commentText = request.POST.get("comment", None)
118
        if commentText == None or commentText == "":
119
120
            context = self.get_context_data(*args, **kwargs)
            context["error_message"] = "Anda belum menuliskan komentar"
121
            context["materi_data"] = get_object_or_404(Materi, pk=kwargs["pk"])
122
123
            query_set_for_comment = Comment.objects.filter(
                materi=context["materi_data"])
124
            context["comment_data"] = query_set_for_comment
125
            return self.render_to_response(context=context)
126

127
        materi = get_object_or_404(Materi, pk=kwargs["pk"])
128
129
130
        user_obj = request.user if request.user.is_authenticated else None
        comment = Comment.objects.create(comment=commentText,
                                         username=self.get_user_name(request), materi=materi, user=user_obj)
131
132
133
        comment.save()
        return HttpResponseRedirect(request.path)

134

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def toggle_like(request):
    if request.method == 'POST':
        materi_id = request.POST.get('materi_id', None)
        session_id = request.POST.get('session_id', None)
        if materi_id is None or session_id is None:
            return JsonResponse({"success": False, "msg": "Missing parameter"})
        materi = get_object_or_404(Materi, pk=materi_id)
        has_liked = Like.objects.filter(
            materi=materi, session_id=session_id).exists()
        if has_liked:
            like = get_object_or_404(
                Like, materi=materi, session_id=session_id)
            like.delete()
            return JsonResponse({"success": True, "liked": True})
        else:
            Like(materi=materi, session_id=session_id).save()
            return JsonResponse({"success": True, "liked": False})
    else:
        return JsonResponse({"success": False, "msg": "Unsuported method"})


156
157
158
159
160
161
def delete_comment(request, pk_materi, pk_comment):
    comment = get_object_or_404(Comment, pk=pk_comment)
    url = '/materi/' + str(pk_materi) + "/"
    comment.delete()
    return HttpResponseRedirect(url)

162

163
164
165
166
167
def download_materi(request, pk):
    materi = get_object_or_404(Materi, pk=pk)
    path = materi.content.path
    file_path = os.path.join(settings.MEDIA_ROOT, path)
    if os.path.exists(file_path):
168
169
170
171
172
173
174
175
176
177
        try:
            mimetype = mimetypes.guess_type(file_path)
            with open(file_path, "rb") as fh:
                response = HttpResponse(fh.read(), content_type=mimetype[0])
                response["Content-Disposition"] = "attachment; filename=" + \
                    os.path.basename(file_path)
                DownloadStatistics(materi=materi).save()
                return response
        except Exception as e:
            raise Http404("File tidak dapat ditemukan.")
178
179
180
    else:
        raise Http404("File tidak dapat ditemukan.")

igor lestin sianipar's avatar
igor lestin sianipar committed
181

182
183
184
185
186
187
def view_materi(request, pk):
    materi = get_object_or_404(Materi, pk=pk)
    path = materi.content.path
    file_path = os.path.join(settings.MEDIA_ROOT, path)
    if os.path.exists(file_path):
        mimetype = mimetypes.guess_type(file_path)
188
189
190
191
192
193
194
195
196
        try:
            with open(file_path, "rb") as fh:
                response = HttpResponse(fh.read(), content_type=mimetype[0])
                response["Content-Disposition"] = "inline; filename=" + \
                    os.path.basename(file_path)
                ViewStatistics(materi=materi).save()
                return response
        except Exception as e:
            raise Http404("File tidak dapat ditemukan.")
197
198
    else:
        raise Http404("File tidak dapat ditemukan.")
igor lestin sianipar's avatar
igor lestin sianipar committed
199

igor lestin sianipar's avatar
igor lestin sianipar committed
200

igor lestin sianipar's avatar
igor lestin sianipar committed
201
class UploadMateriView(TemplateView):
202
    template_name = "unggah.html"
igor lestin sianipar's avatar
igor lestin sianipar committed
203
204
205
206
207
208
    context = {}

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

Saul Andre's avatar
Saul Andre committed
209
    def post(self, request, *args, **kwargs):
igor lestin sianipar's avatar
igor lestin sianipar committed
210
211
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)
212
        form = UploadMateriForm(request.POST, request.FILES)
igor lestin sianipar's avatar
igor lestin sianipar committed
213
214
215
        if form.is_valid():
            materi = form.save(commit=False)
            materi.uploader = request.user
216
217
218
219
220
221
            konten = form.cleaned_data['content']
            try:
                self.validate_file_extension(konten)
            except ValidationError:
                messages.error(request, "Materi gagal diunggah, format file tidak sesuai")
                return HttpResponseRedirect("/unggah/")
igor lestin sianipar's avatar
igor lestin sianipar committed
222
            materi.save()
223
224
225
            kateg = form.cleaned_data['categories']
            for i in kateg:
                materi.categories.add(i)
igor lestin sianipar's avatar
igor lestin sianipar committed
226
227
            messages.success(
                request, "Materi berhasil diunggah, periksa riwayat unggah anda")
228
            return HttpResponseRedirect("/unggah/")
Saul Andre's avatar
Saul Andre committed
229
        else:
igor lestin sianipar's avatar
igor lestin sianipar committed
230
            context = self.get_context_data(**kwargs)
231
            context["form"] = form
232
            messages.error(request, "Terjadi kesalahan pada pengisian data")
igor lestin sianipar's avatar
igor lestin sianipar committed
233
234
235
236
237
238
            return self.render_to_response(context)

    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)
        context = self.get_context_data(**kwargs)
239
        context["form"] = UploadMateriForm
igor lestin sianipar's avatar
igor lestin sianipar committed
240
        return self.render_to_response(context)
241
242
243
244
245
246
    
    def validate_file_extension(self, value):
        ext = os.path.splitext(value.name)[1]  # [0] returns path+filename
        valid_extensions = ['.pdf', '.doc', '.docx', '.jpg', '.png', '.xlsx', '.xls', '.mp4', '.mp3']
        if not ext.lower() in valid_extensions:
            raise ValidationError('Unsupported file extension.')
247

Samuel Dimas's avatar
Samuel Dimas committed
248

249
250
251
class UploadMateriHTML(TemplateView):
    template_name = "unggah.html"
    context = {}
igor lestin sianipar's avatar
igor lestin sianipar committed
252

253
254
255
256
257
258
    def get_template_names(self):
        if self.request.path == "/unggah/":
            template_name = "unggah.html"
        return template_name


259
class DashboardKontributorView(TemplateView):
260
    template_name = "dashboard.html"
261
262
263
264
265
266
267

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated or not request.user.is_contributor:
            raise PermissionDenied(request)
        return super(DashboardKontributorView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
268
269
        context = super(DashboardKontributorView,
                        self).get_context_data(**kwargs)
270
271
272
273
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
274
275
276
        current_user = self.request.user
        materi_list = current_user.materi_set.all()
        context["materi_list"] = materi_list
277
        return self.render_to_response(context)
278

279

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
class ProfilAdminView(TemplateView):
    template_name = "profil-admin.html"

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated or not request.user.is_admin:
            raise PermissionDenied(request)
        return super(ProfilAdminView, self).dispatch(request, *args, **kwargs)

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

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        current_user = self.request.user
        context["user"] = current_user
        return self.render_to_response(context)
297

298

299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
class ProfilKontributorView(TemplateView):
    template_name = "profil.html"

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_authenticated or not request.user.is_contributor:
            raise PermissionDenied(request)
        return super(ProfilKontributorView, self).dispatch(request, *args, **kwargs)

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

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        current_user = self.request.user
        context["user"] = current_user
        return self.render_to_response(context)

317

318
319
320
321
class SuntingProfilView(TemplateView):
    template_name = "sunting.html"

    def dispatch(self, request, *args, **kwargs):
322
        if not request.user.is_authenticated or not request.user.is_contributor:
323
324
325
326
327
328
329
330
331
332
333
334
335
            raise PermissionDenied(request)
        return super(SuntingProfilView, self).dispatch(request, *args, **kwargs)

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

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)

        current_user = self.request.user
        context["user"] = current_user

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
336
        context["form"] = SuntingProfilForm(instance=current_user)
337
338
339
340
341
342
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)

Samuel Dimas Partogi's avatar
Samuel Dimas Partogi committed
343
        current_user = self.request.user
344

345
346
        form = SuntingProfilForm(
            request.POST, request.FILES, instance=current_user)
347
        if form.is_valid():
348
            current_user.default_profile_picture = True
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363

            # Removing exifdata from profile picture on upload
            if request.FILES:
                f_name = request.FILES['profile_picture'].name
                f_name = get_random_filename(f_name)
                f_path = settings.MEDIA_ROOT + "/" + f_name
                request.FILES['profile_picture'].name = f_name

                form = SuntingProfilForm(
            request.POST, request.FILES, instance=current_user)
                form.save()

                remove_image_exifdata(f_path)
            else: 
                form.save()
364
365
366
367
368
            return HttpResponseRedirect("/profil/")
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)
369

igor lestin sianipar's avatar
igor lestin sianipar committed
370

371
372
373
374
375
376
377
378
379
class SuntingProfilAdminView(TemplateView):
    template_name = "sunting_admin.html"

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_admin:
            raise PermissionDenied(request)
        return super(SuntingProfilAdminView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
igor lestin sianipar's avatar
igor lestin sianipar committed
380
381
        context = super(SuntingProfilAdminView,
                        self).get_context_data(**kwargs)
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)

        current_user = self.request.user
        context["user"] = current_user

        context["form"] = SuntingProfilForm(instance=current_user)
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)

        current_user = self.request.user
398

399
400
401
402
        form = SuntingProfilForm(
            request.POST, request.FILES, instance=current_user)
        if form.is_valid():
            current_user.default_profile_picture = True
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417

            # Removing exifdata from profile picture on upload
            if request.FILES:
                f_name = request.FILES['profile_picture'].name
                f_name = get_random_filename(f_name)
                f_path = settings.MEDIA_ROOT + "/" + f_name
                request.FILES['profile_picture'].name = f_name

                form = SuntingProfilForm(
            request.POST, request.FILES, instance=current_user)
                form.save()

                remove_image_exifdata(f_path)
            else: 
                form.save()
418
419
420
            return HttpResponseRedirect("/profil-admin/")
        else:
            context = self.get_context_data(**kwargs)
421
422
            context["form"] = form
            return self.render_to_response(context)
423

424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
class ReqMateriView(TemplateView):
    template_name = "req_materi.html"

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            return HttpResponseRedirect("/login/")
            #raise PermissionDenied(request)
        # else if not request.user.is_admin:
        #     raise PermissionDenied(request)
        return super(ReqMateriView, self).dispatch(request, *args, **kwargs)

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

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)

        current_user = self.request.user
        context["user"] = current_user

        context["form"] = SuntingProfilForm(instance=current_user)
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)

        current_user = self.request.user
        form = SuntingProfilForm(
            request.POST, request.FILES, instance=current_user)
        if form.is_valid():
            current_user.default_profile_picture = True
            form.save()
            return HttpResponseRedirect("/profil-admin/")
        else:
            context = self.get_context_data(**kwargs)
            context["form"] = form
            return self.render_to_response(context)

def post_req_materi(request):
    if request.method == 'POST':
        #return JsonResponse({"success": True, "msg": "Permintaan materi berhasil dikirimkan"})
        title = request.POST.get('title', None)
        if title is None:
            return JsonResponse({"success": False, "msg": "Missing parameter"})
        ReqMaterial(title=title).save()
        return JsonResponse({"success": True, "msg": "Permintaan materi berhasil dikirimkan"})
    else:
        return JsonResponse({"success": False, "msg": "Unsuported method"})

476

477
478
479
480
481
482
483
class SuksesLoginKontributorView(TemplateView):
    template_name = "sukses_kontri.html"

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_contributor:
            raise PermissionDenied(request)
        return super(SuksesLoginKontributorView, self).dispatch(request, *args, **kwargs)
484

485
    def get_context_data(self, **kwargs):
486
487
        context = super(SuksesLoginKontributorView,
                        self).get_context_data(**kwargs)
488
489
490
491
492
493
494
495
496
497
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)

        current_user = self.request.user
        context["user"] = current_user

        return self.render_to_response(context)

498

499
500
501
502
503
504
505
class SuksesLoginAdminView(TemplateView):
    template_name = "sukses_admin.html"

    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_admin:
            raise PermissionDenied(request)
        return super(SuksesLoginAdminView, self).dispatch(request, *args, **kwargs)
506

507
508
509
510
511
512
513
514
515
516
517
518
    def get_context_data(self, **kwargs):
        context = super(SuksesLoginAdminView, self).get_context_data(**kwargs)
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)

        current_user = self.request.user
        context["user"] = current_user

        return self.render_to_response(context)

igor lestin sianipar's avatar
igor lestin sianipar committed
519

520
521
522
class PostsView(TemplateView):

    template_name = "user_uploaded_posts.html"
523
524

    def dispatch(self, request, *args, **kwargs):
525
        if not request.user.is_authenticated:
526
            raise PermissionDenied(request)
527
        return super(PostsView, self).dispatch(request, *args, **kwargs)
528
529

    def get(self, request, *args, **kwargs):
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
        context = super().get_context_data(**kwargs)
        user = self.request.user

        posts = Materi.objects.filter(uploader=user).order_by("-date_created")
        posts_data = { post.id: { "data": post, "comments": [] } for post in posts }

        comments = Comment.objects \
            .filter(materi__id__in=posts_data.keys()) \
            .order_by("-timestamp")

        for comment in comments:
            posts_data[comment.materi.id]["comments"].append(comment)

        context["user"] = user
        context["posts"] = posts_data

546
        return self.render_to_response(context=context)
547

548

549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
class RevisiMateriView(TemplateView):
    template_name = "revisi.html"

    def dispatch(self, request, *args, **kwargs):        
        if not request.user.is_contributor:
            raise PermissionDenied(request)
        return super(RevisiMateriView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(RevisiMateriView, self).get_context_data(**kwargs)
        current_materi = get_object_or_404(Materi, pk=kwargs["pk"])
        context["materi"] = current_materi
        context["revisi_form"] = UploadMateriForm(instance=current_materi)
        return context

    def get(self, request, *args, **kwargs):
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)

    def post(self,request, *args, **kwargs):
        if request.user.is_authenticated == False:
            raise PermissionDenied(request)

        current_materi = get_object_or_404(Materi, pk=kwargs["pk"])
        form = UploadMateriForm(
            request.POST, request.FILES, instance=current_materi)
        if form.is_valid():
            materi = form.save(commit=False)
            materi.uploader = request.user
            materi.status = "REVISION"
            materi.save()
            kateg = form.cleaned_data['categories']
            for i in kateg:
                materi.categories.add(i)
            materi.save()
            return HttpResponseRedirect("/dashboard/")
        else:
            context = self.get_context_data(**kwargs)
            context["form_revisi"] = form
            return self.render_to_response(context)

Samuel Dimas's avatar
Samuel Dimas committed
590
591
592
593
594
595
def pages(request):
    context = {}
    # All resource paths end in .html.
    # Pick out the html file name from the url. And load that template.
    try:

596
        load_template = request.path.split("/")[-1]
Samuel Dimas's avatar
Samuel Dimas committed
597
598
599
        template = loader.get_template(load_template)
        return HttpResponse(template.render(context, request))

600
    except Exception as e:
Samuel Dimas's avatar
Samuel Dimas committed
601

602
        template = loader.get_template("error-404.html")
Samuel Dimas's avatar
Samuel Dimas committed
603
        return HttpResponse(template.render(context, request))