Django navigation bar customization

I attempted to implement a navigation bar on my Django website, but encountered an error stating "Reverse for 'about' not found. 'about' is not a valid view function or pattern name." I followed this solution from [Stack Overflow][1] in order to create the navigation bar. Are there any alternative approaches you can suggest or assist me with debugging this code? Below is the full content of my base.html file:

    <!DOCTYPE html>
<html>
  <head>
    <title>Django Central</title>

    <link
      href="https://fonts.googleapis.com/css?family=Roboto:400,700"
      rel="stylesheet">
    <meta name="google" content="notranslate" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link
      rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
      integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
      crossorigin="anonymous"
    />
    
  </head>

  <body>
    ...
    {% block nav %}
    <ul id="nav">
        <li>{% block nav-home %}<a href="{% url 'home' %}">Home</a>{% endblock %}</li>
        <li>{% block nav-about %}<a href="{% url 'about' %}">About</a>{% endblock %}</li>
        <li>{% block nav-contact %}<a href="{% url 'contact' %}">Contact</a>{% endblock %}</li>
    </ul>
    {% endblock %}
    ... 
    <style>
      body {
        font-family: "Roboto", sans-serif;
        font-size: 17px;
        background-color: #fdfdfd;
      }

    .shadow{
           box-shadow: 0 4px 2px -2px rgba(0,0,0,0.1);
       }
      .btn-danger {
        color: #fff;
        background-color: #f00000;
        border-color: #dc281e;
      }
     
     .masthead {
              background:#3398E1;
              height: auto;
              padding-bottom: 15px;
              box-shadow: 0 16px 48px #E3E7EB;
              padding-top: 10px;
    }
    </style>

    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-light bg-light shadow" id="mainNav">
      <div class="container-fluid">
        <a class="navbar-brand" href="{% url 'home' %}" >Django central</a>
        <button
          class="navbar-toggler navbar-toggler-right"
          type="button"
          data-toggle="collapse"
          data-target="#navbarResponsive"
          aria-controls="navbarResponsive"
          aria-expanded="false"
          aria-label="Toggle navigation"
        >
          <span class="navbar-toggler-icon"></span>
        </button>
       
        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav ml-auto">
            
            <li class="nav-item text-black">
              <a
                class="nav-link text-black font-weight-bold"
                href="#"
                >About</a
              >
            </li>
            <li class="nav-item text-black">
              <a
                class="nav-link text-black font-weight-bold"
                href="#"
                >Policy</a
              >
            </li>
            <li class="nav-item text-black">
              <a
                class="nav-link text-black font-weight-bold"
                href="#"
                >Contact</a
              >
            </li>
          </ul>
        </div>
      </div>
    </div>
    </nav>
    

            {% block content %} 
          <!-- Content Goes here -->
            {% endblock content %}
    
    <!-- Footer -->
    <footer class="py-3 bg-grey">
              <p class="m-0 text-dark text-center ">Copyright &copy; Django Central</p>
    </footer>
    
  </body>
</html>

                                                          Warm regards,
                                                         Dinindu

Answer №1

Double check that you've included the name="about" parameter in your urlpattern. It should be set up similar to this:

urlpatterns = [
    path('about',  views.about_view, name='about'),
]

If you are using Class-Based Views (CBVs)

urlpatterns = [
    path('about',  views.AboutView.as_view(), name='about'),
]

Answer №2

On the navigation bar page, you referenced URL patterns named 'home', 'about', and 'contact'.

<li>{% block nav-home %}<a href="{% url 'home' %}">Home</a>{% endblock %}</li>
        <li>{% block nav-about %}<a href="{% url 'about' %}">About</a>{% endblock %}</li>
        <li>{% block nav-contact %}<a href="{% url 'contact' %}">Contact</a>{% endblock %}</li>

Ensure that you have defined named URL routes in your urlpatterns:

path('about',  views.aboutview, name='about'),

Also, make sure to declare corresponding views for each of these URLs in your views.py file.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Removing empty table rows using JavaScript when the page loads

Within my HTML table, I have noticed some empty cells and rows that need to be removed. To give an example of the code: <table id="7"> <tr> <td>Name</td> <td>Type</td> <td>Parent</td> <td>Time</td&g ...

Creating a Masonry Slider with varying heights and widths of images is a simple and effective way to showcase diverse content in a visually

I am looking to implement a unique grid image slider that accommodates images of varying heights and widths. I envision something similar to the example shown below. The image showcases a pattern where the sliders alternate between square images and two r ...

Obtaining metadata from Youtube videos with Javascript and HTML5

Is there a way to fetch YouTube video data with Javascript and HTML5? If so, could you provide some helpful resources or links? ...

