Django template extends doens`t open a file - python

I`m trying to extend one html file from other, but it simply shows me only base.html result.
Here is my project folders:
-diploma
-catalogue
-migrations
-templates
-catalogue
base.html
header.html
_init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
-diploma
__init__.py
settings.py
urls.py
wsgi.py
settings.py
...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
'catalogue',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
...
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',
],
},
},
]
urls.py
from django.conf.urls import url
from catalogue import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
views.py
from django.shortcuts import render
def index(request):
return render(request, 'catalogue/base.html')
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
header.html
{% extends 'catalogue/base.html' %}
{% block content %}
<h1>Hello</h1>
{% endblock %}
Really, trying to find the issue, but can`t figure out. Only effect cghanges made in base.html

Try putting:
{% extends 'base.html' %}
instead of the whole path

Related

CSS isn't loading in Django

I have a problem with my Django project which does not want to load static files. I tried a lot of solutions proposed here in the forum like:
settings.py -> setting the STATICFILES_DIRS (I had it from the beginning but was trying different things)
changing my URL paths in the HTML links tag
moving everything into one template instead of extending
adding app name in URLs
adding CSS to mime
moving load static to head
and many more out of which all failed.
Could anyone see what the problem in the code is? Thank you
My project has a structure like this:
my_site
blog
-- static/blog
-- templates/blog
-- standard Django files
my_site
-- standard Django files
templates
static
-- images
whole folder organization
BLOG:
urls.py:
from django.urls import path
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
app_name = "blog"
urlpatterns = [
path("index", views.index, name="index"),
path("posts", views.posts_list, name="posts_list"),
path("posts/<slug:slug>", views.post, name="post")
]
urlpatterns += staticfiles_urlpatterns()
views.py:
from django.http.response import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'blog/index.html')
def posts_list(request):
return render(request, "blog/posts_list.html")
def post(request):
return render(request, "blog/post.html")
templates/blog/index.html:
{% extends "base.html" %}
{% load static %}
{% block title %}
Yogiri
{% endblock %}
{% block css_files %}
<link ref="stylesheet" type="text/css" href="{% static 'blog/index.css' %}" />
{% endblock %}
{% block content %}
{% endblock %}
static/blog/index.css
header {
background-color: teal;
}
my_site
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog.apps.BlogConfig',
]
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',
],
},
},
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
templates
base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>
{% block title %}
{% endblock %}
</title>
{% load static %}
<link ref="stylesheet" type="text/css" href="{% static "base_style.css" %}">
{% block css_files %}
{% endblock %}
</head>
<body>
<header>
<img src="{% static "images/logo.png" %}" alt="Yogiri" width="200" height ="100">
Yogiri - Hatha Yoga
</header>
{% block content %}
{% endblock %}
</body>
</html>
static:
base_style.css
body {
background-color: teal;
}
This will not work: <link ref="stylesheet" type="text/css" href="{% static 'blog/index.css' %}" />
You have set the static path to [BASE_DIR / "static"]. This tells Django to look for static files in the static directory which is located in the root directory.
Place your blog/index.css in the root static directory(where you have put the images folder).
EDIT
Another option will be to include the directory in STATICFILES_DIRS.
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'blog/static')
]

Static file images Not found Django

I'm making a website and the static files are not loading,
when i try to accsess static files with localhost:8000/static/image.png it shows this:
Page Not found (404)'image.png' could not be found
and the index page is showing the image that displays when the image couldn't be found
files:
VoicesOnTheSpectrum
VoicesOnTheSpectrum
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
vots
migrations
static
image.png
logo.png
style.css
votstop.png
templates
vots
base.html
index.html
__init__.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
manage.py
settings.py:
from pathlib import Path
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, "vots", "templates")
SECRET_KEY = 'secret'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'VoicesOnTheSpectrum.urls'
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 = 'VoicesOnTheSpectrum.wsgi.application'
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static')]
# STATIC_ROOT = os.path.join(BASE_DIR, "static")
index:
{% extends "vots/base.html" %}
{% load static %}
{% block content %}
<div role="img">
<img src="{% static 'votstop.png' %}">
</div>
{% endblock %}
base:
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}{% endblock %}Voices On The Spectrum</title>
<link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">
</head>
<body>
<div class="navbar">
</div>
<div class="body">
{% block content %}
{% endblock %}
</div>
<div class="footer">
<p>Copyright © {% now "Y" %} Voices on the Spectrum - All Rights Reserved.</p>
<div class="insta">
<img src="{% static 'image.png' %}" alt="Instagram">
</div>
</div>
</body>
</html>
You need to add the view(s) to your urlpatterns to serve static (and media) files:
# VoicesOnTheSpectrum/urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# … other urls patterns …
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
My Answer
just needed to add my app to installed apps
INSTALLED_APPS = [
'vots',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

Why will the css in my django project not render?

I'm still just playing around with Django and cannot figure out what I am doing wrong with my CSS file. My HTML works but not the CSS. I'm guessing it has something to do with settings.py or some special "Django code" I'm not using. Here is the code for my HTML (what I am guessing is the relevant code to show).
Here is my HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>home</title>
<link rel="stylesheet" type="text/css" href="../css/styles.css"/>
</head>
<body>
<h3>HELLO WORLD!</h3>
</body>
</html>
Here is my 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',
],
},
},
]
WSGI_APPLICATION = 'ecommerce005.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Here is my urls.py:
from django.conf.urls import url
from django.contrib import admin
from .views import home_page
urlpatterns = [
url(r'^$', home_page),
url(r'^admin/', admin.site.urls),
]
Here is my views.py:
from django.http import HttpResponse
from django.shortcuts import render
def home_page(request):
return render(request, "index.html", {})
The order of my file are as follows:
'src' directory:
1) 'css' folder holds my styles.css file
2) 'projectname' folder contains all of my django files shown above
3) 'templates' folder holds my index.html file
Please be as specific as possible. I found sources online but they seemed too vague for me to find a solution. I appreciate your help.
Configure static files in settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS=[
(os.path.dirname(BASE_DIR),"static"),
]
STATIC_ROOT = (os.path.dirname(BASE_DIR),"static_files")
In Templates,(ie HTML file)
{% load static %}
<html>
....
<link href="{% static 'styles/style.css' %}" rel="stylesheet" media="screen, print" type="text/css">
Directory Structure
-ProjectFolder
--|static
----|styles
------|styles.css
You need to setup a 'STATIC_ROOT' in the settings.py
STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
If DEBUG=False, you need to setup your proxy server such as nginx to serve these staticfiles
This link can explain it further.
Wootton, as was said, you need to setup your static_root on settings.py, after that you can just call your css
<link type="text/css" rel="stylesheet" href="{{ STATIC_URL }}css/style-responsive.css">
but before/after setup the static_root you need to create a folder named static and serve your css in there.
I think this will help you!

TemplateDoesNotExist - Creating a simple view

I have the following tree structure:
project/
__init__.py
app/
__init__.py
templates/
home.html
base.html
settings.py
urls.py
views.py
wsgi.py
views.py
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.template.loader import get_template
def home(request):
context = RequestContext(request,
{'user': request.user})
return render_to_response('templates/home.html',
context_instance=context)
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', 'app.views.home', name='home'),
]
relevant parts of settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'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',
],
},
},
]
Problem: When I run:
./manage runserver
Navigate to 127.0.0.1:8000
It raises a django.template.exceptions.TemplateDoesNotExist: templates/home.html
I've tried different variations of the path as well as hardcoding the directory into the TEMPLATES dictionary in my settings file. What am I missing?
This setup was fine. The problem was I was extending base.html in home.html
Like: {% extends 'app/base.html' %} when it needed to be {% extends 'base.html' %}

static url path setting in django

I am new to django, presently I am learning and trying to build a basic site with django-cms. Below are my project folder structure and code files
project_folder
manage.py
main_folder
static
css
new-styles.css
js
new-styles.js
templates
base.html
home.html
media
pic_1.gif
.....
settings.py
urls.py
.......
settings.py
import os
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
TEMPLATE_DIRS = (os.path.join(PROJECT_DIR,'templates'))
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.debug',
'django.contrib.messages.context_processors.messages',
'cms.context_processors.media',
'sekizai.context_processors.sekizai',
)
......
.......
urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', ...........),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns
base.html
{% load cms_tags sekizai_tags menu_tags %}
<html>
<head>
<title>{%block "title" %}{% endblock %}</title>
<link rel="stylesheet" href="{{ STATIC_URL }}css/new-styles.css">
{% render_block "css" %}
</head>
<body>
{% cms_toolbar %}
{% block "base_content" %}
{% endblock %}
{% render_block "js" %}
</body>
</html>
home.html
{% extends "base.html" %}
{% load cms_tags %}
{% block "title" %}Welcome to Services{% endblock %}
{% block "base_content" %}
<p>Hi this is paragraph</p>
<img src="{{ MEDIA_URL }}images/promo3.jpg" />
{% placeholder "number_one" %}
{% endblock %}
new-styles.css
body {
padding:0px; margin:0px; margin-top:40px;
background-color:#b0c4de;
}
p {
background-color:#e0ffff;
}
So above are my complete project structure and code files, but my actual problem is new-styles.css file is not working, even though I had written some css code in the file it is not linking to the template, so can anyone please let me know whats wrong and why base.html template file is unable to access the new-styles.css file, whether the path given in link tag is wrong or the path setting in the settings.py is wrong?
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from project_name import settings
admin.autodiscover()
urlpatterns = patterns('',
.....................
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()

Categories

Resources