Skip to content
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

replace reponse by a json response in pages app #52

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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
22 changes: 19 additions & 3 deletions django/src/pages/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
from django.views.decorators.http import require_GET
from django.shortcuts import render
from django.http import JsonResponse, HttpRequest
from django.template.loader import get_template

def create_response(
request: HttpRequest,
template_name: str,
context = None,
need_authentication: bool = False,
title: str|None = None
):
if need_authentication and not request.user.is_authenticated:
return JsonResponse({'error': 'Need authentication'}, status=403)
content = {}
content['html'] = get_template(template_name).render(context, request)
if title:
content['title'] = title
return JsonResponse(content, status=200)

@require_GET
def index(request):
return render(request, 'index.html')
return create_response(request, 'index.html')

@require_GET
def games(request):
return render(request, 'games.html')
return create_response(request, 'games.html')
Loading