我是刚开始使用Django在Python中创建项目的新手。
我正在使用Django框架在Python中创建一个网站。我已经在“ views.py ”文件中创建了一个“ index ”函数,以呈现“ template ”文件夹中的“ index.html ”文件。以下是“ views.py ”文件的代码。
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,"index.html",{})
我还添加了“索引”页面的导航。以下是“ urls.py ”文件的代码。
from django.contrib import admin
from django.urls import path
from gallery import views
urlpatterns = [
path('admin/', admin.site.urls),
path('index/', views.index,name="index")
]
以下是settings.py文件中模板部分的代码。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates'),], #path to templates folder in local system.
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
但是,当我尝试打开“ index ”页面URL时,无法查看该页面。
错误截图供参考
文件夹结构供参考:
Gallery_Website
|--gallery
|_ __init__.py
|_ admin.py
|_ apps.py
|_ models.py
|_ tests.py
|_ views.py
|--Gallery_Website
|_ __init__.py
|_ settings.py
|_ urls.py
|_ wsgi.py
|--templates
|_ index.html
|--db.sqlite3
|--manage.py
定义“索引”页面的导航时可能出什么问题?
让我知道是否需要其他信息。
In your settings.py file, add a value to you template dictionary key -- DIRS=[]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
then create a folder "templates" where "manage.py" file resides. And in that templates folder, create a file "index.html". Then you will be a able to see your html page.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句