0

我正在尝试返回本地主机的主页。但是,当尝试使用一些 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>
4

1 回答 1

-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-04-20T02:40:40.637 回答