1
Fork 0

Store page slug

This commit is contained in:
Jake Howard 2023-12-12 18:14:34 +00:00
parent 4c25b7cd91
commit 67dfd04bbb
Signed by: jake
GPG key ID: 57AFB45680EDD477
5 changed files with 12 additions and 3 deletions

View file

@ -4,5 +4,6 @@ class Page(models.Model):
__yamdl__ = True
title = models.CharField(max_length=255)
slug = models.CharField(max_length=128, unique=True, db_index=True)
content = models.TextField()

View file

@ -0,0 +1,6 @@
from yamdl.loader import ModelLoader
class CustomYamdlLoader(ModelLoader):
def load_fixture(self, model_class, data, file_path):
data["slug"] = file_path.stem
super().load_fixture(model_class, data, file_path)

View file

@ -11,7 +11,7 @@ def search(request):
pages = Page.objects.filter(id__in=row)
return HttpResponse(str(pages), content_type="text/plain")
def content(request, id):
page = get_object_or_404(Page, id=id)
def content(request, slug):
page = get_object_or_404(Page, slug=slug)
return HttpResponse(markdown.markdown(page.content), content_type="text/html")

View file

@ -80,6 +80,8 @@ YAMDL_DIRECTORIES = [
BASE_DIR / "content",
]
YAMDL_LOADER = "yamdl_playground.core.utils.CustomYamdlLoader"
# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

View file

@ -19,5 +19,5 @@ from .core import views
urlpatterns = [
path("search/", views.search),
path("content/<int:id>/", views.content)
path("content/<slug:slug>/", views.content)
]