Image does not appear Django template - python

I am trying to load some images into a template but it continues to appear as broken link
My files and directories are as follows
1.views.py
def home(request):
#template = loader.get_template("polls/home.html")
return render_to_response('polls/home.html',
context_instance=RequestContext(request))
2.urls.py
urlpatterns = patterns('',
#url(r'^applications/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
(r'^', include('polls.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
3.home.html
{% load staticfiles %}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>psdtowebSimpleWebsiteTemplate.psd</title>
<link href="{% static 'C:\Users\omars_000\Desktop\mytask\polls\templates\polls\styles.css' %}" rel="stylesheet" type="text/css">
</head>
<body>
<div id="background">
<div id="background"><img src="{{MEDIA_URL}}/Capture.png"></div>
</body>
</html>
4.settings.py
MEDIA_ROOT = os.path.join( PROJECT_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, "static"),
'/var/www/static/',
)
Any idea why it is not working? I have checked many questions but nothing helped so far.

Your MEDIA_URL already has a trailing slash. You are adding another in <img src="{{MEDIA_URL}}/Capture.png"> Most browsers don't care, but perhaps yours does.

Got it.
In home.html I fixed to be as follows:
<div id="background"><img src="{% static "C:\Users\omars_000\Desktop\mytask\mytask\media\images\background.png" %}""></div>
and in settings.py I added the location of the images i will be using in the template as follows:
STATICFILES_DIRS = (
'C:\Users\omars_000\Desktop\mytask\mytask\media\images',
'C:\Users\omars_000\Desktop\mytask\mytask\media',
)

Related

how to use static in django project

Is this not the correct way of making use of Django's static directories in code?
The folder is static/assets/css/style.css
settings.py
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
urls.py
from django.conf.urls.static import static
from django.conf import settings
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
base.html
{% load static %}
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="{% static 'assets/css/style.css' %}">
</head>
<body>
Go somewhere
</body>
</html>
style.css
a{
text-decoration: none;
}

Django templates not picking static files

Following is the my static folder structure:
ProjectABC
- App1
- App2
- App3
- ProjectABC
- resources
- static
- imgs
- css
- templates
This is how the project structure looks like.
This is how the static configuration in settings.py looks like:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
This is how the base.html looks like:
<!doctype html>
{% load static %}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, maximum-scale=1, initial-
scale=1, user-scalable=0">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?
family=Open+Sans:400,600,800">
<title>{{SITE_NAME}}</title>
<link rel='shortcut icon' href= "{% static 'img/favicon.png' %}"/>
<link href="{{STATIC_URL}}css/application.css" media="screen" rel="stylesheet"
type="text/css" />
<link href="{{STATIC_URL}}css/style.css" media="screen" rel="stylesheet"
type="text/css" />
<script src="{{STATIC_URL}}js/application.js" type="text/javascript"></script>
{% block extracss %}
{% endblock %}
</head>
<body>
<div class="container">
<h1>Hello</h1>
</div>
</body>
</html>
All the static files are being loaded as seen in terminal, but in browser its just loading bare html without any css or img:
Please lemme know if i am missing something.
Thnk You!
<link href="{%static 'css/style.css' %}" media="screen" rel="stylesheet"
type="text/css" />
add settings.py
STATIC_URL = "/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
Project App Url
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Static directory must be under the app itself. Create a 'static' directory directly to your app and for better practice, you can also create another directory to the static directory with your app name (static/[app_name]).
Also, {% load static %} must be above doctype in your base.html
change the static config in your settings.py to this:
STATIC_ROOT = os.path.join(BASE_DIR, 'resources/static')
STATIC_URL = 'resources/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'ProjectABC')
]
then run python manage.py collectstatic
and when you call the static file do it like this {% static 'css/style.css' %}

Django static files does not load while in the root directory

I stored my static files in project root directory but I can't make them load on the browser.
configurations
settings.py
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIR = [
STATIC_DIR,
]
template file
<!DOCTYPE html>
{% load static %}
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Index</title>
</head>
<body>
<h1>{{temp}}</h1>
<h2>Click here to veiw user details</h2>
<img src="{% static "user/images/smile.png"%}" alt="oh oh didn't show"/>
</body>
</html>
In the file urls.py add the static url.
urlpatterns = [
path('admin/', admin.site.urls),
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Static files are not loading in Django 1.8 templates

I am using Django 1.8 for my project but the problem is my static files are not loading into the template. I have followed each steps in detail, but still don't know why it is not loading.
Template file
{% load staticfiles %}
<!DOCTYPE html>
<html>
<!-- This code is only meant for previewing your Reflow design. -->
<head>
<link rel="stylesheet" href='{% static "css/boilerplate.css" %}'>
<link rel="stylesheet" href='{% static "css/index.css" %}'>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0">
</head>
<body>
Settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'staticfiles')
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'media')
STATICFILES_DIR = (
os.path.join(BASE_DIR, 'static'),
)
Project Directory
Could anyone tell me why the static files are not loading? The page source in browser is accurately showing the file path. When i click on it, it is showing me the css content but it is not loading when I run the server.
Page Source:
<!DOCTYPE html>
<html>
<!-- This code is only meant for previewing your Reflow design. -->
<head>
<link rel="stylesheet" href='/static/css/boilerplate.css'>
<link rel="stylesheet" href='/static/css/index.css'>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0">
</head>
<body>
urls.py file
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', AboutView.as_view(), name='index-template'),
url(r'^register/', RegistrationListView.as_view(), name='register'),
url(r'^form/', RegistrationForm.as_view(), name=''),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Inspect Element Result:
You are missing an 'S' in the spelling of STATICFILES_DIRS.
Once you correct that, do a python manage.py collectstatic and restart the dev server.

