I am new to django , I am facing one while django running server , I have copy and paste my code please tell me what is wrong with my code
'DIRS': [templates],
NameError: name 'templates' is not defined
in settings.py file , I have put templates in [] brackets
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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 is my views.py
from django.http import HttpResponse
from django.shortcuts import render
def index(response):
return render(request,'index.html')
this is urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about', views.about, name='about'),
]
templates should be string. so ['templates'] instead of [templates]
You need to change DIRS here
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Here, Django expects it has a template path but you provide templates variable which does not have any path.
While others have already given right answers on how to fix it, but I would like to point out a minor thing that you are getting the NameError because you haven't defined the name templates.
The NameError is raised when the variable you are using is not defined anywhere in the program (if you defined it outside the current scope you will be getting UnboundLocalError).
If you define the name templates as a string for the absolute path to your template folder, this will work. Still, never use absolute path in your Django application cause it will become a headache while deployment.
You have to change your line to this
'DIRS': [os.path.join(BASE_DIR, 'templates')],
In the begging of the settings files you can see the BASE_DIR
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Changing DIRS to
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Will help the app to find the templates folder.
Just a personal advice in each app store the templates/appname folder for the specific app. It will help you to have a more robust Django app.
for example you have the polls app, inside the polls app it will be like this
pollsApp/templates/pollsApp/index.html etc.
Also here is the official guide, it might help you
Related
I created simple form register user but something is wrong. This is my django project:
settings.py
account/urls.py
forms.py
views.py
Directories:
error:
I don't understand this issue. I use render(request, 'name_template', {}) but django request name_template. What did I do wrong?
Sorry for my english but I still learn ;)
in your path it should be
path('register/, views.register, ....)
Could you check your TEMPLATES section in settings.py. It looks something like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
},
]
Check if you have path specified to your templates in 'DIR' key.
If not, then add specified path to your templates folder.
TEMPLATES_DIR = os.path.join(BASE_DIR, 'account','templates')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIR': [TEMPLATES_DIR,]
},
]
Documentation
https://docs.djangoproject.com/en/4.0/ref/settings/#templates
you're mapped a wrong view inside urls.py
path('register/', views.render, name='register'),
Change it to
path('register/', views.register, name='register'),
I'm setting up the structure for a new project in Django. I've created the app 'accounts', within which I've added a templates folder containing an html template. However, when I go into the development server and click on a link to this page from my index page (which loads no problem), it returns a TemplateDoesNotExist error message.
I've examined the error message and the Template-loader postmortem details the correct pathway for my html template (I've checked this and made sure countless times), suggesting that Django is looking in the right place for it. However, it also says 'source does not exist'. Does anybody have any troubleshooting tips, considering Django appears to be searching the correct pathway?
# From settings.py ('accounts' is also included with INSTALLED_APPS):
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# ...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
# From urls.py:
from django.conf.urls import url, include
from accounts.views import index
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', index, name="index"),
url(r'^accounts/', include('accounts.urls')),
]
# From urls.py (accounts):
from .views import signup
urlpatterns = [
url(r'^signup/$', signup, name="signup"),
]
# From views.py (accounts):
def signup(request):
return render(request, 'signup')
I guess do you have a newer version of django on your server machine.
DIRS is now (2.2) into TEMPLATES setting var. On django old version (ex: 1.8) was in DIRS setting var.
Then, check versions and move to newest one and use:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
...
},
]
on settings.
I'm learning Django from 'Mastering Django: Core' book and now I'm
stucked in this third chapter of the book which describes about the Django template.
The problem I'm facing is I can't load a template from a specific directory because it gives me this "TemplateDoesNotExist at /home/" error.
Here is my project directory :
mywebsite/
mywebapp/
...
...
views.py
...
temp/
template.html
Here is TEMPLATES list from settings.py :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/temp'],
'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',
],
},
},
]
And finally here's my view :
from django.template.loader import get_template
def home(request):
t = get_template("template.html")
c = Context({'heading':'Welcome to MyWebsite.',
'name':'Arya Stark','age':19})
return HttpResponse(t.render(c))
Note: The template I'm using in this view is in the temp directory.
So, can you please explain me why would that error happen?
You've set DIRS to "/home/temp", but as you've clearly shown in your directory structure, your templates are in "mywebsite/temp". You'll probably need to use the full path there, or at least os.path.join(BASE_DIR, 'temp').
First of all, check if in your settings.py BASE_DIR is declared, if it is,
then check,
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Also, in your DIRS in TEMPLATES,
'DIRS' : [BASE_DIR + '/temp/',]
This should do the trick,
Also, django as default checks for the sub-directory templates in your app-directory and project-directory as well, when you APP_DIRS = True.
I got an error:
TemplateDoesNotExist at /accounts/login/ registration/login.html.
I think I should create login.html file but probably it is not required in Django for default beahivor.
After I placed login.html in accounts/templates/accounts the error has not disappeared.What should I do next?
I wrote in urls.py of accounts,
from django.conf.urls import url
from django.contrib.auth.views import login, logout
urlpatterns = [
url(r'^login/$', login,
name='login'),
url(r'^logout/$', logout, name='logout')
]
in urls.py of parent app,
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls')),
url(r'^api/', include('UserToken.urls')),
]
in TEMPLATES of settings.py of parent app
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
I found in blowser,
emplate-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/templates/registration/login.html (Source does not exist)
I think maybe I should make templates folder.
Now,I made a directory like accounts/registration/accounts/login.html .
I cannot understand why error shows 2 ways directory to admin&auth.Should I make admin&auth directory?
For me worked adding:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Take care to delete browser cache.
The pathnames in your error messages are showing a space character just after login/ - this is probably not contained in the actual filenames, thus the files can't be found. In one case it's a normal space, in the other it's a Unicode 'IDEOGRAPHIC SPACE' (U+3000). I don't see where this is coming from in your source code.
check all your folders name what you put same Error I also receiving.
I mistakenly put template instead of templates.
Django default takes templates folder rather than just template or any other name.
This resolves my problem.
I got this error during the python django project
I do not understand why the template does not connect.
Please let me know which part of the error it is and let me know how to fix it.
How can I to do?
Attach an error picture.
enter image description here
settings.py
INSTALLED_APPS = [
--- skip ---
board.apps.BoardConfig',
]
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',
],
},
},
]
These are my project sub-lists
/mysite
/board
/migrations
/templates
/board
board_list.html
board_detail.html
search.html
__init__.py
admin.py
apps.py
forms.py
models.py
tests.py
urls.py
views.py
/mysite
__init__.py
settings.py
urls.py
views.py
wsgi.py
/static
css
js
image
/templates
base.html
main.html
db.sqlite3
manage.py
/mysite/mysite/urls.py
from django.contrib import admin
from django.conf.urls import url, include
from .views import MainHome
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', MainHome.as_view(), name='main'),
url(r'^board/', include('board.urls', namespace='board'),
]
/mysite/board/urls.py
from django.conf.urls import url
from .views import *
from mysite.views import MainHome
app_name = 'board_app'
urlpatterns = [
url(r'^$', MainHome.as_view(), name='main'),
url(r'^search/$', SearchFormView.as_view(), name='search'),
url(r'^boards/$', BoardList.as_view(), name='board_list'),
url(r'^boards/(?P<slug>[-\w]+)/$', BoardDetail.as_view(), name='board_detail'),
]
/mysite/borad/views
from .models import Board
from django.views.generic import ListView
--- skip ---
class BoardList(ListView):
model = Board
template_name = 'board_list.html'
content_object_name = 'boards'
paginate_by = 10
--- skip ---
In /mysite/borad/views, you have:
template_name = 'board_list.html'
Replace it with:
template_name = 'board/board_list.html'
Because board_list.html is inside templates/board.
Edit
In settings.py, you have:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
which means that Django will look for templates in your mysite/templates folder. Remove it like this:
'DIRS': []
so Django will look by default inside templates/ directory for every installed app.
Your template is inside board directory. And your model template name is not referencing to that path.
In your class BoardList add template_name = 'board/board_list.html'
I'd try a systematic approach:
in settings.py, define TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
Given your directory structure, your app name is board not board_app. So, set INSTALLED_APPS = [..., 'board']. Also, I don't understand in your code why you refered to board.apps.BoardConfig instead of board, maybe am I missing something
TEMPLATES = [{ .... 'DIRS': [TEMPLATE_DIR], ... }] will make things more visible
in urls.py of board app, set app_name=board
in views.py, if you use Django naming conventions for file / directories, you don't need to specify the template name for class based views. Remove template_name = 'board_list.html provided your template is in board/templates/board/board_list.html
Also, reboot the server after performing those changes.
Does this help?