Django django.urls.exceptions.NoReverseMatch: Reverse for ' new_topic' not found. ' new_topic' is not a valid view function or pattern name

Viewed 760

I'm trying to build a topics section, in where you can create a new topic. This exercise is from the python crash course book.

The error I see is the next one:

django.urls.exceptions.NoReverseMatch: Reverse for ' new_topic' not found. ' new_topic' is not a valid view function or pattern name.

views.py

from django.shortcuts import render
from .models import Topic, Entry
from .forms import TopicForm, EntryForm
from django.http import HttpResponseRedirect
from django.urls import reverse

# Create your views here.
def index(request):
    return render(request, 'learning_logs/index.html')

def topics(request):
    topics = Topic.objects.order_by('date_added')
    context = {'topics': topics}
    return render (request, 'learning_logs/topics.html', context)


def topic(request, topic_id):
    topic = Topic.objects.get(id=topic_id)
    topics = Topic.objects.order_by('date_added')
    entries = topic.entry_set.order_by('date_added')
    context = {'topic': topic, 'entries': entries}
    return render(request, 'learning_logs/topics.html', context)

def new_topic(request):
    if request.method != 'POST':
        form = TopicForm()
    else:
        form = TopicForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('learning_logs:topics'))
    context = {'form': form}
    return render(request, 'learning_logs/new_topic.html', context)



def new_entry(request, topic_id):
    topic = Topic.objects.get(id=topic_id)
    if request.method != 'POST':
        form = EntryForm()
    else:
        form =  EntryForm(data=request.POST)
        if form.is_valid():
            new_entry = form.save(commit=False)
            new_entry.topic = topic
            new_entry.save()
            return HttpResponseRedirect(reverse('learning_logs:topic', args=[topic_id]))
    context = {'topic': topic, 'form': form}
    return render(request, 'learning_logs/new_entry.html', context)

proj/urls.py

from django.urls import path
from . import views

app_name = 'learning_logs'
urlpatterns = [path('', views.index, name='index'),
            path('topics/', views.topics, name='topics'),
            path('topics/<int:topic_id>/', views.topic, name='topic'),
            path('new_topic/', views.new_topic, name= 'new_topic'),
            path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),

            ]

new_topic.html

{% extends "learning_logs/base.html" %}

{% block content %}

<p> Add a new topic: </p>

< form action ="{% url 'learning_logs: new_topic' %}"
method = 'post' class= "form">

{% csrf_token %}
{{form.as_p}}

<button name = "submit"> add topic</button>
</form>

{% endblock content %}

base.html

<p>
  <a href="{% url 'learning_logs:index'%}"> Learning Log</a>
  <a href="{% url 'learning_logs:topics'%}"> Topics </a>
</p>

{% block content %} {% endblock content %}

website/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('learning_logs.urls')),
    

]

Any help would appreciate it. I've been trying to fix it for quite a while now and I don't seem to find the reason why it is an error.

1 Answers

I believe the issue is in this line:

< form action ="{% url 'learning_logs: new_topic' %}"

There's a space before new_topic, and it's looking for a url that includes that space. I believe if you remove that space your project will work.

Related