From f267d9d2e789da0f4d86bf326bd104b432ba7ac1 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Mon, 4 Jul 2022 22:42:20 +0100 Subject: [PATCH] Moar work --- flangr/posts/forms.py | 3 +++ flangr/posts/models.py | 4 ++-- flangr/posts/urls.py | 1 + flangr/posts/views.py | 10 +++++++++- flangr/templates/posts/post_detail.html | 6 ++++++ 5 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 flangr/posts/forms.py diff --git a/flangr/posts/forms.py b/flangr/posts/forms.py new file mode 100644 index 0000000..726a1c4 --- /dev/null +++ b/flangr/posts/forms.py @@ -0,0 +1,3 @@ +from django.forms import ModelForm + +from .models import Comment diff --git a/flangr/posts/models.py b/flangr/posts/models.py index 5799270..fa22ba2 100644 --- a/flangr/posts/models.py +++ b/flangr/posts/models.py @@ -8,8 +8,8 @@ from django.conf import settings def get_img_location(instance, filename): - uid = uuid.uuid4() - return f"{uid[0:2]}/{uid[2:]}".replace("-", "") + uid = uuid.uuid4().hex + return f"{uid[0:2]}/{uid[2:]}" class Post(models.Model): diff --git a/flangr/posts/urls.py b/flangr/posts/urls.py index 604330b..6bd7575 100644 --- a/flangr/posts/urls.py +++ b/flangr/posts/urls.py @@ -4,4 +4,5 @@ from . import views urlpatterns = [ path("post/", views.PostDetailView.as_view()), + path("post//comments", views.AddCommentView.as_view()), ] diff --git a/flangr/posts/views.py b/flangr/posts/views.py index d90837f..5fa1d08 100644 --- a/flangr/posts/views.py +++ b/flangr/posts/views.py @@ -1,9 +1,17 @@ from django.views.generic import DetailView +from django.forms import modelform_factory # Create your views here. -from .models import Post +from .models import Post, Comment class PostDetailView(DetailView): model = Post + + def get_context_data(self, **kwargs): + context = { + "add_comment_form": modelform_factory(Comment, fields=("comment",)), + **kwargs, + } + return super().get_context_data(**context) diff --git a/flangr/templates/posts/post_detail.html b/flangr/templates/posts/post_detail.html index 0ad7cae..d44eb00 100644 --- a/flangr/templates/posts/post_detail.html +++ b/flangr/templates/posts/post_detail.html @@ -11,4 +11,10 @@
  • {{ comment.user }} - {{ comment.comment }}
  • {% endfor %} +
    + {% csrf_token %} + {{ add_comment_form }} + +