Click here to Skip to main content
15,885,278 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Who is afraid of regex? Pin
honey the codewitch3-Jan-21 7:50
mvahoney the codewitch3-Jan-21 7:50 
GeneralRe: Who is afraid of regex? Pin
#realJSOP3-Jan-21 7:56
mve#realJSOP3-Jan-21 7:56 
GeneralRe: Who is afraid of regex? Pin
honey the codewitch3-Jan-21 8:24
mvahoney the codewitch3-Jan-21 8:24 
GeneralRe: Who is afraid of regex? Pin
Sander Rossel3-Jan-21 23:02
professionalSander Rossel3-Jan-21 23:02 
GeneralRe: Who is afraid of regex? Pin
honey the codewitch4-Jan-21 2:52
mvahoney the codewitch4-Jan-21 2:52 
GeneralRe: Who is afraid of regex? Pin
Thomas Stockwell4-Jan-21 5:08
professionalThomas Stockwell4-Jan-21 5:08 
GeneralRe: Who is afraid of regex? Pin
Matt McGuire5-Jan-21 5:21
professionalMatt McGuire5-Jan-21 5:21 
GeneralNo Post matches the given query in django Error Pin
k3 v1n2-Jan-21 17:32
k3 v1n2-Jan-21 17:32 
So i have a project called star social project this project is similar to a socail media that you can post and create group but this project you can only post when you are in a group. So i get an error message that is not familiar to me which is on the title, i tried to search on google and get some result but when i implement it to my project it does not work. So why im getting this error is because i'm trying to create a comment section and when i click the add comment that's when i get the error message. So i'm here to ask someone to help me because i'm not really familiar on this error and i'm just learning django for about 2 months now.


models.py

##########################
## POSTS MODELS.PY FILE ##
##########################

from django.contrib.auth import get_user_model
from django.db import models
from groups.models import Group
from misaka import html
from django.urls import reverse
from django.utils import timezone

User = get_user_model()

class Post(models.Model):

    user = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now=True)
    message = models.TextField()
    message_html = models.TextField(editable=False)
    group = models.ForeignKey(Group, related_name='posts', null=True, blank=True, on_delete=models.CASCADE)

    def __str__(self):
        return self.message

    def save(self, *args, **kwargs):

        self.message_html = html(self.message)
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse(
            'posts:single',
            kwargs={
                'username': self.user.username,
                'pk': self.pk
            }
        )

    class Meta:

        ordering = ['-created_at']

class Comment(models.Model):

    post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
    username = models.CharField(max_length=50)
    text = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    approved = models.BooleanField(default=False)

    def __str__(self):
        return self.text

    def approved(self):

        self.approved = True
        self.save()


views.py

class AddComment(LoginRequiredMixin, generic.CreateView):

    model = Comment
    fields = ('username', 'text')

    def form_valid(self, form):

        post = get_object_or_404(Post, pk=self.kwargs.get('pk'))
        self.object = form.save(commit=False)
        self.object.post = post
        self.object.save()
        return super().form_valid(form)


urls.py

########################
## POSTS URLS.PY FILE ##
########################

from django.urls import path
from posts import views

app_name = 'posts'

urlpatterns = [
    path('', views.PostList.as_view(), name='all'),
    path('by/<username>/', views.UserPost.as_view(), name='for_user'),
    path('by/<username>/<int:pk>/', views.PostDetail.as_view(), name='single'),
    path('new/', views.CreatePost.as_view(), name='create'),
    path('delete/<int:pk>/', views.DeletePost.as_view(), name='delete'),
    path('post/comment/', views.AddComment.as_view(), name='add_comment'),
]


comment_form.html

{% extends 'posts/post_base.html' %}
{% load bootstrap4 %}

    {% block prepost %}
        <h4>Add New Comment</h4>

        <form id="commentForm" action="{% url 'posts:add_comment' %}" method="POST">
            {% csrf_token %}
            {% bootstrap_form form %}
            {% buttons %}
                <button type="submit" class="btn btn-large btn-primary">Add Comment</button>
            {% endbuttons %}
        </form>
    {% endblock %}


