英文:
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')), # 🖘 no prefix
path('df/', views.df_show),
]</code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论