Django-使用HTML HREF链接到另一个视图
在我的主页中,我有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要重定向到您的观点,而不是模板。为此,您的HREF应在
{%url'your_view_name'%}
下。因此,在您的情况下,最终结果就是这样:
请注意,如果您在
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 :
Note that if you defined an
app_name
in yoururls.py
, your href would then be{% url 'your_app_name:your_view_name' %}
.我们在不考虑主要根的情况下编写其余地址:
示例: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}}