From 9e816d2281cd1ca93471df93be7f74c0f8e58655 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 30 Oct 2022 10:12:49 +0100 Subject: [PATCH] Setup very basic character view --- src/character/models/character.py | 4 ++++ src/character/templates/character/view.html | 8 ++++++++ src/character/urls.py | 6 ++++++ src/character/views.py | 14 +++++++++++++- src/charasheet/urls.py | 1 + src/common/templates/common/base.html | 6 ++++-- 6 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/character/templates/character/view.html create mode 100644 src/character/urls.py diff --git a/src/character/models/character.py b/src/character/models/character.py index 7c66855..3db3de2 100644 --- a/src/character/models/character.py +++ b/src/character/models/character.py @@ -1,5 +1,6 @@ from django.db import models from django.db.models.functions import Lower +from django.urls import reverse from django_extensions.db.models import TimeStampedModel from character.models.dice import Dice @@ -113,6 +114,9 @@ class Character(models.Model): def natural_key(self): return (self.name, self.player_id) + def get_absolute_url(self): + return reverse("character:view", kwargs={"pk": self.pk}) + @property def modifier_strength(self) -> int: return modifier(self.value_strength) diff --git a/src/character/templates/character/view.html b/src/character/templates/character/view.html new file mode 100644 index 0000000..2ac1a50 --- /dev/null +++ b/src/character/templates/character/view.html @@ -0,0 +1,8 @@ +{% extends "common/base.html" %} + +{% block title %}{{ character.name }}{% endblock %} + +{% block content %} +

{{ character.name }}

+

Edit

+{% endblock %} diff --git a/src/character/urls.py b/src/character/urls.py new file mode 100644 index 0000000..6e66e8a --- /dev/null +++ b/src/character/urls.py @@ -0,0 +1,6 @@ +from django.urls import path + +from character import views + +app_name = "character" +urlpatterns = [path("/", views.character_view, name="view")] diff --git a/src/character/views.py b/src/character/views.py index 60f00ef..4449c6b 100644 --- a/src/character/views.py +++ b/src/character/views.py @@ -1 +1,13 @@ -# Create your views here. +from django.contrib.auth.decorators import login_required +from django.core.handlers.wsgi import WSGIRequest +from django.http import HttpResponse +from django.shortcuts import get_object_or_404, render + +from character.models import Character + + +@login_required +def character_view(request: WSGIRequest, pk: int) -> HttpResponse: + character = get_object_or_404(Character, pk=pk) + context = {"character": character} + return render(request, "character/view.html", context) diff --git a/src/charasheet/urls.py b/src/charasheet/urls.py index 153339a..e0821ef 100644 --- a/src/charasheet/urls.py +++ b/src/charasheet/urls.py @@ -25,6 +25,7 @@ urlpatterns = [ path("logout/", logout, {"next_page": settings.LOGOUT_REDIRECT_URL}, name="logout"), path("admin/", admin.site.urls), path("", hello_world, name="hello_world"), + path("character/", include("character.urls", namespace="character")), ] urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/src/common/templates/common/base.html b/src/common/templates/common/base.html index c213801..383eadd 100644 --- a/src/common/templates/common/base.html +++ b/src/common/templates/common/base.html @@ -4,10 +4,12 @@ - Character Sheet + {% block title %}Character Sheet{% endblock %} - {% include "common/hello-random.html" %} + {% block content %} + {% include "common/hello-random.html" %} + {% endblock %} {% django_htmx_script %} {% if debug %}