主页链接发送到关于/无法回家的页面

问题描述 投票:0回答:1

我想回到我的主页,这是本地主机。然而,当尝试使用一些python脚本链接到家时,它将我发送到我的论坛页面。甚至更奇怪我的论坛页面是在论坛/论坛/。虽然它回到家里使用html href srcipt。也在家里装。这是怎么回事?即时通讯使用django 2.2和pyhton 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 html5 django-urls homepage
1个回答
-1
投票

上面的项目urls.py你导入你的包含功能2次,这就是为什么你得到论坛/论坛/ ...

修改你的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>

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

© www.soinside.com 2019 - 2024. All rights reserved.