Home > Enterprise >  Django - using HTML href to link to another view
Django - using HTML href to link to another view

Time:07-06

In my home page i have 2 links that i would like to bring the user to different pages. How would i make the href link to a view i have created in views.py? Putting in the html file name in as the href and clicking it would make the url in the search bar appear as /qualifications/qualifications.html when i want it to be /qualifications/

HTML:

<div id="nav_bar">
        <ul>
            <li><a href="home.html">Home</a></li>
            <li><a href="qualifications.html">Qualifications</a></li>         
        </ul>
    </div>

views.py

def qualifications(request):
    context = {"title": "Qualifications"}
    return render(request, "myApp/qualifications.html", context)

urls.py

urlpatterns = [
    path("", views.home, name="home"),
    path("qualifications/", views.qualifications, name="qualifications"),
]

CodePudding user response:

You need to redirect to your views, not your templates. To do that, your href should be under the form {% url 'your_view_name' %}.

So in your case the end result would be something like that :

<div id="nav_bar">
    <ul>
        <li><a href="{% url 'home' %}">Home</a></li>
        <li><a href="{% url 'qualifications' %}">Qualifications</a></li>         
    </ul>
</div>

Note that if you defined an app_name in your urls.py, your href would then be {% url 'your_app_name:your_view_name' %}.

  • Related