【问题标题】:How to navigate to home page? - python django如何导航到主页? - 蟒蛇django
【发布时间】:2021-02-24 04:09:57
【问题描述】:

我使用 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/urls.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.html 页面?但它保留在同一个索引页面中,在此先感谢。

【问题讨论】:

  • 当我更改它时,我收到错误消息“django.urls.exceptions.NoReverseMatch: Reverse for 'home' not found.'home' 不是有效的视图函数或模式名称。”当我运行服务器时

标签: python django


【解决方案1】:

您应该在导入库 urls.py 文件之后添加 app_name = 'tbs_home'。如果同样的问题依然存在,在views.py中添加一个打印(“check home function”)看看是否进入了home函数。

【讨论】:

  • 从 django.urls 导入路径从 . import views # from .views import home app_name = "tbs_home" urlpatterns = [ path('home',views.home, name='home'), ]
  • 不,你能推荐其他方法吗?
  • 能否分享github链接或者我们一起看代码
【解决方案2】:

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

enter image description here

settings.py

配置应用和模板。

urls.py:

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

Views.py:

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

Home.html

{% 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 %}

【讨论】:

  • 你在home.html中提到的代码我把它单独保存在Index.html中,因为index.html页面是主页面
  • 我试过你的代码它没有导航到 home.html,谢谢
  • 如果您更改了 html 页面名称,那么您也需要更改视图和 url。
  • 我的代码运行良好,还显示了屏幕截图。首先,您需要尝试使用相同的代码,清楚地理解它。之后,您需要进行修改。美好的一天。
猜你喜欢
  • 1970-01-01
  • 2017-10-28
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多