Reputation:
I'm new to django. I am trying to learn it by building a project. I wanna replace the django's localhost hompage with my homepage. But, I couldn't find views.py in main project directory.Why? By the way, I made a new 'views.py' and also created a templates folder and put index.html in another directory inside templates named englishexchange. Below is the project structure. englishexchange/ manage.py and exnglishexchange/ views.py,urls.py and templates/englishexchange/index.html
englishexchange/url.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name = 'index'),
path('admin/', admin.site.urls),
]
englishexchange/views.py
from django.shortcuts import render
def index(request):
return render(request, 'englishexchange/index.html')
and I put my templates directory in main project directory englishexchange.
I know I can do that by making another app and maping the url to nothing like ' '. But I want them to have different home page than the main's static home page.
The error I am getting is like
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\Sujeet Agrahari\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\templates\englishexchange\index.html (Source does not exist) django.template.loaders.app_directories.Loader: C:\Users\Sujeet Agrahari\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\templates\englishexchange\index.html (Source does not exist)
I believe it's trying to find the templates in its default directory which is admin. How can I change this behavior so that it gets the templates from mian project directory?
Upvotes: 0
Views: 1383
Reputation: 692
But, I couldn't find views.py in main project directory.Why?
Django encourages you to make apps for almost anything you do. So there is no views.py in the main project directory.
Regarding template rendering be sure to add:
os.path.join(BASE_DIR,'templates')
in 'DIR' as stated in an answer above.
Upvotes: 0
Reputation:
Make sure to add this in your settings file
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',
],
},
},
]
where you can change the 'templates' in DIRS
to any folder you want to keep your template files to.
Upvotes: 0
Reputation:
Make sure your views.py file is inside the app folder you made , and templates folder inside the project folder not your app folder , for changing current home page , just add
path('home/', views.index , name = 'index'),
You can replace home with any thing you want your home page url to be.
Upvotes: 0