Bhawana Badlani
Bhawana Badlani

Reputation: 51

NoReverse MAtch at /

I have developed my blog following https://tutorial.djangogirls.org/en. The website runs on localhost, but it shows NoReverse Match at / error in pythonanywhere.com

This error is shown when the view is not specified. however I have given code snippets to show the views.

1) my views.py

from django.utils import timezone
from .models import Post

# Create your views here.
def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})

def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})

2) post_detail.html


{% block content %}
    <div class="post">
        {% if post.published_date %}
            <div class="date">
                {{ post.published_date }}
            </div>
        {% endif %}
        <h2>{{ post.title }}</h2>
        <p>{{ post.text|linebreaksbr }}</p>
    </div>
{% endblock %}

3) post_list.html


{% block content %}
    {% for post in posts %}
        <div class="post">
            <div class="date">
                <p>published: {{ post.published_date }}</p>
            </div>
            <h2><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></h2>
            <p>{{ post.text|linebreaksbr }}</p>
        </div>
    {% endfor %}
{% endblock %}

4) and base.html

<html>
    <head>       
        <title>Krishna's blog</title>
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
        <link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
        <link rel="stylesheet" href="{% static 'css/blog.css' %}">
    </head>
    <body>
        <div class="page-header">
            <h1><a href="/">Django Girls Blog</a></h1>
        </div>
        <div class="content container">
            <div class="row">
                <div class="col-md-8">
                {% block content %}
                {% endblock %}
                </div>
            </div>
        </div>
    </body>    
</html>

1) the error shown is:

Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name.
Request Method: GET
Request URL:    http://bhawanabadlani.pythonanywhere.com/
Django Version: 2.0.13
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name.
Exception Location: /home/bhawanabadlani/.virtualenvs/bhawanabadlani.pythonanywhere.com/lib/python3.5/site-packages/django/urls/resolvers.py in _reverse_with_prefix, line 634
Python Executable:  /usr/local/bin/uwsgi
Python Version: 3.5.6
Python Path:    
['/home/bhawanabadlani/bhawanabadlani.pythonanywhere.com',
 '/var/www',
 '.',
 '',
 '/var/www',
 '/home/bhawanabadlani/.virtualenvs/bhawanabadlani.pythonanywhere.com/lib/python35.zip',
 '/home/bhawanabadlani/.virtualenvs/bhawanabadlani.pythonanywhere.com/lib/python3.5',
 '/home/bhawanabadlani/.virtualenvs/bhawanabadlani.pythonanywhere.com/lib/python3.5/plat-linux',
 '/home/bhawanabadlani/.virtualenvs/bhawanabadlani.pythonanywhere.com/lib/python3.5/lib-dynload',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-linux',
 '/home/bhawanabadlani/.virtualenvs/bhawanabadlani.pythonanywhere.com/lib/python3.5/site-packages']
Server time:    Tue, 24 Sep 2019 13:37:10 +0530
Error during template rendering
In template /home/bhawanabadlani/bhawanabadlani.pythonanywhere.com/blog/template/blog/base.html, error at line 6

Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name.
1   {% load static %}
2   <html>
3       <head>       
4           <title>Krishna's blog</title>
5           <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
6           <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
7           <link href="//fonts.googleapis.com/css?family=Lobster&subset=latin,latin-ext" rel="stylesheet" type="text/css">
8           <link rel="stylesheet" href="{% static 'css/blog.css' %}">
9       </head>
10      <body>
11          <div class="page-header">
12              <h1><a href="/">Django Girls Blog</a></h1>
13          </div>
14          <div class="content container">
15              <div class="row">
16                  <div class="col-md-8">

Upvotes: 0

Views: 125

Answers (2)

Bhawana Badlani
Bhawana Badlani

Reputation: 51

Thank you for the response. I realised that anytime I pull changes from GIT in pythonanywhere account I have to reload the Web Page not by refreshing it, but pressing the Reload button in the web app page.

It is funny that it shows errors, noticing that some changes have been made, but it does not incorporate the changes.

Changes are incorporated when the green webapp reload button is hit.

Upvotes: 0

conrad
conrad

Reputation: 1913

If it worked locally but not on PythonAnywhere, then perhaps you had made changes to one or more of the urls.py files in your code base, that were not committed to your git repo and not transferred over to PythonAnywhere.

You would have to make sure that Django is aware of a url with the name post_detail for your template to be able to find the reverse of post_detail.

Upvotes: 2

Related Questions