Функция комментариев Django создает ошибку HTTP 405

Я попытался создать функцию комментирования для своего приложения блога Django, и комментарии как таковые работают нормально, но я могу добавить их только через свою страницу администратора. Однако я хотел бы, чтобы пользователи могли добавлять комментарии к сообщениям в блоге.

Модель комментария в models.py:

class Comment(models.Model):
    post = models.ForeignKey('feed.Post', on_delete=models.CASCADE, related_name='comments')
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField(max_length=500)
    date_posted = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.content

Я попытался добавить функцию комментирования с представлением на основе класса в моем views.py:

class CommentCreateView(CreateView):
    model = Comment
    fields = ['content']

    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

В моем urls.py я добавил путь комментария таким образом, что он использует тот же путь, что и сообщение в блоге, поэтому пользователи могут добавлять комментарии на той же странице, и после публикации комментария они все еще находятся на странице сообщения.

urlpatterns = [
    path('', login_required(PostListView.as_view()), name='feed-home'),
    path('user/<str:username>/', login_required(UserPostListView.as_view()), name='user-feed'),
    # Blog Post View
    path('post/<int:pk>/', login_required(PostDetailView.as_view()), name='post-detail'),
    # (NOT WORKING) Comment View
    path('post/<int:pk>/', login_required(CommentCreateView.as_view()), name='post-detail'),
    path('post/new/', login_required(PostCreateView.as_view()), name='post-create'),
    path('post/<int:pk>/update', login_required(PostUpdateView.as_view()), name='post-update'),
    path('post/<int:pk>/delete', login_required(PostDeleteView.as_view()), name='post-delete'),
    path('about/', views.about, name='feed-about'),
]

Для других моих форм, таких как вход в систему, регистрация и т. д., я использовал четкие формы, и я подумал, что могу сделать то же самое здесь, поэтому я добавил небольшую форму под сообщением в блоге в моем post_detail.html

{% extends "feed/base.html" %}
{% load crispy_forms_tags %}

{% block content %}
    <!-- Post (works well!) -->
    <article class="media content-section">
        <img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}">
        <div class="media-body">
        <div class="article-metadata">
            <a class="mr-2" href="{% url 'user-feed' object.author.username %}">{{ object.author }}</a>
            <small class="text-muted">{{ object.date_posted|date:"d. F Y" }}</small>
            {% if object.author == user %}
            <div>
                <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'post-update' object.id %}">Update</a>
                <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
            </div>
            {% endif %}
        </div>
        <h2 class="article-title">{{ object.title }}</h2>
        <img class="post-image mb-2" src="{{ post.image.url }}" alt="">
        <p class="article-content">{{ object.content }}</p>

        {% for comment in post.comments.all %}
            <div class="comment">
                <div class="date">{{ comment.date_posted }}</div>
                <strong>{{ comment.author }}</strong>
                <p>{{ comment.content|linebreaks }}</p>
            </div>
            {% empty %}
                <p>No comments here yet(remove later)</p>
        {% endfor %}

        <!-- Form for the Comments (not working yet) -->
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
            <button class="btn btn-outline-secondary" type="submit">Post</button>
            </div>
        </form>

        </div>
    </article>
{% endblock content %}

Кроме того, форма отображается неправильно, она показывает только кнопку, но обычно при добавлении {{ form|crispy }} текстовые поля добавлялись автоматически.

Теперь, когда я нажимаю кнопку отправки, я получаю ошибку HTTP 405, и я не знаю, как я могу заставить это работать.

Я провел некоторое исследование в Интернете, но не смог найти ничего, связанного с моей проблемой.


person 0xDr0id    schedule 03.06.2020    source источник


Ответы (1)


Этот URL создает проблему. зачем вам пк при создании формы.

path('post/<int:pk>/', login_required(CommentCreateView.as_view()), name='post-detail'),

и если вы хотите создать комментарий с подробностями вашего сообщения, вы можете написать логику внутри самого post_detail_view, как в приведенном ниже коде.

def tutorial_detail_view(request, lecture_id):
tutorial = get_object_or_404(Course, pk=lecture_id) # here we get the id and all its componenet associated with it
comments = Comment.objects.filter(course=tutorial, reply=None).order_by('-id') # here we are filtering the comment for the relevent post and getting the latest post at the top

# if the request is post than create a foprm object
if request.method == 'POST':
    comment_form = CommentForm(request.POST or None)

    # if the form is valid than get the comment content and than create a comment object with the course detail, user detail and the comments detail and save it

    if comment_form.is_valid():
        comment = request.POST.get('comment') 
        reply_id = request.POST.get('comment_id')
        qs = None

        # for reply we first get the comment id
        # if there is reply than we get the reply id 
        if reply_id:
            qs = Comment.objects.get(id=reply_id)# to knw the reply of that comment by getting the reply id to the comment id

        comment = Comment.objects.create(course=tutorial, user=request.user, comment=comment, reply=qs)
        comment.save()

        return redirect('teacher-profile')


else:
    comment_form = CommentForm()

context = {
    'tutorial': tutorial,
    'comments': comments,
    'comment_form': comment_form,
}

return render(request, 'apps/lecture.html', context)

Таким образом, вы можете использовать один URL-адрес для его отображения. Надеюсь, вы получите какое-то представление об этом.

person ngawang13    schedule 03.06.2020