主页链接发送到“关于/关于主页无法到达”页面

时间:2019-04-20 02:01:54

标签: django html5 django-urls homepage

我正在尝试返回到本地主机的主页。但是,当尝试使用某些python脚本链接到首页时,它会将我发送到我的论坛页面。甚至陌生人,我的论坛页面也位于forum / forum /。在使用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>

1 个答案:

答案 0 :(得分:-1)

以上项目urls.py您正在导入ur include函数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>

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