Compoot
Compoot

Reputation: 2375

Django site creating a link from html page to another app's main html page

I have an index page in a django project that has (in it's header.html page) a navigation bar. I want the navigation bar to actually work ...currently when I click on 'teachers' in the nav bar I get an error.

The code in header.html (which has the nav bar)

</button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item mx-0 mx-lg-1">
              <a class="nav-link py-3 px-0 px-lg-3 rounded js-scroll-trigger" href="/teachers/teachers.html">Teachers</a>
            </li>
        </div>

The link above in question is:

href="/teachers/teachers.html">Teachers

The file path/structure of where the teachers.html page is: C:\Users\User\Desktop\rast\teachers\templates\teachers

The error:

Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:

admin/
admin/
header/ [name='header']
[name='index']
teachers/
The current path, teachers.html, didn't match any of these.

My question is - what do I need to write in the header.html page (or do I need to dos something else?) to get the nav bar button 'teachers' to go to the teachers.html page.

Upvotes: 0

Views: 5351

Answers (2)

pastaleg
pastaleg

Reputation: 1838

Django is a framework that feature dynamic web page generation, meaning that there are functionalities that makes it easy to maintain and scale your web app, while retaining high customization and controll. One of these functions is Templates.

You have already placed the teachers.html in the template directory, so in a Django-world, you would put a url path that would direct the web page generator as follows:

href link > url > view > template

Your error message gives you an insight into this as it tells you what urls it have tried to match the given url address.

In your navbar.html put the dynamic url in with Django syntax:

<a href="{% url 'teachers %}">Teachers</a>

In your urls.py give Django the instructions on what view to map the teachers keyword to:

urlpatterns = [
    ...
    url(r'^teachers', views.teachers),
    ...
]

Now in your views.py you need to tell Django what the teachers function need to properly generate the web page.

def teachers(request):
    return render(request, 'app/teachers.html')

Where app should be replaced with the name of your app in Django.

Now this is a high level example of how you build up the dynamic framework in Django. Details may vary depending on your build and project structure. I recommend that you read the Django documentation and tutorials. They are thorough and detailed. https://docs.djangoproject.com/en/2.0/

Similarly, any page you want to point towards, should be declared in urls, and generated by a view. It is in the views.py that you are able to do a lot of the backend work that should be send to the template in a context.

Upvotes: 3

Stolson
Stolson

Reputation: 108

The URL isn't correct. Instead of pointing to the template filename:

href="/teachers/teachers.html"

Use the actual link you've supplied in the urls.py file:

href="/teachers/"

Upvotes: 1

Related Questions