Hmm... I didn't think about this option. But the process should be even simpler than the instructions I've written here.
Firstly, in our models.py we don't need an author based on User anymore, we will just use TextField():
models.py
TextField()
# models.py from django.db import models from django.utils import timezone class BlogComment(models.Model): blogpost_connected = models.ForeignKey( BlogPost, related_name='comments', on_delete=models.CASCADE) author = TextField() content = TextField() date_posted = models.DateTimeField(default=timezone.now) def __str__(self): return self.blogpost_connected.title[:40]
[Optional] We can still return the number of comments if we want in our main BlogPost class model in models.py:
BlogPost
# models.py class BlogPost(models.Model): title = models.CharField(max_length=100) ... @property def number_of_comments(self): return BlogComment.objects.filter(blogpost_connected=self).count()
Make the changes to our database:
# CLI/Terminal >> cd C:\Projects\...\YourDjangoAppMainFolder >> python manage.py makemigrations >> python manage.py migrate
Create the same forms.py with an author:
forms.py
# forms.py from django import forms from .models import BlogComment class NewCommentForm(forms.ModelForm): class Meta: model = BlogComment fields = ['author', 'content']
And now in views.py we don't need to check if self.request.user.is_authenticated anymore, we will just send the form as context to our HTML-based blogpost_detail.html.
views.py
self.request.user.is_authenticated
blogpost_detail.html
# views.py from .models import BlogPost, BlogComment from .forms import NewCommentForm class BlogPostDetailView(DetailView): model = BlogPost # template_name = MainApp/BlogPost_detail.html # context_object_name = 'object' def get_context_data(self, **kwargs): data = super().get_context_data(**kwargs) comments_connected = BlogComment.objects.filter( blogpost_connected=self.get_object()).order_by('date_posted') data['comments'] = comments_connected data['comment_form'] = NewCommentForm( ) return data def post(self, request, *args, **kwargs): new_comment = BlogComment(content=request.POST.get('content'), author=request.POST.get('author'), blogpost_connected=self.get_object()) new_comment.save() return self.get(self, request, *args, **kwargs)
And finally in our blogpost_detail.html we'll show directly the forms without checking if user.is_authenticated.
user.is_authenticated
<!-- COMMENTS --> <h2>Leave your comment!</h2> <div id="comments_section"> <form method="POST"> {% csrf_token %} <div class="form-group"> {{ comment_form }} <button class="btn btn-info" type="submit">Add comment <i class="fas fa-comments"></i></button> </div> </form> {% if comments %} <strong class="text-secondary">{{ object.number_of_comments }} Comment{{ object.number_of_comments|pluralize }}</strong> <hr> <ul> {% for comment in comments %} <li> <div> <span> <strong class="text-info">{{ comment.author }} </strong> <small class="text-muted">{{ comment.date_posted }}</small> </span> <p> {{ comment.content|safe }} </p> </div> </li> {% endfor %} </ul> {% else %} <strong class="text-secondary">No comments yet...</strong> {% endif %} </div>
Hope all of these will work. Good Luck!
Thanks for publishing Python information. This is very understanding and useful to me
Amazing!! Thank you a lot!! It's more or less how i thought it should be, now i can continue with the project :-D
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hmm... I didn't think about this option. But the process should be even simpler than the instructions I've written here.
Firstly, in our
models.py
we don't need an author based on User anymore, we will just useTextField()
:[Optional] We can still return the number of comments if we want in our main
BlogPost
class model inmodels.py
:Make the changes to our database:
Create the same
forms.py
with an author:And now in
views.py
we don't need to check ifself.request.user.is_authenticated
anymore, we will just send the form as context to our HTML-basedblogpost_detail.html
.And finally in our
blogpost_detail.html
we'll show directly the forms without checking ifuser.is_authenticated
.Hope all of these will work. Good Luck!
Thanks for publishing Python information. This is very understanding and useful to me
Amazing!! Thank you a lot!! It's more or less how i thought it should be, now i can continue with the project :-D