'NoReverseMatch at / 'learning_logs' is not a registered namespace
I think i tired everything in trying to solve this problem. I'm getting a NoReverseMatch i understand its from my "urls" but that's basically it, I was using url() instead of path().at the end of it all i just made a mess of my code. view.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from .models import Topic as TopicModel
from .forms import TopicForms
# Create your views here.
def index(request):
""""the home page for learning app"""
return render(request,'learning_logs/index.html')
def topics(request):
topics = TopicModel.objects.order_by('date_added')
context = {'topics' : topics}
return render(request, 'learning_logs/topics.html',context)
def topic(request, topic_id):
"""show a single topic ans all its entries"""
topic = TopicModel.objects.get(id=topic_id)
entries = topic.entry_set.order_by('date_added')
context = {'topic':topic, 'entries': entries}
return render(request,'learning_logs/topic.html',context)
def new_topic(request):
"""add a new topic"""
if request.method != 'POST':
#no data submitted ; create a black form
form = TopicForms
else:
#POST data submited ; process data
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('learning_log:topics'))
context = {'form': form }
return render(request,'learning_log/new_topic.html', context)
urls.py
#defins URL patterns for learning log
from django.conf.urls import url
from . import views
from django.urls import path
app_name = 'learning_log'
urlpatterns = [
#home page
path('', views.index, name ='index'),
#url(r'^$', views.index, name='index.html'),
#show topics
path('topics/',views.topics, name ='topics'),
# url(r'^topics/$',views.topics, name='topics.html'),
path('topics/<topic_id>',views.topic, name = 'topic'),
#url(r'^topics/(?P<topic_id>\d+)\$',views.topic,name='topic.html'),
path('topics/',views.new_topic, name = 'new_topic'),
#url(r'^topics/$',views.new_topic, name='new_topic.html'),
]
main urls.py
from django.contrib import admin
from django.conf.urls import include, url
from django.urls import path, include
urlpatterns = [
path('', include('learning_logs.urls')),
path('admin/',admin.site.urls),
]
please anything I'm doing wrong plus any books or tutorials i can read because i think the book im using is outdated
Solution 1:[1]
Your app_name
inside the urls file is "learning_log" but it seems your html folders are in learning_logs. Maybe something to do with that? Also can you make sure the app_name exists in your INSTALLED_APPS
?
I've done a Udemy course for Django which was good for me but they are not free if you want to learn more.
Solution 2:[2]
just add the include param called namespace
in your main urls.py for the app.
from django.contrib import admin
from django.conf.urls import include, url
from django.urls import path, include
urlpatterns = [
path('', include('learning_logs.urls', namespace="learning_log")),
path('admin/',admin.site.urls),
]
here you have the documentation of URLs namespaces: https://docs.djangoproject.com/en/3.2/topics/http/urls/#url-namespaces
If you don't want to set the namespace you must call the url like this: reverse('topics')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dean Elliott |
Solution 2 | Diego Puente |