django将不同的链接重定向到同一页面

时间:2017-06-01 04:59:20

标签: python django frontend

from django.shortcuts import render, HttpResponse

# Create your views here.
def index(request):
    return render(request, 'platoweb/index.html')

def about(request):
    return render(request, 'platoweb/about.html')

这是views.py文件。

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index.html'),
    url(r'^$', views.about, name='about.html'),

]

这是app urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

admin.autodiscover()


urlpatterns = [

    url(r'^platoweb/index.html', include('platoweb.urls')),
    url(r'^platoweb/about.html', include('platoweb.urls')),
    url(r'^admin/', admin.site.urls),
# .   url(r'^posts/', include("posts.urls", namespace='posts')),
]

if settings.DEBUG:
        urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

这是全球urls.py

基本上,我试图点击两个不同的网址,index.html和about.html(以及更晚些时候)。但是,当我运行它时,index.html和about.html都会重定向到index.html。

我测试了两个html文件,它们工作得很好。任何想法或想法?

1 个答案:

答案 0 :(得分:1)

首先,您需要像这样更改global urls.py

from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

admin.autodiscover()


urlpatterns = [

    url(r'^platoweb/', include('platoweb.urls')),
    url(r'^admin/', admin.site.urls),
# .   url(r'^posts/', include("posts.urls", namespace='posts')),
]

然后是您的app urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^about$', views.about, name='about'),

]

然后网址http://127.0.0.1:8000/platoweb将转到您的索引页面,http://127.0.0.1:8000/platoweb/about将转到您的关键页面。