0
votes

I've been following djangogirls' tutorial to create a blog with django with little to no trouble, but now I'm going on with their extensions tutorial which works on the web application created on the first one, and I'm coming to the error in the title when I try to get to the detail of a given post, which worked alright before.

Here's the error message in full detail

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/post/6/

Django Version: 1.10.6 Python Version: 3.5.2 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template error: In template /home/frank/Desarrollo/python/devdjango/djangogirlstut/blog/templates/blog/post_detail.html, error at line 14 Reverse for 'post_new' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] 4 :
5 : {% if post.published_date %} 6 : 7 : {{ post.published_date }} 8 :
9 : {% else %} 10 : Publish 11 :
{% endif %} 12 : {% if user.is_authenticated %} 13 : 14 : 15 : {% endif %} 16 :

{{ post.title }}

17 :

{{ post.text|linebreaksbr }}

18 : 19 : {% endblock %} 20 :

Traceback:

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner 42. response = get_response(request)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/blog/views.py" in post_detail 15. return render(request, 'blog/post_detail.html', {'post': post})

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/shortcuts.py" in render 30. content = loader.render_to_string(template_name, context, request, using=using)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/loader.py" in render_to_string 68. return template.render(context, request)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/backends/django.py" in render 66. return self.template.render(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" in render 208. return self._render(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" in _render 199. return self.nodelist.render(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" in render 994. bit = node.render_annotated(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" in render_annotated 961. return self.render(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/loader_tags.py" in render 174. return compiled_parent._render(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" in _render 199. return self.nodelist.render(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" in render 994. bit = node.render_annotated(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" in render_annotated 961. return self.render(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/defaulttags.py" in render 315. return nodelist.render(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" in render 994. bit = node.render_annotated(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/base.py" in render_annotated 961. return self.render(context)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/template/defaulttags.py" in render 439. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/urls/base.py" in reverse 91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))

File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/urls/resolvers.py" in _reverse_with_prefix 392. (lookup_view_s, args, kwargs, len(patterns), patterns)

Exception Type: NoReverseMatch at /post/6/ Exception Value: Reverse for 'post_new' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

The site urls.py file

from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views




urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/login/$', views.login, name='login'),
    url(r'^accounts/logout/$', views.logout, name='logout', kwargs={'next_page': '/'}),
    url(r'', include('blog.urls', namespace='blog')),
]

the blog/urls.py file (blog being the app name)

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.post_list, name='post_list'),
    url(r'^post/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
    url(r'^post/new/$', views.post_new, name='post_new'),
    url(r'^drafts/$', views.post_draft_list, name='post_draft_list'),
    url(r'^post/(?P<pk>\d+)/publish/$', views.post_publish, name='post_publish'),
    url(r'^post/(?P<pk>\d+)/remove/$', views.post_remove, name='post_remove'),


]

The offending template blog/templates/blog/post_detail.html

{% extends 'blog/base.html' %}

{% block content %}
    <div class="post">
        {% if post.published_date %}
            <div class="date">
            {{ post.published_date }}
        </div>
    {% else %}
        <a class="btn btn-default" href="{% url 'blog:post_publish' pk=post.pk %}">Publish</a>
        {% endif %}
    {% if user.is_authenticated %}
    <a class="btn btn-default" href="{% url 'blog:post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a>
    <a class="btn btn-default" href="{% url 'blog:post_remove' pk=post.pk %}"><span class="glyphicon glyphicon-remove"></span></a>
    {% endif %}
        <h1>{{ post.title }}</h1>
        <p>{{ post.text|linebreaksbr }}</p>
    </div>
{% endblock %}

and the base template it extends from

{% load staticfiles %}
<html>
    <head>
        <title>Django Girls 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">
    {% if user.is_authenticated %}
        <a href="{% url 'blog:post_new' %}" class="top-menu"><span class="glyphicon glyphicon-plus"></span></a>     
        <a href="{% url 'blog:post_draft_list' %}" class="top-menu"><span class="glyphicon glyphicon-edit"></span></a>
        <p class="top-menu">Hello {{ user.username }} <small>(<a href="{% url 'logout' %}">Log out</a>)</small></p>

    {% else %}
            <a href="{% url 'login' %}" class="top-menu"><span class="glyphicon glyphicon-lock"></span></a>
    {% endif %}
        <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>

I've tried namespacing, I've not changed the regexp in the urls files (i.e. they're the same that did work before), the django version is ok (1.10)... I've read this other answer about the NoReverseMatch but no cigar.

Any ideas on where I may have made it go wrong?


Testing int bash:

from django.urls import reverse
reverse('blog:post_new') Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File
"/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/urls/base.py",
line 33, in reverse
    resolver = get_resolver(urlconf)   File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/urls/resolvers.py",
line 67, in get_resolver
    urlconf = settings.ROOT_URLCONF   File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/conf/__init__.py",
line 53, in __getattr__
    self._setup(name)   File "/home/frank/Desarrollo/python/devdjango/djangogirlstut/lib/python3.5/site-packages/django/conf/__init__.py",
line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting
ROOT_URLCONF, but settings are not configured. You must either define
the environment variable DJANGO_SETTINGS_MODULE or call
settings.configure() before accessing settings.
1

1 Answers

1
votes

The blog app is under the namespace blog. Every reference to a URL to this app should be of the form namespace:url_name.

So, you should use a namespace inside the {% url %} template tag. You are doing it correctly inside the offending template and you should do the same inside the base template too.

So inside base.html (and anywhere else blog urls are involved):

<!-- base.html -->

{% url 'blog:post_new' %}
{% url 'blog:post_draft_list' %}

Leave {% url 'login' %} and {% url 'logout' %} as is. They are not namespaced.