Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django – using HTML href to link to another view

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"),
]

>Solution :

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' %}.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading