I'm trying to deploy django project to webfaction and stuck with the problem that the server does not see my templates folder
PROJECT_DIR = os.path.dirname((os.path.dirname((os.path.dirname(__file__)))))
TEMPLATE_DIRS = (
os.path.join(PROJECT_DIR,'templates'),
)
python path in httpd.conf :
python-path=/home/wadadaaa/webapps/promo_site/myproject:/home/wadadaaa/webapps/promo_site/lib/python2.7
And i have exception:
Exception Type: TemplateDoesNotExist
Exception Value: index.html
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/wadadaaa/webapps/promo_site/templates/index.html (File does not exist)
any ideas how to fix it?
When deploying to WebFaction I add the project's parent directory to the python_path:
python-path=/home/wadadaaa/webapps/promo_site:/home/wadadaaa/webapps/promo_site/myproject:/home/wadadaaa/webapps/promo_site/lib/python2.7
If you're using Django 1.5.x, a common way I "map" directory paths like TEMPLATE_DIRS, STATIC_ROOT, etc, is to use a function to compute those. This is especially useful if you work on more than one machine, or in a group, where the path to these files is going to vary per-developer:
# settings.py
import os
def map_path(directory_name):
return os.path.join(os.path.dirname(__file__),
'../' + directory_name).replace('\\', '/')
...
TEMPLATE_DIRS = (
map_path('templates'),
)
This is a convenient way to map your templates directory, given a project structure of:
my_app/
my_app/
__init__.py
settings.py
...
a_module/
__init__.py
models.py
templates/
layouts/
base.html
index.html
...
Related
I'm new to Django, trying my hand at creating a web app based on the self learn tutorial. i have created a app with name "travello" and project name is "travellproject", in the views i'am trying to render a html page.
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def homepage(request):
return render(request,'homepage.html')
i have created the "templates" directory under travellproj (please refer the directory structure below) also defined DIRS of template variable as below and urls.py as below.
"DIRS": [os.path.join(BASE_DIR,'templates')],
urlpatterns = [
path("",views.homepage),
path("admin/", admin.site.urls),
]
But I'm receiving a TemplateDoesNotExist error, please help.
Error
TemplateDoesNotExist at /
homepage.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 4.1.5
Exception Type: TemplateDoesNotExist
Exception Value:
homepage.html
Exception Location: C:\Users\Django\first_proj\lib\site-packages\django\template\loader.py, line 19, in get_template
Raised during: travello.views.homepage
Directory structure:
travello
|-views.py
travellProj
|-templates -- > homepage.html
|-urls.py
|-setting.py
Your templates folder should be at the root of your site. Let see how it should look like:
Project_folder
|-travelproj <-- Your site configuration folder
|-|-urls.py
|-|-wsgi.py
|-|-asgi.py
|-|-.......
|-travello <-- this is an application
|-|-views.py
|-|-urls.py
|-|-templates <-- templates for this app only
|-|-|-travello
|-|-|-|-template1.html
|-|-|-|-.......
|-|-.......
|-templates <- This is the templates folder for your site
|-|-homepage.html
|-|-..........
|-manage.py
|-requirements.txt
So in your case your templates folder is in your site configuration folder which is not a good practice. you have to move it one step higher.
Good practices tips:
At the root of your site (same level than manage.py) you have a templates folder which will contain the templates commons to all your applications.
In each application you have a folder templates/app_name which contain all the templates specifics for this application.
You have the same architecture for statics.
Inside templates create another new directory by your app name travello then keep the html file inside the travello directory
travello
| templates/travello/homepage.html
|-views.py
travellProj
|-urls.py
|-setting.py
Then views should be like
def homepage(request):
return render(request,'travello/homepage.html')
Was looking for a Python package for Django to manage assets, using Sass to compile CSS, and also cache busting, and Django-Assets / Webassets was recommended. Having trouble getting it setup though with my directory structure.
By default it looks for assets.py in each installed app. I want to set it up so that it sits in the same directory as settings.py and compiles app specific assets from each app directory into /static/js and /static/css.
I have django_assets in INSTALLED_APPS. According to the docs it looks like I needed to add this to settings.py:
ASSETS_MODULES = [
'project_dir',
]
Or:
ASSETS_MODULES = [
os.path.join(BASE_DIR, 'project_dir'),
]
Or:
ASSETS_MODULES = [
PROJECT_ROOT
]
At any rate, it just returns No asset bundles were found. If you are defining assets directly within your template, you want to use the --parse-templates option.
Even moving the assets.py into one of the app directories, it is looking in BASE_DIR/scripts which is where I keep my manage.py. Again, changing ASSETS_ROOT doesn't really same to be doing anything.
~/portal-client
project_dir
apps
account
templates
account
login.html
forms.py
urls.py
views.py
home
templates
home
home.html
urls.py
views.py
results
assets.py
settings.py
urls.py
scripts
manage.py
static
templates
base.html
footer.html
title.html
Couple of quick notes, for ASSETS_MODULES, the key word is additional:
django-assets will automatically look for assets.py files in each application, where you can register your bundles. If you want additional modules to be loaded, you can define this setting. It expects a list of importable modules:
i.e., we only use ASSETS_MODULES if we have assets.py files outside of any application. In your case, we will only specify this if project_dir is not an INSTALLED_APP.
Second, when using ASSETS_MODULES, you specify a dotted module path, not a directory. In your case, this would be project_dir.assets only if project_dir is not already part of INSTALLED_APPS.
Now, I have a Django web site which has two projects. One is root project and another is a app.
The directory structure is below:
--root project
--static
--templates
--index.html
--app
--static
--templates
--index.html
The relative settings in setting.py is below:
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__).decode('utf-8')).replace('\\', '/')
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, "static"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
And, when I want to specify a path to "/app/static/templates/index.html", I always get index.html in root.If I change the turn in STATICFILES_FINDERS, I will face the same problem when I want to get index.html in root.
How can I accurately get one of them?
Your directory structure seems strange...
First thing, in case the index.html in the app directory is supposed to be a Django-template, it shouldn't be under the static directory.
Also, you mentioned that you used the path /app/static/templates/index.html, which actually shouldn't work at all.
Usually in Django, the /static/ path will be used to access static resources from all apps' static directories, as well as all directories specified in STATICFILES_DIRS, as if all the content from all those directories is "merged" into one /static/ directory!
So, in your example, the path /static/templates/index.html indeed refers to both the index.html from the root project directory, as well as the index.html from the app-specific static directory, which is why the actual file you get will depend on the order of static files finders specified.
The recommended layout to avoid such collisions would be:
-project root
-static
-global static resources accessible via /static/...
-app
-static
-app
-app-specific static resources accessible via /static/app/...
This is also appropriate for app-template directories:
-app1
-templates
-app1
-index.html (referred from Django view as 'app1/index.html')
-app2
-templates
-app2
-index.html (referred from Django view as 'app2/index.html')
Edit to add info on shared templates:
If you're trying to have a "base-template" that is extended by other apps, I would recommend using the "common-app" approach.
You simply create a new app (named e.g. "common", although you can name it however you want), that contains common templates (and other logic, if you'd like), and have the app-specific templates extend it.
The layout would be:
-app1
-templates
-app1
-index.html
-common
-templates
-common
-base.html
And in index.html you'll have {% extends "common/base.html" %} at the top of the file (read the Django docs on template inheritance if you're unfamiliar with it).
Of course, the common app must be enabled in the Django settings for this to work.
Originally I had just one app in my Django project, the templates consisted of an index.html a detail.html and a layout.html ... the index and detail files extended the layout. They all lived in the same directory ([app1]/templates/[app1]/) and it could find all the files fine.
Now I've got a second app, and I want to re-use the layout.html ... I decided to make a templates dir off the base of the django project, and put it in there. Here's what my directory structure looks like now:
<basepath>/<django_project>/templates/shared
<basepath>/<django_project>/<app1>/templates/<app1>
<basepath>/<django_project>/<app2>/templates/<app2>
I updated my settings.py:
TEMPLATE_DIRS = (
'/full/path/to/<django_project>/<app1>/templates',
'/full/path/to/<django_project>/<app2>/templates',
'/full/path/to/<django_project>/templates',
)
In the 'shared' dir I have the layout.html ... from the app1 or app2 template dirs, I have the index.html and the 'extends' line at the top of the file reads:
{% extends "shared/layout.html" %}
However, when I try to load the apps view, it gets an error that it can't find shared/layout.html
TemplateSyntaxError at /
Caught TemplateDoesNotExist while rendering: shared/layout.html
Any ideas what I'm missing? This should be fairly straightforward, I must be overlooking something really obvious?
My bad! I thought I was running this behind Nginx, so I restarted that and was still having the problems. After re-checking I realized I was running behind Apache, restarting Apache updated my TEMPLATE_DIRS and now it works!
My Django templates use a lot of related stuff: images, style sheets, etc.
Where should I put these file, or how should I refer to them in the template itself?
For now I'm using the development server.
I know it's a really common thing, but I can't really figure it out.
I put them inside a folder named static, which is in the web project's top level folder.
Example:
/static/img/
/static/js/
/static/css/
/templates/
urls.py
settings.py
I then have the following rule in my urls.py file:
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
My settings.py contains:
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static').replace('\\', '/')
ADMIN_MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static/admin').replace('\\', '/')
Maybe you can read the doc http://docs.djangoproject.com/en/dev/howto/static-files/#howto-static-files
We put ours under /media. Everything that is specifically tied to the layout of the sites is further separated. Of course none of this static content is served by Django on the production site. They often aren't even on the same physical server.
/media
/images - this is for content-specific images
/video - these next 2 are normally symlinks to a /big_content folder ...
/audio - so that they aren't included in our mercurial repository.
/layout - everything that is tied to the basic templates.
/css
/js
/images