Django project template doesn't exist - python

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.

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: How to pass variables from view to a template

I'm having trouble passing a variable from view to an html file. This is what I did so far:
My root directory (where the manage.py file is) contains the following folders:
myapp
myproject
templates
myproject/urls.py looks like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls'))
]
myproject/settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
myapp/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
templates/index.html:
<p>Hello {{ name }}</p>
When running the server locally, the only thing it shows is "Hello" without the name. What's wrong?
So let's start with views.py. When your urls.py calls your function in views.py, it's asking it to render a template.
Let's walk through a view function:
def index(request): the function the view
context = {'name':'Sol'} the key in the dictionary is what the HTML can call it and the value is what HTML gets back. So, when you do "{{ name }}" in HTML, it is replaced by Django with "Sol".
return render(request, "index.html", context) Making Django render it, request is the parameter from the function, "index.html" is the html file that Django is going to render, and context is the key + value pair that Django is going to use to replace.

Trying to set up urls.py to direct to index.html

As the question implies, I am trying to set up my urls.py file to point towards my index.html file.
Here is the structure of my project:
-->mysiteX
---->.idea
---->mysite
------->migrations
__init__
admin.py
apps.py
models.py
test.py
views.py
---->mysiteX
-------->templates
index
---->css
---->fonts
---->js
---->vendors
__init__
settings
urls
wsgi
----->venv
db.sqlite3
manage
This is what my urls.py file looks like
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^index/', admin.site.urls),
]
my views.py file
from __future__ import unicode_literals
def index_file(request):
return render(request, "index.html")
settings.py:
import os, sys
abspath = lambda *p: os.path.abspath(os.path.join(*p))
PROJECT_ROOT = abspath(os.path.dirname(__file__))
sys.path.insert(0, PROJECT_ROOT)
TEMPLATE_DIRS = (
abspath(PROJECT_ROOT, 'templates'),
)
When I run manage.py I get this page
Update after making changes to my urls.py file to this extent:
from django.conf.urls import url
from django.contrib import admin
from gradientboost import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^/$',views.index_file,name='index')
]
This is what I am getting
Second Update
changed my settings.py file according to an answer given
import os, sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'mysiteX/templates')
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'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',
],
},
},
]
However still getting this error
You Need To Change Your Urls.py
Because You Are Changing Admin Url Admin Url Is For Admin Login And For Website Control!
and if you add index/ than you need to type in your brower like that domainname.com/index
So if you Want Your index.html show on website home page try my code:
Try This If You browsing this link 127.0.0.1:8000
from django.conf.urls import url
from django.contrib import admin
from mysite import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index_file, name='YOUR NAME')
]
And if you trying this link 127.0.0.1:8000/index Try This:
from django.conf.urls import url
from django.contrib import admin
from mysite import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/$', views.index_file, name='YOUR NAME')
]
Also Add This In Your Settings.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'mysiteX/templates')
And Also Add This In your Settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'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',
],
},
},
]
create a html template index.html and your url should be redirected the view.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', your_view, name = 'index'),
]
You don't reference templates from your urls.py. That is for matching routes up to view functions, and it is the view's job to tell Django to render a template, if appropriate. You already have a simple view that renders index.html, you just need to reference it in your urlconf.
If you want your index.html rendered at the root of your site, then you need to add this line to your urls.py (EDIT: fixed the route):
url(r'^$', index_file)
You will also need from mysite.views import index_file at the top of your urlconf.

How can I view my HTML page in Python using Django

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.

Django : Template not found/index.html

I am running a website using Django. There is no problem in login. When i logged in and click on some dashboard, it is showing "page not found"(404).
Views.py:
def index(request):
return(request,'obs_app/index.html')
Settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'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',
],
'libraries': {
'get_by_index':'obs_app.templatetags.templatefilters',
'get_by_key':'obs_app.templatetags.templatefilters',
'get_dict':'obs_app.templatetags.templatefilters',
'get_items':'obs_app.templatetags.templatefilters',
'multiple':'obs_app.templatetags.templatefilters',
},
},
},
]
urls.py:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from obs_app import views
urlpatterns = [
url(r'index/',views.index , name = 'index'),
path('admin/', admin.site.urls),
path('', views.user_login, name='user_login'),
path('dashboard',views.obs_index, name='admin_dash'),
path('halls/active',views.obs_halls_active,name='active-halls'),
path('halls/pending',views.obs_halls_pending,name='pending-halls'),
path('febs/userlist', views.febs_user_list, name='febs-users'),
path('bookings/user', views.bookings_user, name='bookings-users'),
path('bookings/owner', views.bookings_owner, name='bookings-owner'),
path('cancellation/user', views.cancelled_user, name='cancelled-user'),
path('cancellation/owner', views.cancelled_owner, name='cancelled-owner'),
path('terms/obs', views.terms_conditions, name='terms-conditions'),
path('terms/febs', views.terms_febs, name='terms-febs'),
path('terms/febs/events', views.terms_febs_events, name='terms-febs-events'),
Error i am getting is :
Page not found (404)
The current path, index.html, didn't match any of these.
You have to render template and also fix the indentation
def index(request):
return render(request,'obs_app/index.html')
And also here you need to provide directory instead of file
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
Change:
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates/index.html')
To:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
TEMPLATES = [
{
...
'DIRS': [], # DIRS defines a list of directories where the engine should look for template source files, in search order.
...
},
]
You might want to read: Support for template engines
Related part copied here:
DIRS defines a list of directories where the engine should look for template source files, in search order.

Categories