I followed this Django tutorial: https://docs.djangoproject.com/en/1.10/intro/reusable-apps/.
I have a project called oldcity and an app called oldantwerp. The app is located in a parent directory called django-oldantwerp and the app directory itself has a subdirectory templates. The index.html file that my project is looking for is situated like so:
django-oldantwerp>oldantwerp>templates>oldantwerp>index.html
I tried to use this app with my project by including it in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'oldantwerp'
]
and in urls.py (in the projec), like so:
urlpatterns = [
url(r'^oldantwerp/', include('oldantwerp.urls')),
url(r'^admin/', admin.site.urls),
]
When I go to the admin page, everything works, but when I try to open the index page I get this error:
TemplateDoesNotExist at /oldantwerp/
It says it tried to locate the index.html file like so:
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/admin/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/auth/templates/oldantwerp/index.html (Source does not exist)
And it also tried searching for another file: place_list.html, which is strange because I don't think I have such a file.
What could be wrong?
EDIT
This is the code of views.py in the oldantwerp folder:
class IndexView(generic.ListView):
template_name = 'oldantwerp/index.html'
context_object_name = 'places'
def get_queryset(self):
return Place.objects.order_by('id')[:]
EDIT
Maybe worth mentioning: it all did work when I just had a folder oldantwerp as a subdirectory in the oldicty project folder. This error only occurred after I started implementing it from an external package.
EDIT
These are my template settings in 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',
],
},
},
]
One most likely problem could be you have got the template name wrong in your view. Ensure you use the template oldantwerp/index.html in the view, not just index.html.
If you look at your stacktrace you will see that you application tries to load the template from: * django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist).
If I read your first statement correctly your directory structure is:
django-oldantwerp>oldantwerp>templates>oldantwerp>index.html.
So you can either create a directory structure proper for django to identify your templates directory or you can update your templates constant from the settings file and point django to a physical location where it can find your templates.
You can try to set DIRS in settings.py like this: os.path.join(BASE_DIR, 'templates'), delete the oldantwerp folder from your templates folder and add index.html in templates.
After that, edit template_name = 'oldantwerp/index.html' to template_name = 'index.html'
Related
Im learning programming and im completely new in it..i was working on a project and i have used django for back-end. Now the problem im currenctly facing is that i got no idea how should i link frontend and backend ?..
first we created backend (where there is login/signup/and dashboard) using django and boostrap,js .. and the backend work perfectly so below the folder structure of the backend
we are working on.
so this is the structure of the backend .. to be more clear check 2nd image.
Here you can see that, budgetwebsite folder which is just below the folder authentication.
budgetwebsite is our main thing or a part of our system..
then we did django startapp for authentication(for username validation and email)
then we did django startapp for userincome(here we worked on userincome like add/delete income)
then we did django startapp for expenses(here we worked on expenses like add/delete/expense)
and that userpreference is our admin panel.
thats for the backend section
Now lets move on the front end section aswell.
then we created a different folder name front end and we started working on it.
So now lets move to the problem... i just want to merge this front end and back end ..
below is my code of
setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'expenses',
'userpreferences',
'userincome',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'main')],
'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',
],
},
},
]
WSGI_APPLICATION = 'budgetwebsite.wsgi.application'
STATIC_URL = '/static/'
STATICFILES_DIRS=[os.path.join(BASE_DIR,'budgetwebsite/static')]
STATIC_ROOT = os.path.join(BASE_DIR,'static')
MESSAGE_TAGS ={
messages.ERROR : "danger"
}
urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('', include('expenses.urls')),
path('authentication/', include('authentication.urls')),
path('preferences/', include('userpreferences.urls')),
path('income/', include('userincome.urls')),
path('admin/', admin.site.urls),
]
now my question is do i again have to startapp which i did for like expenses , income and authentication or is there any way i can easly merge my front end with backend. If you didn't understood the question im sorry ..
and if the question is that appropriate then im sorry for that aswell.
Thanks.
Let's assume you want the user to get the expense app to open when he first visits your wesite
So the expense app will have it's own views.py and you have to create a urls.py for it and also all the other apps too.
now in the urls.py file of the expense you have to write the url patternss for that app
urls.py
from django.urls import path
from . import views
urlpatterns=[
path('expense', views.expense, name= 'expense'),
]
similarly all the urls you want to display under the expense app has to written here first connected to a view function
views.py
from django.shortcuts import render
def expense(request):
return render(request, 'expense.html') #or any other html file related to expese
all the pages related to the expense app will have different view function to render them and also url patterns connected to then so that the user can request
This is the most basic codes that you have to write to render any page related to the apps you want to display
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.
My Django structure is:
testing
contacts
__init__.py
templates
contacts
index.html
(additional modules)
testing
__init__.py
templates
testing
test.html
urls.py
(additional modules)
Inside of the main URL module, testing.urls, I have a urlconf that is as follows:
url(r'^testing/$', TemplateView.as_view(template_name='testing/test.html'))
The problem is, it keeps looking in contacts.templates.contacts for the test.html file. With the existing urlcon, the debug page says the following:
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/admin/templates/testing/test.html (File does not exist)
/Library/Python/2.7/site-packages/django/contrib/auth/templates/testing/test.html (File does not exist)
/Users/*/Developer/django/testing/contacts/templates/testing/test.html (File does not exist)
It always defaults to the..........................^^^^^^^^^^ contacts folder for some reason. Is there some other parameters for TemplateView or template_name that can control this? Any help appreciated!
Update - Inside settings.py
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',
],
},
},
]
The testing/testing directory is not currently being searched by Django. The easiest fix is to add it to the DIRS setting:
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
Another option would be to add testing to your INSTALLED_APPS setting. Then Django would find your template, since you have APP_DIRS=True. However I wouldn't recommend this, because in your case testing/testing is the special directory that contains the settings.py and root url config.
By specifying "APP_DIRS": True, you are telling django to search for template files inside each app installed. Check in settings.py whether all your apps are contained within INSTALLED_APPS. If they are, you can try to force django to look for the templates in your app.
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'testing', 'templates')],
....
},
]
This is driving me crazy. I've done something weird and it appears that my TEMPLATE_DIRS entries are being ignored. I have only one settings.py file, located in the project directory, and it contains:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'web_app/views/'),
)
I'm putting project-level templates in the /templates folder, and then have folders for different view categories in my app folder (e.g. authentication views, account views, etc.).
For example, my main index page view is in web_app/views/main/views_main.py and looks like
from web_app.views.view_classes import AuthenticatedView, AppView
class Index(AppView):
template_name = "main/templates/index.html"
where an AppView is just an extension of TemplateView. Here's my problem: when I try to visit the page, I get a TemplateDoesNotExist exception and the part that's really confusing me is the Template-Loader Postmortem:
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django\contrib\admin\templates\main\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\main\templates\index.html (File does not exist)
Why in the world are the 'templates' and 'web_app/views' directories not being searched? I've checked Settings via the debugger and a breakpoint in views_main.py and it looks like they're in there. Has anyone had a similar problem? Thanks.
What version of Django are you using? TEMPLATE_DIRSis deprecated since 1.8
Deprecated since version 1.8:
Set the DIRS option of a DjangoTemplates backend instead.
https://docs.djangoproject.com/en/1.8/ref/settings/#template-dirs
So try this instead:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Here's a link to an upgrade guide: https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/
I know there are many questions similar to this one, but none has solved my problem yet.
Python version: 3.4
Django version: 1.8
I get TemplateDoesNotExist at /lfstd/ when loading http://127.0.0.1:8000/lfstd/.
system.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
lfstd/views.py:
def index(request):
[...]
return render(request, 'lfstd/index.html', context_dict)
project urls.py:
from django.conf.urls import include, url
from django.contrib import admin
url(r'^admin/', include(admin.site.urls)),
url(r'^lfstd/', include('lfstd.urls')),
lfstd urls.py:
from django.conf.urls import patterns, url
from lfstd import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'))
Running print on base_dir and template_path, I get:
Base dir: C:\Users\Phil\PycharmProjects\TownBuddies
Template path: C:\Users\Phil\PycharmProjects\TownBuddies\templates
Which is exactly where my project and template folder is located. However, django doesn't look in that folder for templates. It looks in the following folders:
C:\Python34\lib\site-packages\django\contrib\admin\templates\lfstd\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\lfstd\index.html (File does not exist)
In fact, if I move my template there, it does find it and it works.
Any help is very appreciated, I'm starting out with Django and completely stuck...
EDIT:
I changed TEMPLATE_DIRS to TEMPLATES but Django still isn't looking in the templates folder:
TEMPLATE_DIRS setting is deprecated in django 1.8. You should use the TEMPLATES instead:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_PATH],
'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 was doing some unit testing with pytest-django when suddenly I was getting repeated and completely unexpected ** TemplateDoesNotExist** errors.
I found this stackoverflow question while trying to figure out what was going on.
I finally solved the issue by deleting all the __pycache__ directories. I have since learned that when things suddenly go inexplicably wonky to clear out the __pycache__ before breaking things by trying to fix them!
I used this script to clear out all the __pycache__ directories recursively where a module with the following resides:
#!/usr/bin/env python
import os
import shutil
#!/usr/bin/env python
import os
import shutil
BASE_DIR = os.path.abspath(__file__)
print(BASE_DIR)
for root, dirs, files in os.walk(BASE_DIR):
for directory in dirs:
if directory == '__pycache__':
shutil.rmtree(os.path.join(root, directory))