TemplateDoesNotExist. loading static files and templates Django project structure

I am a bit confused on how to yield templates from other apps
Here is a picture of my project structure:
I am getting this error:
TemplateDoesNotExist at /
home.html
home/views.py:
class HomeView(generic.TemplateView):
template_name = "home.html"
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "home/static/home/js"),
os.path.join(BASE_DIR, "home/static/home/images"),
os.path.join(BASE_DIR, "home/static/home/css"),
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
project/home/urls.py:
from django.conf.urls import patterns, url
from home.views import HomeView
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"),
)
project/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include("home.urls", namespace="home")),
)
project/templates/home/index.html
<!DOCTYPE html>
<html>
<head lang="en">
...
<!-- Fonts -->
<link href='http://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
<!-- Custom CSS -->
<link href="{{ STATIC_URL }}boothie.css" rel="stylesheet">
<!-- Custom JS-->
<script src="{{ STATIC_URL }}boothie.js"></script>
<title>Boothie</title>
</head>
<body>
<div class="container">
...
</div>
</head>
{% block content %}{% endblock %}
</body>
</html>
project/home/templates/home/home.html:
{% extends index.html %}
{% block content %}
...
<script src="{{ STATIC_URL }}js/jquery.easing.1.3.js"></script>
<script src="{{ STATIC_URL }}js/boothie.js"></script>
{% endblock %}
Django stops at the first template that it finds matching the requested filename, and it will start at the templates directory in the app folder.
If it doesn't find it here, it will then go up the chain till it has searched all locations in the TEMPLATE_DIRS setting, and only then will it print an error that it cannot find a template. The way it searches for templates is defined by the TEMPLATE_LOADERS setting.
In your project layout, the template you want is located in the template directory of the home app, and inside this directory, under the home directory. Thus the template path should be home/home.html in your view:
class HomeView(generic.TemplateView):
template_name = "home/home.html"
If your templates directory is in your project's root you have to add it to setitngs.py
If it's the subdirectory of an app (that is in INSTALLED_APPS in settings.py) then the app template loader (enabled by default) will load it.
If your app is called home then you would have the templates dir inside the home dir, not home under templates which would make it accessible as home/home.html since it's under the home subfolder of the templates dir in settings.py

Categories

Resources