Reputation: 112
I'm new into creating projects in Python using Django.
I'm creating a website in Python using the Django framework. I've created an "index" function in the "views.py" file to render the "index.html" file present in the "templates" folder. Below is the code for the "views.py" file.
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,"index.html",{})
I've also added the navigation for the "index" page. Below is the code for "urls.py" file.
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")
]
Below is the code of templates section in settings.py file.
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',
],
},
},
]
But when I try to open the "index" page URL, I'm not able to view the page.
Error screenshot for reference
Folder structure for reference:-
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
What could be wrong while defining the navigation for the "index" page?
Let me know if any other information is required.
Upvotes: 0
Views: 2454
Reputation: 232
You have to remove 'index/
' in urls.py
Correct Code:
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index,name="index")
]
Upvotes: 1
Reputation: 2103
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.
Upvotes: 0