【问题标题】:homepage link sends to about/about page unable to get home主页链接发送到关于/关于页面无法回家
【发布时间】:2019-09-10 04:25:16
【问题描述】:

我正在尝试返回本地主机的主页。但是,当尝试使用一些 python 脚本链接到主页时,它会将我发送到我的论坛页面。更奇怪的是,我的论坛页面位于 forum/forum/。在使用 html href srcipt 时,虽然它会回到家中。也加载在家里。这是怎么回事?我正在使用 django 2.2 和 python 3.6

#tcghome/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('hometemplate.urls')),
    path('forum/', include('hometemplate.urls')),
    path('admin/', admin.site.urls),
]



#hometemplate/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name ='tcg-home'),
    path('forum/', views.forum, name ='tcg-forum'),
]




#hometemplate/views.py
from django.shortcuts import render
from django.http import HttpResponse


posts = [
    {
        'author': 'Pyralis',
        'title': 'Test 1',
        'content': 'test content',
        'date_posted': 'April 19, 2019'
    },
    {
        'author': 'Pyro',
        'title': 'Test 2',
        'content': 'test content how are you',
        'date_posted': 'April 13, 2019'
    },
]


def home(request):
    context = {
        'posts': posts
    }
    return render(request, 'hometemplate/home.html', context)
    
def forum(request):
    return render(request, 'hometemplate/forum.html', {'title': 'About'})



#base.html

<a class="navbar-brand mr-4" href="{% url 'tcg-home' %}">The Coddiwomple Ginger</a>
<a class="nav-item nav-link" href="/">Home</a>
<a class="nav-item nav-link" href="{% url 'tcg-forum' %}">Forum</a>

#web source
 <a class="navbar-brand mr-4" href="/forum/">The Coddiwomple Ginger</a>
 <a class="nav-item nav-link" href="/">Home</a>
 <a class="nav-item nav-link" href="/forum/forum/">Forum</a>

【问题讨论】:

    标签: django html django-urls website-homepage


    【解决方案1】:

    上面的项目 urls.py 你正在导入你的包含函数 2 次,这就是为什么你得到 forum/forum/...

    修改你的 tcghome/urls.py:--

        from django.contrib import admin 
        from django.urls import path, include
    
        urlpatterns = [
          path('', include('hometemplate.urls')),
    
           path('admin/', admin.site.urls),
        ]
    

    在你的 HTML 中:-----

         #base.html
    
         <a class="navbar-brand mr-4" href="{% url 'tcg-home' %}">The CoddiwompleGinger</a>
         <a class="nav-item nav-link" href="{% url 'tcg-home' %}">Home</a>
         <a class="nav-item nav-link" href="{% url 'tcg-forum' %}">Forum</a>
    

    试试这个,如果有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 1970-01-01
      • 2018-01-21
      • 2022-07-19
      • 2012-04-23
      • 1970-01-01
      • 2016-09-22
      • 1970-01-01
      • 2019-12-08
      相关资源
      最近更新 更多