Django不要手动更改URL并转到index.html页面。

huangapple go评论66阅读模式
英文:

Django don't manually change the URL and go to the index.html page

问题

我是Django的新手。让我们假设我的项目名称是mysite,我有一个名为Polls的应用程序。
在模板文件中,我有两个HTML文件:"index.html"和"df.html"。
现在,当我运行服务器时,我必须手动更改URL,在"http://127.0.0.1:8000/"之后添加"polls"来显示index.html,

但我希望当我在终端中点击链接时,它可以直接将我导航到index.html。我不想手动更改URL。

谢谢你,如果有任何帮助,我真的很感激!

views.py

def index(request):
xxxx
return render(request, "index.html")

polls\urls.py

from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

mysite\urls.py

from django.contrib import admin
from django.urls import include, path
from polls import views
urlpatterns = [
path('polls/', include('polls.urls')),
path('df/', views.df_show)
]
英文:

I am new to Django. Let's assume my project name is mysite, and I have one app called Polls.
I have 2 html in the templates file: "index.html", "df.html"
Now, when I runserver, I have to manually change the URL, add "polls" after "http://127.0.0.1:8000/" to show the index.html,

but I want when I click the link in the terminal, it can direct me to the index.html. I don't need to manually change the URL

Thank you and any help if really appreciated!

views.py

def index(request):
xxxx
return render (request,"index.html")

polls\urls.py

from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]

mysite\urls.py

from django.contrib import admin
from django.urls import include, path
from polls import views
urlpatterns = [
path('polls/', include('polls.urls')),
path('df/', views.df_show)
]

答案1

得分: 2

这是由于您自己添加的前缀造成的。如果您不想要这个前缀,您应该省略 polls/

from django.contrib import admin
from django.urls import include, path
from polls import views

urlpatterns = [
    path('', include('polls.urls')),  # 无前缀
    path('df/', views.df_show),
]
英文:

That's due to the prefix you added yourself. You should omit the polls/ if you don't want this prefix:

<pre><code>from django.contrib import admin
from django.urls import include, path
from polls import views

urlpatterns = [
path('', include('polls.urls')), # &#x1f598; no prefix
path('df/', views.df_show),
]</code></pre>

huangapple
  • 本文由 发表于 2023年6月29日 23:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定