How can I view my HTML page in Python using Django - python

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.

You have to remove 'index/' in urls.py
Correct Code:
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index,name="index")
]

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.

Related

Django: Template does not exist

Please can you help me understand what I have done wrong here, I am sure it's a very simple problem but, I am new to django and especially the latest version...
I believe and as far to my research that I have routed my views to the best of my knowledge, please may you help identify my issue, I am getting an error:
"Page Not Found"
url.py from my app
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
views from my app
from django.shortcuts import render
from requests import request
# Create your views here.
def index(request):
return render(request, 'landing_page/index.html')
url from my project folder
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('/', include('comp.urls')),
path('admin/', admin.site.urls),
]
urls from my project
and then here is my project and template folders, I have attached an image for your ease of helping me:
In settings.py file, just add this:
os.path.join(BASE_DIR, 'templates')
EX:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], #Removed landing_page from here
'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',
],
#Removed libraries from here
},
},
]

Django project template doesn't exist

Hi i'm just getting started with Django and i'm running a project, I have created an HTML file and this is the views.py
def index(request):
return render(request, "hello/index.html")
and this is the urls.py inside the maine file
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include("hello.urls"))
]
and this is the urls.py inside the project file
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
and i got this error
view from my project
Make sure You have template folder same as where your manage.py
and also in settings.py
'DIRS': [os.path.join(BASE_DIR, 'templates')],
You have a typo. You have written templetes instead of templates
The problem is that you have simply misspelt the the template folder as templetes within your hello app. Rename it as templates and it will work
Add this to your main settings.py file.
and replace YOUR_APP_NAME with your django app name
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR, 'YOUR_APP_NAME/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',
],
},
},
]
Try creating the folder named templates inside the hello app. Then create a folder named by the app name 'hello'. That will enable django to read inside that folder and fetch for the template named index.html.
And the path will look like
projectDir/hello/templates/hello/index.html
Do so, hope it will help.

How to use template from project in another app?

I'm thinking in organizing all my templates in inside my project: scolarte.
As suggested by this question:
What is the best location to put templates in django project?
If you can’t think of an obvious place to put your templates, we
recommend creating a templates directory within your Django project
(i.e., within the mysite directory you created in Chapter 2, if you’ve
been following along with our examples).
But I need to call it from another app. The view is called but getting error:
TemplateDoesNotExist at /cuentas/ingreso/
scolarte/templates/scolarte/registration/signup.html
I even tried to put the full path to the template in project folder:
roles/views.py:
class SignUpView(TemplateView):
template_name = 'scolarte/templates/scolarte/registration/signup.html'
# don't work neither
#template_name = 'templates/scolarte/registration/signup.html'
#template_name = 'scolarte/registration/signup.html'
#template_name = 'registration/signup.html'
roles/urls.py:
from django.urls import include, path
from .views import SignUpView, SellerSignUpView, ClientSignUpView
urlpatterns = [
path('ingreso/', SignUpView.as_view(), name='signup'),
]
scolarte/urls.py
urlpatterns = [
path('', include('core.urls')),
path('cuentas/', include('roles.urls')),
path('admin/', admin.site.urls),
]
My app is orgnized like this:
roles
|_migrations
|_templates
...
|_urls.py
|_views.py
scolarte #project name
|_templates
|_scolarte
|_registration
|_signup.html
|_setting.py
|_urls.py
setting.py
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',
],
},
},
]
UPDATE 1:
roles app:
** roles app - view.py **:
UPDATE 2:
Your path to the template is incorrect by standard Django convention, but let me show you first how to fix it. What you'll want to do is make sure in settings.py you have these settings made. This is from a Django 3.0 fresh project creation.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__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',
],
},
},
]
If you use those, and follow the suggested directory structure of:
scolarte
|_scolarte
|_settings.py
|_urls.py
(etc..)
|_templates
|_scolarte
|_registration
|_signup.html
Then you can use with this path:
template_name = 'scolarte/registration/signup.html'

django TemplateDoesNotExist exception but template exists

I am trying to render a template in django.
I get this error when i connect to the site:
django.template.loaders.filesystem.Loader: path-to-azerty/templates/base.html (Source does not exist)
Here's my project directory structure:
azerty/
__init__.py
├── settings.py
├── templates
│   └── base.html
├── urls.py
├── views.py
└── wsgi.py
Here's my code:
// ulrs.py
from django.contrib import admin
from django.urls import path
from azerty import views
urlpatterns = [
path('', views.index),
path('admin/', admin.site.urls),
]
// settings.py
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',
],
},
},
]
//views.py
from django.shortcuts import render
def welcome(request):
return render(request, 'base.html')`
Your 'DIRS' setting is for a templates directory in your project directory (the one that contains manage.py).
If you want the templates to be on the inner directory (the one that contains settings.py, then you need to change it to:
os.path.join(BASE_DIR, 'azerty', 'templates')],
Another option that is sometimes used is to add azerty to your INSTALLED_APPS. Then the app loader will find the directory, and you can use 'DIRS': [],

How do I properly structure Templates and include them in the settings in Django 1.8.6?

So, here is my current structure:
django_project/
home_app/
__init__.py
migrations/
templates/
home_app/
base.html
admin.py
models.py
views.py
project_backend/
__init__.py
settings.py
url.py
wsgi.py
Now in home_app/views.py I have this very simple piece of code:
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = "home/base.html"
and in the settings.py I set up my templates like this:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'home_app', 'templates'),
# os.path.join(BASE_DIR, 'app_name', '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',
],
},
},
]
This works fine, and I am quite happy using the app/templates/app/some_temp.html approach, but this only works when I include the OS.path.joins for all the apps and templates, as far as I can see. Is there a way to register this structure in the settings by default and I am just overlooking it?
This is just no very pretty and I feel like it would be bad practice to write that down for every single app.:
os.path.join(BASE_DIR, 'home_app', 'templates')
You have 'APP_DIRS': True, so you shouldn't have to edit DIRS to use templates in the home_app/templates/ directory, as long as home_app is in your installed apps.
If you have a template,
home_app/templates/home_app/base.html
then you should use it with:
template_name = "home_app/base.html"
You currently have,
template_name = "home/base.html"
which isn't consistent.

Categories