如何导航到主页?python Django

2024-09-28 23:22:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用HTML创建了一个索引页面,并为“主页”按钮提供了一个URL,当单击主页按钮时,页面将从索引页面导航到主页。但它不会导航到主页,当在URL地址中单击时,地址会正确地更改到指定位置,但显示的页面仍然是索引页面

<div class="top-menu"> <div class="container"> <div class="row"> <div class="col-xs-2"> <div id="fh5co-logo"><a href="{% url 'index' %}">TrainedbySamson<span>.</span></a></div> </div> <div class="col-xs-10 text-right menu-1"> <ul> <li class="active"><a href="{% url 'tbs_home:home' %}">Home</a></li> <li><a href="{% static 'gallery.html' %}">Gallery</a></li> <li><a href="{% static 'about.html' %}">Trainer</a></li> <li><a href="{% static 'pricing.html' %}">Pricing</a></li> <li class="has-dropdown"> <a href="{% static 'blog.html' %}">Blog</a>

tbs_home/url.py:

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

tbs_home/views.py:

def home(request): return render(request, 'home.html')

模板/home.html:

{% load static %} {% block content%} <h1 style = "font-family:Georgia;font:40px;font-style:normal;">Hi! {{name}}</h1> <form action="add" method="POST"> {% csrf_token %} Enter 1st num : <input type="text" name="num1"><br> Enter 2nd num : <input type="text" name="num2"><br> <input type="submit"> </form> {% endblock %}

因此,当我根据代码单击home按钮时,页面应该导航到home.html页面?。但它仍然停留在同一个索引页上,提前感谢


Tags: textnamedivurlhomehtmlstatic主页
2条回答

您应该在导入库URL.py文件后添加app_name='tbs_home'。如果相同的问题仍然存在,请添加一个打印(“检查主功能”),查看它是否进入views.py中的主功能

试试这段代码,它工作正常。

enter image description here

设置.py

configure apps & templates.

url.py:

import views from app
path('', views.homeone, name='home'),

视图。py:

def homeone(request):
    return render(request, 'home.html')

Home.html

&13; 第13部分,;
{% load static %}


<div class="top-menu">
            <div class="container">
                <div class="row">
                    <div class="col-xs-2">
                        <div id="fh5co-logo">TrainedbySamson<span>.</span></div>
                    </div>
                    <div class="col-xs-10 text-right menu-1">
                        <ul>
{#                            <li class="active"><a href="{% url 'tbs_home' %}">Home</a></li>#}
                            <li><a href="{% url "home" %}"> Home2 </a>  </li>
                            <li><a href="{% static 'gallery.html' %}">Gallery</a></li>
                            <li><a href="{% static 'about.html' %}">Trainer</a></li>
                            <li><a href="{% static 'pricing.html' %}">Pricing</a></li>
                            <li class="has-dropdown">
                                <a href="{% static 'blog.html' %}">Blog</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
</div>

{% block content%}

<h1 style = "font-family:Georgia;font:40px;font-style:normal;">Cool&#128521 {{name}}</h1>


<form action="add" method="POST">
    {% csrf_token %}
    Enter 1st num : <input type="text" name="num1"><br>
    Enter 2nd num : <input type="text" name="num2"><br>
    <input type="submit">
</form>
{% endblock %}
和#13;
和#13;

相关问题 更多 >