templets not found error in django - django

I am little bit confused about django templates, I have attached screen shot of workspace, can some one please tell how can I use addprofile.html template in my views.py in buddy app.
In my settings/base.py I have mentioned templates in following way:
# Build paths inside the project like this: join(BASE_DIR, "directory")
BASE_DIR = dirname(dirname(dirname(__file__)))
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATICFILES_DIRS = [join(BASE_DIR, 'static')]
MEDIA_ROOT = join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
# Use Django templates using the new Django 1.8 TEMPLATES settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
join(BASE_DIR, 'templates'),
# insert more TEMPLATE_DIRS here
join(PROJECT_PATH, 'templates/')
],
'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',
],
},
},
]
I am new to Django and python and this is my first app in django

create following structure:
buddy (this is your existing budy app folder)/templates (this is existing folder)/buddy (this is new folder) and place all your templates: addprofile.html, edit_profile.html, show_profile.html, success.html withing this path.
edit your settings 'DIRS' so it looks as follows:
'DIRS': [
join(BASE_DIR, 'templates'),
join(PROJECT_PATH, 'templates/'),
join(BASE_DIR, 'buddy/templates')

Related

Customizing Django login template

I'm new to Django and I want to use my custom login page instead of the ugly looking django login template. How could I make it work? I think I need to edit the urls.py file and override the login template but I don't know how to do it. Thanks
urls.py
from django.urls import path
from django.contrib.auth.views import (
LoginView,
)
urlpatterns = [
path('login/', LoginView.as_view(template_name='login/login.html'), name='login'),
]
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',
],
},
},
]
LOGIN_URL = '/login/'
Now make a templates folder in the Base directory( where your manage.py is present) and in that folder make a new folder named login. Inside login folder make login.html
So the directory structure will be like this
-my_project
-templates
-login
-login.html
-manage.py
Now do whatever customization you want to do in login.html. You will get form object in context of the template, which is nothing but your login form.

Using django-environ; Django admin css not found

I removed the BASE_DIR and the os import because I am using django-environ.
However when I entered the admin dashboard of Django, the css is not read so I only see plain html elements.
Here is how part of my settings.py looks like.
import environ
root = environ.Path(__file__) - 3 # get root of the project
env = environ.Env()
environ.Env.read_env() # reading .env file
SITE_ROOT = root()
DEBUG = env.bool('DEBUG', default=False)
TEMPLATE_DEBUG = DEBUG
ALLOWED_HOSTS = tuple(env.str('ALLOWED_HOSTS', default=[]))
DATABASES = {
'default': env.db('DATABASE_URL')
}
SECRET_KEY = env.str('SECRET_KEY')
# Static and Media Config
public_root = root.path('public/')
MEDIA_ROOT = public_root('media')
# MEDIA_ROOT = root.path('media/')
MEDIA_URL = env.str('MEDIA_URL', default='media/')
STATIC_ROOT = public_root('static')
STATIC_URL = env.str('STATIC_URL', default='static/')
ROOT_URLCONF = 'myproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [root.path('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',
],
},
},
]
So I also put in root.path('templates') into the DIRS of TEMPLATES.
I also added this snippet below to the project's urls.py.
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
I also have this on my .env file:
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
MEDIA_URL=media/
STATIC_URL=static/
Where could I have gone wrong and what should be written instead?
I looked into djangogirls tutorial and found out that I missed out some slashes.
From my .env file:
MEDIA_URL=media/
STATIC_URL=static/
I changed it into
MEDIA_URL=/media/
STATIC_URL=/static/

Can't load static files Django 1.9

Template cant find static files and load it on page.
This is my settings:
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,'cargo/templates')
# STATIC_DIR = os.path.join(BASE_DIR,'cargo/static')
DEBUG = True
ALLOWED_HOSTS = []
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',
],
},
},
]
WSGI_APPLICATION = 'mysite.wsgi.application'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = 'os.path.join(os.path.expanduser("~"), "domains/h2h.su/static/")'
This my path to files cargo/static/cargo/images and templates at the same path in cargo app cargo/templates/cargo/index.html
Can someone help me?
I've fixed the problem by changing os.path.join(BASE_DIR, "static"),
to os.path.join(BASE_DIR, "cargo"),

Add current site's domain to template dirs

What is the correct way to prepend an extra, site-specific templates directory to the template dirs setting in Django?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
<PREPEND SITE-BASED TEMPLATE DIR IN HERE>
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'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',
],
},
},
]
I obviously can't use Site.objects.get_current() in there.
I could write my own template loader which uses it but is that "correct"?
Or should I be using an extended version of the settings files or something?
First of all, start by separating the settings. You should have a settings hierarchy that goes like:
base_settings.py
siteA_settings.py
siteB_settings.py
On base_settings.py you should put all the settings that are project-wide.
siteA_settings.py (which is a child) will override the base_settings.
To make it site-specific you have to put on each setting file the SITE_ID setting. Example of siteA_settings.py
# Sites Framework
SITE_ID = 1
SITE_TITLE = 'First Site'
Then load the TEMPLATES settings. They can be different on each setting file. Example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
... , etc.
Just like TEMPLATES, you can choose to have different settings for each Site (apps, email settings etc)
Then, when you run the server, you should specify which setting (site) you want to run. Example:
python manage.py runserver --settings=myproject.siteA_settings
where myproject is the app where your settings are located.
Do not forget that you have to put the --settings argument everytime you run a command (including makemigrations, migrate, etc etc)
Usually, on my projects, I create a core app, a settings directory and inside it, I put base_settings.py, dev.py, local.py, production.py etc. When running the Django Development Server, I then run the command:
python manage.py runserver --settings=core.settings.local

Overriding Admin Templates in django 1.11

I don't get django 1.11 to accept a new base_site.html:
Following docs and some posts i copied the file to:
BASE_DIR/templates/base_site.html and
BASE_DIR/templates/admin/base_site.html
in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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',
],
'loaders': (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
),
},
},
]
I tried that with the file in templates and in templates/admin:
>>> print ( os.path.isfile((os.path.join(settings.BASE_DIR, 'templates', 'admin','base_site.html'))) )
True
>>> print ( os.path.isfile((os.path.join(settings.BASE_DIR, 'templates', 'base_site.html'))) )
True
If you want to override admin files they must exist in admin folder inside templates folder like below
your_project
your_project
|-- your_project/
|-- myapp/
|-- templates/
|-- admin/
|-- base_site.html
here file name base_site.html must be same as the file name in original admin folder.
base_site.html
{% extends "admin/base.html" %}
# you can write changes here
Just had this problem and literally fixed it by installing django-apptemplates and adding it as a loader into settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [PROJECT_DIR],
'APP_DIRS': False,
'OPTIONS': {
'loaders': [
# ADD LOADER HERE
'apptemplates.Loader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
**APP_DIRS will have to be set to False in order for this to work
To override the base_site.html of Django admin, add the custom base_site.html such that it's path is BASE_DIR/templates/admin/base_site.html. This works in Django 1.11 as well.