Browse Source

Запоминаем никнейм в куки (врменно)

master
Дмитрий 3 years ago
parent
commit
5cf1f1b3d3
  1. 2
      blog/blog/settings.py
  2. 2
      blog/comments/models.py
  3. 4
      blog/comments/views.py
  4. 2
      blog/posts/templates/posts/post.html
  5. 10
      blog/posts/views.py

2
blog/blog/settings.py

@ -109,7 +109,7 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = "en-us" LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC" TIME_ZONE = "Europe/Moscow"
USE_I18N = True USE_I18N = True

2
blog/comments/models.py

@ -56,4 +56,4 @@ class Comment(models.Model):
return False return False
def __str__(self): def __str__(self):
return f"{self.author_name}: {self.comment_text[:100]}" return f"{self.nickname}: {self.comment_text[:100]}"

4
blog/comments/views.py

@ -51,8 +51,10 @@ def leave_comment(request, post_id):
comment_text=request.POST["text"], comment_text=request.POST["text"],
reply=parent, reply=parent,
) )
response = HttpResponseRedirect(reverse("posts:detail", args=(post.id,)))
response.set_cookie("nickname", nickname, max_age=300)
return HttpResponseRedirect(reverse("posts:detail", args=(post.id,))) return response
def get_or_create_author(name: str, secret: str) -> Union[CommentAuthor, None]: def get_or_create_author(name: str, secret: str) -> Union[CommentAuthor, None]:

2
blog/posts/templates/posts/post.html

@ -58,7 +58,7 @@
<input type="hidden" id="reply_to" name="reply_to"> <input type="hidden" id="reply_to" name="reply_to">
<div class="field"> <div class="field">
<label class="label">Имя</label> <label class="label">Имя</label>
<input class="input" type="text" placeholder="Имярек" name="name" required> <input class="input" type="text" placeholder="Имярек" name="name" value="{{ nickname }}" required>
</div> </div>
<div class="field"> <div class="field">
<label class="label">Секрет</label> <label class="label">Секрет</label>

10
blog/posts/views.py

@ -19,8 +19,16 @@ def detail(request, post_id):
post = get_object_or_404(Post, pk=post_id) post = get_object_or_404(Post, pk=post_id)
comments = post.comment_set.all() comments = post.comment_set.all()
comments_count = comments.count() comments_count = comments.count()
nickname = (
request.COOKIES.get("nickname") if request.COOKIES.get("nickname") else ""
)
return render( return render(
request, request,
"posts/post.html", "posts/post.html",
{"post": post, "comments": comments, "comments_count": comments_count}, {
"post": post,
"comments": comments,
"comments_count": comments_count,
"nickname": nickname,
},
) )

Loading…
Cancel
Save