What are some ways to monitor the movement of elements and activate functions at precise locations?

I am working on a project that involves a #ball element which, when clicked, utilizes jQuery animate to move downwards by 210px. The code I currently have is as follows: $('#ball').click(function() { $(this).animate({ top: '+=2 ...

Is there a way to assign the value of a textfield to a session attribute in JSP prior to rendering?

Imagine I have the following code snippet: <html> <head> <script> function setSession() { var roll=document.getElementById("rollno").value; session.setAttribute("rollno", roll); } & ...

What could be the reason for receiving the error message "Unresolved variable or type approvedPriceDealerCostForm" in my HTML

I'm working with Angular 8 and facing an issue with sending the approvedPriceDealerCostForm when clicking my button. I keep getting an error that says "Unresolved variable or type approvedPriceDealerCostForm". Please refer to the attached image for mo ...

Can the arrangement of icons/buttons on the JW Player control bar be customized?

I am trying to customize the skin of my JWplayer like this: This is my current progress: I have been researching how to rearrange the order of icon / button on the controlbar. According to the jwplayer documentation, the buttons on the controlbar are div ...

Error occurs when SRCSET loads the wrong size because "sizes" parameter is missing

According to the guidelines, when sizes is not specified in the image attribute, the browser should automatically calculate the required size based on CSS rendering. In this scenario, my image is 300px and the browser should select the 300px image. Howeve ...

Utilizing the '<' or '>' operator in combination with an if statement within a Repeater component

Is it possible to use an if statement in a repeater like this? <%#Eval("FN").ToString().Count > 0 ? "SB" : "" %> When I try to implement this, I receive an error 73 indicating that the > operator cannot be used. How can I adjust it so that i ...

Embed shapes inside the margins of an HTML container

I'm aiming to create something unique by placing the circle and triangle on the borders of an HTML block. https://i.sstatic.net/MVYuc.png Below is the CSS code for my block: .block { color: red; } .cercle { border-radius: 50%; } .triang ...

Managing images within a bootstrap column: Tips and tricks

I'm using a bootstrap col-md-1 with an image inside, wrapped in a div with padding. The issue is that the image is too small and I want it to be at least as big as the original size, https://i.sstatic.net/9hqyj.png while still being responsive. An ...

I'm looking for a way to add an onclick event to every element created by Vue.js through a "v-for" loop in order to toggle the visibility of its inner content

My Vue data consists of: data: function() { return { results: { "items": [ { "id": "770c257b-7ded-437c-99b5-3b8953b5be3a", "name": "Gsbssbb", "price": 9559, "colour": { ...

Set styles to child elements of an external component using styled-components

In my Component.tsx file, I am using the following template: import {Foo} from 'external-stuff' <Foo> <p>bar</p> </Foo> The Foo component is designed to accept a className parameter. const Foo = ({className, title}) =& ...

Embracing right-to-left (RTL) languages

I am looking to create a website that can support both left-to-right (LTR) and right-to-left (RTL) languages seamlessly. My goal is to have the text direction switch automatically to RTL if the content is in an RTL language. Additionally, I want the input ...

unable to establish filters within a Django project

I'm encountering an issue while trying to add a filter in my project. The following error is displayed in CMD: AttributeError: module 'rest_framework.filters' has no attribute 'DjangoFilterBackend' Project Name: frame ProjectApp N ...

Arrange elements within a navigation bar

As a beginner in coding, I am taking on the challenge of creating a webpage for a student project using React. I've hit a hurdle while working on the navbar for the page. How can I align the items (Home, Project, Team, Contact) to the right side of t ...

When echoing SESSION in PHP, the previous value is displayed

<script language="javascript"> function handleEmergency() { if(document.getElementById('emer').checked == true) { <?php $_SESSION['total2']= $_SESSION['total1'] ...

Can non-mobile browsers handle touch events effectively?

Touch and gesture functionalities are no longer limited to mobile devices. When can we expect to see these features integrated into web apps for devices like the MacBook Pro or any other device with a touchpad or trackpad? I'm particularly interested ...

Unable to perform the 'setSelectionRange' function on the 'HTMLInputElement' due to the input element's type being 'number', which does not allow selection

My attempt to enable text selection in an input box upon user click by using the code snippet below was unsuccessful: <input type="number" onclick="this.setSelectionRange(0, this.value.length)" name="quantity" /> Instead of achieving the desired ef ...

Retrieve a DOCX file via AJAX response

I am encountering an issue with a Django function that returns a file: ... return FileResponse(open('demo.docx', 'rb')) I am using ajax to fetch it on the client side. Now, I need to download it on the client side. This is the code I a ...