How to connect some Django template to html hyperlink?

[1]For example i have such html page(main page) [1]: https://i.stack.imgur.com/PJlbd.png Also there are three hyperlink tags, and i want to generate some template for each tag when it’s clicked. For example, if “home” is clicked, i will generate the same page with template of all posts of my blog. If ‘create’ is clicked, i’ll generate the same page with another template that contains a form for creating of a post. It’s a code of my main page

<body>
<div class="left-menu">
    <img src="/static/POST PROJECT.png" class="logo" alt="">
    <ul class="left-ul">
        <li class="left-li">
            <a href="{% url 'main'%}">Home</a>
        </li>
        <li class="left-li">
            <a href="{% url 'main'%}">Create</a>
        </li>
        <li class="left-li">
            <a href="{% url 'main'%}">Find</a>
        </li>
    </ul>
</div>
<div class="main">
    {% block content %}
    {% endblock %}
</div>

My urls.py

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

It’s my views.py

def main(request):
    return render(request, "index.html", {})

Answer

You can just use context for that. You are already passing it, but it’s only an empty dict. One way to do it is by modifying your main view to accept a slug, and do something according to that slug’s value.


urls.py

urlpatterns = [
    path('/<str:slug>', views.main, name='main')
]

views.py

def main(request, slug):
    context = {}

    if slug == "home":
        posts = # query all your posts here
        context["posts"] = posts
    elif slug == "create":
        form # create the form instance here
        context["create_form"] = form
    elif slug == "somethingelse":
        somethingelse = # do something else
        context["somethingelse"] = somethingelse

    return render(request, "index.html", context)

index.html

<div class="left-menu">
    <img src="/static/POST PROJECT.png" class="logo" alt="">
    <ul class="left-ul">
        <li class="left-li">
            <a href="{% url 'main' slug='home' %}">Home</a>
        </li>
        <li class="left-li">
            <a href="{% url 'main' slug='create' %}">Create</a>
        </li>
        <li class="left-li">
            <a href="{% url 'main' slug='find' %}">Find</a>
        </li>
    </ul>
</div>
<div class="main">
    {% block content %}
    {% endblock %}
</div>