Let's assume I have the following project:
myproject/
myproject/
__init__.py
settings.py
urls.py
wsgi.py
templates/
base.html
app1/
__init__.py
admin.py
models.py
urls.py
views.py
templates/
base.html
index.html
I want to have a base template, which all other apps will use, in myproject/template/base.html.
Then, I want to have the app templates in app/templates.
One option would be writting in myproject/settings.py:
TEMPLATE_DIRS = (
"/dir/to/myproject/myproject/templates",
"/dir/to/myproject/app1/templates",
)
But, is this the best way to do it?
Django supports this automatically, via the app_directories loader that is installed by default. See the documentation.
Maybe you can do the following:
#settings.py
import os
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates'),
os.path.join(PROJECT_PATH, 'app1/templates'),
)
This will save you from editing each and every absolute path in setting file in case you happen to move your project.
Related
I have templates for example like home.html, about.html, etc. Which are "general."
Where should the views that render these views be located?
I am not convinced to place these views in the applications of my project, since each one has a very specific purpose. It occurs to me to create an application specifically for these "general" views, but what should this application be called? Is it good practice to do it?
Another solution would be to put the views in the urlconf, as follows:
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name = 'pages/home.html'), name = 'home'), # here
...
]
But is this alternative of good practice?
Is there a much better alternative?
Just put them in your global app. Let's suppose my Django project name is project_name and I have an app called tasks. Here is what I would do:
~/projects/projects_name/
project_name/ # project dir (the one which django-admin.py creates)
...
settings/ # settings for different environments, see below
__init__.py
...
views.py # Put here global views (home, etc.)
urls.py
wsgi.py
static/ # site-specific static files
templates/ # site-specific templates
project_name/
home.html
about.html
tests/ # site-specific tests (mostly in-browser ones)
...
tasks/
...
static/ # tasks app-specific static files
templates/ # tasks app-specific templates
When creating an app with python manage.py startapp myapp, it automatically creates an apps.py file.
from django.apps import AppConfig
class MyappConfig(AppConfig):
name = 'myapp'
When I removed it, everything seems to work as before (at least my tests all passed). Is it a bad practice to remove these kind of files from the apps? Should we keep them to avoid side effects?
The recommended approach in Django is to use the app config in your INSTALLED_APPS:
INSTALLED_APPS = [
'myapp.apps.MyappConfig',
...
]
If you do this, then the apps.py file is required.
However, if you do not customize the app config at all, then it is possible to remove the apps.py if you use the app name in INSTALLED_APPS instead:
INSTALLED_APPS = [
'myapp',
...
]
I want to be able to have dynamic directories where templates will reside. Let me explain what I mean. I have an application system wich has such a file structure:
\proj
__init__.py
settings.py
urls.py
...
\system
__init__.py
models.py
views.py
urls.py
\modules
\module_1
__init__.py
models.py
views.py
urls.py
\templates ## Attention
one.html
two.html
\module_2
__init__.py
modules.py
\templates ##
three.html
four.html
...
\module_N
...
As you can see there is a modules folder, which contains "atomic" modules, atomic in a sense that they have all necessary files, including templates in one place. So that module_1 has a template folder with its tempaltes, module_2 has a templates folder and all other modules have their own templates folder. What I want is to be able to refer to these templates folders in my settings.py file, so that when I upload a brand new module to modules folder, I would not have to change this settings.py file. So my question is, how can I dynamically build TEMPLATE_DIRS variable:
TEMPLATE_DIRS = (
## how to implement this???
)
EDIT
I'm considering another approach. First, to convert my modules to dinamic applications like this:
MODULES_DIR = 'system/modules'
for item in os.listdir(MODULES_DIR):
if os.path.isdir(os.path.join(MODULES_DIR, item)):
app_name = 'system.modules.%s' % item
INSTALLED_APPS += (app_name, )
And then to do something to make Django look for templates in all applications' folders. But I'm not sure whether it will work and how can I complete this task.
This is simply app-specific templates, which is already catered for by the APP_DIRS flag in the template settings dict. There shouldn't be any other configuration needed.
Below is the structure of my Django App:
Project/
static/
App1/
App2/
App3/
...
So I have a Django project, and I want to keep the HTML/CSS styling uniform across the apps in the project.
How does one go about extending a HTML template residing in the Project folder from each of these apps?
I have only been successful extending HTML templates within App folders for each respective App.
My project settings has static files set to be located in "/static/"
You can put your project-common templates in Project/templates and add the /path/to/Project/templates to your TEMPLATE_DIRS setting in your settings.py file. prepend the root to everything else, so it gets searched first:
TEMPLATE_DIRS = (
# Don't forget to use absolute paths, not relative paths.
"/path/to/Projects/templates",
#other dirs ...
)
Then you go extend it from your application templates as usual:
{# App1/templates/App1/template1.html #}
{% extends "template_in_project_root.html" %}
...
the TEMPLATE_DIRS setting
As this other SO post shows, my Django 1.4 directory structure globally looks like:
wsgi/
champis/
settings.py
settings_deployment.py
urls.py
site/
static/
css/
app.css
templates/some_app/foo.html
__init__.py
urls.py
views.py
models.py
manage.py
The project is champis, the app is site. My PYTHONPATH includes the wsgi folder (well from Django standards it should be named after the project i.e. champis, but here I'm starting from an Openshift django-example Git project).
My champis.urls:
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^champis/', include('site.urls')),
url(r'^admin/', include(admin.site.urls)),
)
My site.urls module then routes to specific pages, but when trying to access on local, I have the error:
http://127.0.0.1/champis => no module name site.urls
The site app is present in my INSTALLED_APPS, and my ROOT_URLCONF is champis.urls.
Do you have an idea why ? Even moving the site folder into the champis one didn't help.
I finally managed to solve this problem by:
adding an __init__.py at project level
renaming my site app into web (app name seemed to be colliding with... something that I did not find)
Here is my current directory structure now:
wsgi/
champis/
settings.py
settings_deployment.py
urls.py
web/ <= changed app name
static/
css/
app.css
templates/some_app/foo.html
__init__.py
urls.py
views.py
models.py
manage.py
__init__.py <= added