please help sort out.
my problem is that it is impossible to add django1.6 css-file in html-template. my project directory structure is as follows:
proj1(catalog)
manage.py(file)
proj1(catalog)
wsgi.py(file)
urls.py(file)
settings.py(file)
__init__.py(file)
views(catalog)
templates(catalog)
index.html(file)
static(catalog)
css(catalog)
styles.css(file)
urls.py:
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import proj1.views.views
urlpatterns = patterns('',
url('^$', proj1.views.views.hello),
url('^datetime$', proj1.views.views.current_datetime),
url('^dt$', proj1.views.views.current_datetime),
url('^dt/(\d{0,2})$', proj1.views.views.current_datetime2),
url('^dynamic$', proj1.views.views.dynamic),
)
urlpatterns += staticfiles_urlpatterns()
in settings.py prescribed the following (excerpt):
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'proj1/templates/'),
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
index.html following content:
<!DOCTYPE html>
{% load static %}
<html>
<head>
<meta charset="utf-8" />
<title>qwerty</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}" />
</head>
<body>
<div class="wrap">
<span class='text'>hello,</span> <span class='name'>{{name}}</span>
</div>
</body>
</html>
as a result of all this themselves for certain pages appear url (html true), but does not connect css-
ps
windows7
Change your BASE_DIR as the following
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
then use href as
href="/static/css/style.css"
Related
While creating a web app in Django, I've encountered a small issue. There seems to be a problem with my about template referencing. This is about.html:
<!DOCTYPE html>
{% load staticfiles %} <!-- New line -->
{% load static %}
<html> <head>
<title>Rango</title> </head>
<body>
<h1>Rango says...</h1>
<div>
here is the about page. <br />
<strong>This tutorial has been put together by Oliver Alan Stafurik</strong><br />
</div>
<div>
Index<br />
<img src="{% static 'images/rango.jpg' %}"alt="Picture of Rango" /> <!-- New line -->
<img src="{% static 'cat.jpg %'}" alt="Picture of a Cat" />
</div> </body>
</html>
Here's my settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIR = os.path.join(BASE_DIR, 'static')
MEDIA_DIR = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = [STATIC_DIR,]
STATIC_URL = '/static/'
MEDIA_ROOT = [MEDIA_DIR, ]
MEDIA_URL = '/media/'
And here's my urls.py:
from django.contrib import admin
from django.urls import path
from django.urls import include
from django.conf import settings
from django.conf.urls.static import static
from rango import views
urlpatterns = [
path('', views.index, name='index'),
path('rango/', include('rango.urls')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I'm getting a traceback: File "/Users/stovifo/tango_with_django_project/tango_with_django_project/rango/tests_chapter4.py", line 254, in test_about_contains_rango self.assertTrue(required_str in self.about_response.content.decode(), f"{FAILURE_HEADER}The HTML markup to include the image of Rango in the about template was not found. It needs to match exactly what we are looking for. Check the book.{FAILURE_FOOTER}") AssertionError: False is not true :
EDIT:
The issue was just a small typo, where instead of "{% static 'cat.jpg'%}" I have typed in "{% static 'cat.jpg %'}".
try this you have just forgot to put 'cat.jpg' :
<!DOCTYPE html>
{% load staticfiles %} <!-- New line -->
{% load static %}
<html> <head>
<title>Rango</title> </head>
<body>
<h1>Rango says...</h1>
<div>
here is the about page. <br />
<strong>This tutorial has been put together by Oliver Alan Stafurik</strong><br />
</div>
<div>
Index<br />
<img src="{% static 'images/rango.jpg' %}"alt="Picture of Rango" />
<img src="{% static 'cat.jpg' %}" alt="Picture of a Cat" /> <!-- you make a mistake here -->
</div> </body>
</html>
I'm having issues getting an image to load on my Django web application. I have used the online documentation and tried peoples suggestions that I found on Stackoverflow but I am still not having any luck getting it to load. Here is what I have:
Settings.py:
STATIC_DIR = os.path.join(BASE_DIR,'static')
INSTALLED_APPS = [
...
'django.contrib.staticfiles'
]
STATIC_URL = '/static/'
STATIC_ROOT = [STATIC_DIR,]
Urls.py:
from django.urls import path
from dir_app import views
from django.conf import settings
from django.conf.urls.static import static
app_name = 'dir_app'
urlpatterns=[
path('', views.tradesmen, name='tradesmen'),
path('register', views.register,name='register'),
path('user_login', views.user_login,name='user_login')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Index.html: (Gist)
<!DOCTYPE html>
{% load static %}
<html>
<head>
</head>
<body>
<img src="{% static 'dir_app/tools.png' %}">
</body>
</html>
Here is where the image is stored:
DEBUG should be true if want to use django.conf.url.static
https://github.com/django/django/blob/926148ef019abcac3a9988c78734d9336d69f24e/django/conf/urls/static.py#L23
STATIC_ROOT should be a string, not a list.
https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-STATIC_ROOT
I am learning Django and while I was trying to load static CSS files, it did not show any output. When I run server I only get HTML as my output.
Here is my code
settings.py code
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
'bloodnepal/bloodnepal/static',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
{% load static %}
<meta charset="utf-8">
<title>Bloodnepal</title>
<link rel="stylesheet" href="{% static 'css_files/style.css' %}">
<link rel="stylesheet" href='{% static "css_files/link.css" %}'>
<link href='https://fonts.googleapis.com/css?family=Oswald' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Fira Sans Extra Condensed' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Nanum Myeongjo' rel='stylesheet' type='text/css'>
</head>
</html>
url.py
from django.contrib import admin
from django.urls import path
from . import views #imported the views.py files here for our pipeline
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('home/',views.home,name="Homepage"),
path('donate/',views.donate,name="Donatepage"),
path('organization/',views.organization,name="Organizationpage"),
path('getinvolved/',views.getinvolved,name="Getinvolvedpage"),
path('about/',views.about,name="AboutUspage"),
]
urlpatterns += staticfiles_urlpatterns()
If you store your CSS inside your project folder under "static" directory then the following code for settings.py should work:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
....
STATIC_URL = '/static/'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
]
I can load my HTML templates but can't load css. When I view the source code in my browser and access the css file from there it shows the error "Page not found (404)" 'css\index.css' could not be found
I have tried all the things from doc but I don't know what I'm missing. Does anyone know what changes I need to make so that the css loads properly?
Here are relevant files:
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES_DIR = os.path.join(BASE_DIR, "templates")
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'parking',
'DIRS': [TEMPLATES_DIR, ],
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
app urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index)
]
urls.py
from django.contrib import admin
from django.urls import path, include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('parking.urls'))
]
urlpatterns +=staticfiles_urlpatterns()
this is my HTML file
<!DOCTYPE html>
{% load staticfiles %}
<html class="nojs html css_verticalspacer" lang="en-US">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="{% static 'css/site_global.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'css/master_a-master.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'css/index.css' %}" id="pagesheet"/>
<!-- IE-only CSS -->
<!--[if lt IE 9]>
<link rel="stylesheet" type="text/css" href="{% static 'css/nomq_preview_master_a-master.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'css/nomq_index.css' %}" id="nomq_pagesheet"/>
<![endif]-->
<!-- Other scripts -->
<script type="text/javascript">
var __adobewebfontsappname__ = "muse";
</script>
I have configured my static settings like so:
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('js', os.path.join(STATIC_ROOT, 'js')),
('css', os.path.join(STATIC_ROOT, 'css')),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
and these in my urls.py:
urlpatterns = patterns('',
url(r'^login/?$', login, name='login'),
url(r'^logout/?$', logout_then_login, name='logout'),
url(r'^profile/(?P<user_id>\d+)$', 'profiles.views.detail'),
url(r'^profile/edit$', 'profiles.views.edit'),
)
urlpatterns += staticfiles_urlpatterns()
It works very well for the url localhost:8000/login, but when I get to the localhost:8000/profile/edit site, which is handled by my profiles App, the {{ STATIC_URL }} changes all the paths from /static/... to /profile/static/..., so my javascripts and stylesheets are not found any more.
What'd I do wrong?
EDIT: Here would be my base.html
<!DOCTYPE html>
<html>
<head>
<title>Neighr{% block title %}{% endblock %}</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="{{ STATIC_URL }}js/jquery.min.js"></script>
{% block script %}{% endblock %}
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
Since you're using the django built-in developement server, try to remove the following line from your urls.py:
urlpatterns += staticfiles_urlpatterns()
In production, you'de better not serve static files with django, so use the collectstatic command.
edit
If settings.py, try something like this:
STATIC_ROOT = os.path.join(os.path.dirname(__file__), '../../static')
STATIC_URL = '/static/'