Django-使用HTML HREF链接到另一个视图

发布于 2025-02-13 02:47:08 字数 861 浏览 2 评论 0原文

在我的主页中,我有2个链接,我想将用户带到不同页面。我将如何使HREF链接指向我在Views.py中创建的视图?将HTML文件名放入HREF中并单击它将在搜索栏中显示为/Qualifications/Qualifications.html时,将其显示为code>/premialigation/代码>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂牵梦绕锁你心扉 2025-02-20 02:47:08

您需要重定向到您的观点,而不是模板。为此,您的HREF应在{%url'your_view_name'%}下。

因此,在您的情况下,最终结果就是这样:

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

请注意,如果您在urls.py中定义了app_name,则您的HREF将为{%{% url'your_app_name:your_view_name'%}

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

北笙凉宸 2025-02-20 02:47:08

我们在不考虑主要根的情况下编写其余地址:
示例:127.0.0.1:8000/course_list/详细信息/id

#x是关键上下文名称
&lt; a href =“/course_list/detail/{{x.id}}}&gt; {{x}}}

We write the rest of the address without considering the main root:
Example:127.0.0.1:8000/course_list/detail/id

#x is a key context name in view
<a href="/course_list/detail/{{x.id}}>{{x}}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文