_post.html

<div class="media">
    <h3 class="mr-5"><a href="{% url 'posts:for_user' username=post.user.username %}">@{{ post.user.username }}</a></h3>

    <div class="media-body">
        {{ post.user.username }}
        <h5>{{ post.message_html|safe }}
            <time class="time"><a href="{% url 'posts:single' username=post.user.username pk=post.pk %}">{{ post.created_at }}</a></time>

            {% if post.group %}
                in <a href="#">{{ post.group.name }}</a>
            {% endif %}
        </h5>

        <hr>
        {% if user.is_authenticated %}
            <a class="btn btn-primary btn-comment" href="{% url 'posts:add_comment' %}">Add Comment</a>
        {% endif %}

        <div class="container">
            {% for comment in comment_list %}
                <br>
                    {{ comment.created }}
                    <p>{{ comment.text|safe|linebreaks }}</p>
                    <p>Posted By: {{ comment.username }}</p>                   
            {% endfor %}
        </div>

        <div class="media-footer">
            {% if user.is_authenticated and post.user == user and not hide_delete %}
                <a class="btn btn-simple" href="{% url 'posts:delete' pk=post.pk %}" title="delete">Delete</a>
            {% endif %}
        </div>
    </div>
</div>

GeneralRe: No Post matches the given query in django Error Pin
Rick York2-Jan-21 17:45
mveRick York2-Jan-21 17:45 
GeneralRe: No Post matches the given query in django Error Pin
OriginalGriff2-Jan-21 21:31
mveOriginalGriff2-Jan-21 21:31 
GeneralThe New ABCD Pin
Amarnath S2-Jan-21 15:15
professionalAmarnath S2-Jan-21 15:15 
GeneralRe: The New ABCD Pin
OriginalGriff2-Jan-21 21:30
mveOriginalGriff2-Jan-21 21:30 
GeneralRe: The New ABCD Pin
Marc Clifton3-Jan-21 3:43
mvaMarc Clifton3-Jan-21 3:43 
GeneralRe: The New ABCD Pin
DaveAuld3-Jan-21 6:28
professionalDaveAuld3-Jan-21 6:28 
GeneralRe: The New ABCD Pin
enhzflep3-Jan-21 16:13
enhzflep3-Jan-21 16:13 
GeneralMy New Year Resolution Pin
jsc422-Jan-21 10:05
professionaljsc422-Jan-21 10:05 
GeneralRe: My New Year Resolution Pin
Mladen Janković2-Jan-21 10:36
Mladen Janković2-Jan-21 10:36 
GeneralRe: My New Year Resolution Pin
Dave Kreskowiak2-Jan-21 11:50
mveDave Kreskowiak2-Jan-21 11:50 
GeneralRe: My New Year Resolution Pin
honey the codewitch2-Jan-21 12:46
mvahoney the codewitch2-Jan-21 12:46 
GeneralRe: My New Year Resolution Pin
Rick York2-Jan-21 17:52
mveRick York2-Jan-21 17:52 
GeneralRe: My New Year Resolution Pin
Dave Kreskowiak2-Jan-21 18:33
mveDave Kreskowiak2-Jan-21 18:33 
GeneralRe: My New Year Resolution Pin
dandy723-Jan-21 9:04
dandy723-Jan-21 9:04 
GeneralHappy and Healthy new year. Pin
Slow Eddie2-Jan-21 3:51
professionalSlow Eddie2-Jan-21 3:51 
GeneralRe: Happy and Healthy new year. Pin
theoldfool2-Jan-21 4:53
professionaltheoldfool2-Jan-21 4:53 
GeneralRe: Happy and Healthy new year. Pin
Slacker0072-Jan-21 5:06
professionalSlacker0072-Jan-21 5:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.