Home django==2.0.4 with MySQL, NoReverseMatch at /1/
Reply: 0

django==2.0.4 with MySQL, NoReverseMatch at /1/

user4660
1#
user4660 Published in August 15, 2018, 11:16 am

I got this message whenever I try to open my blog article. my blog url is http://leonkong.com/ and post url is http://leonkong.com/1/

Error message:

NoReverseMatch at /1/
Reverse for 'like_action' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)\\/like_action\\/$']

I use Django version 2.0.4 and recently created MySQL using AWS RDS.

FYI, like_action function code(views.py):

from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt

def like_action(request, pk):
    """Like action: add +1 to post.like_button"""

    post = get_object_or_404(Post, pk=pk)
    post.like_button = post.like_button + 1
    post.save()

    return redirect('post_detail', pk=post.pk)

urls.py:

from django.urls import path
from . import views


urlpatterns = [
        path('keyboard', views.buttons, name='buttons'),
        path('message', views.message, name='message'),
        path('', views.post_list, name='post_list'),
        path('<int:pk>/', views.post_detail, name='post_detail'),
        path('post/new/', views.post_new, name='post_new'),
        path('post/<int:pk>/edit/', views.post_edit, name='post_edit'),
        path('about', views.about, name='about'),
        path('post/<int:pk>/remove/', views.post_remove, name='post_remove'),
        path('<str:category>', views.category_list, name='category_list'),
        path('<int:pk>/like_action/', views.like_action, name='like_action'),
]

Here is html code for 'post_detail'. I always got error whenever I try to access 'post_detail' page.

{% extends 'blog/base.html' %}
{% load markdownify %}
{% load static %}

{% block content %}
    <div class="post">
        {% if post.created_date %}
            <div class="date">
                {{ post.created_date }}
            </div>
        {% endif %}

        <h1 class="detail">{{ post.title }}</h1>
        <div class="edit">
            {% if user.is_authenticated %}
            <a href="{% url 'post_edit' pk=post.pk %}">edit</a>
            <a class="delete" href="{% url 'post_remove' pk=post.pk %}">delete</a>
            {% endif %}
        </div>
        <div class="content">
            {{ post.text|markdown|safe }}
        </div>
        <div class="interaction">
            <p style="color: #4e57ac; display: inline; font-weight: bold; font-family: sans-serif;">
                    {{ post.like_button }}
            </p>
            {% if post.like_button < 2 %}
                <a href="{% url 'like_action' pk=post.pk %}" style="font-weight: bold; font-family: sans-serif;">Like</a>
            {% else %}
                <a href="{% url 'like_action' pk=post.pk %}" style="font-weight: bold; font-family: sans-serif;">Likes</a>
            {% endif %}
            <div class="fb-share-button" data-href="http://leonkong.com{% url 'post_detail' pk=post.pk %}" data-layout="button" data-size="large" data-mobile-iframe="true"><a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fleonkong.com%2F&amp;src=sdkpreparse" class="fb-xfbml-parse-ignore">Share</a></div>
        </div>
    </div>
{% endblock %}

Here is MySQL outcome:

> DESC blog_post;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | int(11)      | NO   | PRI | 0       |       |
| title        | varchar(200) | NO   |     | NULL    |       |
| text         | longtext     | NO   |     | NULL    |       |
| created_date | datetime(6)  | YES  |     | NULL    |       |
| author_id    | int(11)      | NO   |     | NULL    |       |
| category     | varchar(5)   | NO   |     | NULL    |       |
| like_button  | int(11)      | NO   |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+

views.post_detail

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'posts': post})
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.295152 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO