Aisling Ni Mhurchu
Aisling Ni Mhurchu

Reputation: 1

Django - cannot get the html page to display - NoReverseMatch Error

I have a django app already created and working. I am trying to add another html page. I have added the about page into the home app enter image description here

This is home/views.py

from django.shortcuts import render, redirect, reverse
from hobby_product.models import hobby_product



def home(request):
    """ Return home page """
    #return redirect(reverse('home'))
    return render(request, 'home.html')


def not_found(request):
    """ Return 404 page not found """

    return render(request, '404.html')


def server_error(request):
    """ Return 500 internal server error """

    return render(request, '500.html')

def about(request):
    return render(
        request, "about.html"
    )

Here is the url.py in home:

from django.conf.urls import url, include                                                                                                                                                                                                                                                  
from .views import not_found, server_error, home, about

urlpatterns = [
    url('/', home, name='home'),
    url('not_found/', not_found, name='not_found'),
    url('server_error/', server_error, name='server_error'),
    url(r'^about$', about, name='about'),
    
]

This is the url.py for the base app:

from django.conf.urls import url, include
from django.contrib import admin
from accounts.views import index, logout, login, registration, user_profile
from django.views.generic import RedirectView
from django.views.static import serve
from .settings import MEDIA_ROOT
from accounts import urls as accounts_urls
from about.views import about
from accounts.views import index
from accounts.views import home
from hobby_product import urls as urls_hobby_product
from cart import urls as urls_cart

from home import urls as urls_home
from about import urls as urls_about
from search import urls as urls_search
from checkout import urls as urls_checkout
from django.views import static
from .settings import MEDIA_ROOT


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', RedirectView.as_view(url='home/')),
    url(r'^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT}),
    url(r'^$', index, name="index"),
    url(r'^accounts/', include(accounts_urls)),
    url(r'^hobby_product/', include(urls_hobby_product)),
    url(r'^cart/', include(urls_cart)),
    url(r'^about/', include(urls_about)),
    url(r'^checkout/', include(urls_checkout)),
    url(r'^search/', include(urls_search)),
    url(r'^media/(?P<path>.*)$', static.serve, {'document_root': MEDIA_ROOT}),
    url(r'^home/', include(urls_home)),
]

And finally this is how I am calling it in the base.html

 </li>
                    <li class="nav-item"><a class="nav-link" href="{% url 'about' %}">About</a></li>
                    <li class="nav-item"><a class="nav-link" href="{% url 'all_hobby_products' %}">Book</a></li>

I am getting the Error: NoReverseMatch

enter image description here

I have tried to create a new app "about" but I am still getting the same errors. I need to add a few html pages and assume that the issue is with the url but I cannot see where there is a discrepancy. Any ideas or help would be greatly appreciated!!

Upvotes: 0

Views: 43

Answers (2)

Mugoma
Mugoma

Reputation: 324

NoReverseMatch Error simply means that Django could not get a proper reversal using the name given. The method which the URLs have been included might be the problem. According to Django, there are several ways of including URLs.

https://docs.djangoproject.com/en/3.1/ref/urls/#include

Quick fix urls.py in base app

...

#from home import urls as urls_home
...

urlpatterns = [
    ...
        url(r'^home/', include('home.urls')),
    ...

I've tried using the information given and it works.

You could also include other urlpatterns from other apps in a similar way

Upvotes: 1

Redgren Grumbholdt
Redgren Grumbholdt

Reputation: 1230

From your defined URLs, I don't see where all_hobby_products is defined, or maybe you left a snippet from another urls.py? Either way, try this:

In your sub url configs, i.e. those you include in your base/root URLs config, put a app_name = 'app_name_here' to namespace your URLs, that way it doesn's get messy. So to add on to your home\urls.py:

...

app_name = "home"

urlpatterns = [
     url(r'^about$', about, name='about'),
]

So to get the 'about' URL, you will do this in your template(s):

<a href="{% url 'home:about' %}">Home</a>

UPDATE

Your issue might actually be coming from how you are includeing your sub URLs, try changing that to for example:

...
    url(r'^home/', include("home.urls")),

Where "home" is your home app and "urls" is a "urls.py" in your home app.

Upvotes: 0

Related Questions