The problem is setting the static file.
I set the file according to the official document said.
I first created a directory called static in the app which my URL links to
Then I made some changes on the template.
The code is below:
#Structure
src
|---makesite
|---sitemaker(app)
|---templates
| |----main.html
| |----static
| |-css
| |-style.css
|-static
|-manage.py
#The settings.py
STATIC_URL = '/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),]
STATIC_ROOT = os.path.join(BASE_DIR,"templates/static")
#The urls.py
from django.conf.urls import url
from django.contrib import admin
from makesite.views import make_site
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^main/',make_site),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, documents_root=settings.STATIC_ROOT)
#The File structure
#The template
<!DOCTYPE html>
{% load staticfiles %}
<html>
<head>
<title>Main Site</title>
<link rel='stylesheet prefetch' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'>
<link rel= "{% static %}'css/style.css'" href="style.css">
there is error in your template code. You should rewrite link tag to this:
<link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css">
In the settings.py file:
STATIC_URL = '/static/'
In order to load the static files in .html just use:
{% load static %}
Its always better to use {% load static %} on the top of the file.
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}My Website{% endblock %}</title>
and suppose you have base.html and somefile.html that extends the base.html use it like this:
{% extends "appname/base.html" %}
{% load static %}
In your code:
use:
<link href="{% static "css/style.css" %}" rel="stylesheet">
That will solve the problem.
Related
<meta charset="utf-8">
<title>Exam Page</title>
<link rel="stylesheet" href="../../../static/css/teststyle.css">
</head>
Im using this code in exam folder and i wanted to go to parent directory so i used ../../ but it is showing the below error
Not Found: /exam/static/css/teststyle.css
At the top of your template add {% load static %} and to add your css you can use {% static 'css/teststyle.css' %}.
{% load static %}
<meta charset="utf-8">
<title>Exam Page</title>
<link rel="stylesheet" href="{% static 'css/teststyle.css' %}">
</head>
In your main urls.py add
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and in your settings.py add
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
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 am trying to add CSS styling to my html email to be sent so I used django-inlinecss 0.3.0
In my template I am using the following:
{% load inlinecss %}
{% inlinecss "/css/bootstrap.css" %}
TEXT
{% endinlinecss %}
Here is the complete path of the CSS file:
C:\Users\User\Desktop\Project\static_in_env\css\bootstrap.css
I have tried the following:
{% inlinecss static "css/bootstrap.css" %}
After debugging I found that the reason is due to
[Errno 2] No such file or directory: 'C:\\Users\\User\\Desktop\\static_root\\css\\bootstrap.css'
Here is the files structure:
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static_in_env')]
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
So, how should I fix this error?
Follow the below steps:
1) setting.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR/"static/", ]
2) urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
3) Create Static Folder
create a static folder inside startproject (project_name).
The Folder name should be "static"
4) Add your css file
Inside that static folder create a "css" folder and then add your css file (bootstrap.css)
Once you done all the setup for static file, now we can work with html template.
5) Html Template
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Custom Css -->
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}" />
</head>
</html>
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"
I am using django 1.5.2 and I am having a hard time understanding the purpose of the media directory vs. the static directory and how to include stylesheets in my django project.
This is my directory structure:
django_books
beer
__init__.py
admin.py
models.py
views.py
random_book
(same as beer above)
django_books (the actual django project; beer and random_book above are my apps)
templates
base.html
beersall.html
__init__.py
settings.py
urls.py
views.py
wsgi.py
static
css
beers.css
media
css
beers.css
static
css
beers.css
manage.py
My beersall.html template (the beer output is correct, so just linking the stylesheet is wrong):
{% extends 'base.html' %}
{% block content %}
<div id="beer_list">
{% for beer in beers %}
{{ beer }},
{% endfor %}
</div>
{% endblock %}
My base.html file:
<html>
<head>
<link rel="stylesheet" type="text/css" href="/media/css/beers.css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/beers.css" />
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/beers.css" />
<link rel="stylesheet" type="text/css" href="/static/css/beers.css" />
<link rel="stylesheet" type="text/css" href="/Users/username/Projects/django_books/media/beers.css" />
<link rel="stylesheet" type="text/css" href="/Users/username/Projects/django_books/static/beers.css" />
<link rel="stylesheet" type="text/css" href="/Users/username/Projects/django_books/django_books/static/beers.css" />
{% block extrahead %}{% endblock %}
</head>
<body>
<div id="page_container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
My settings.py file:
MEDIA_ROOT = '/Users/username/Projects/django_books/media/'
MEDIA_URL = '/Users/username/Projects/django_books/media/'
STATIC_ROOT = ''
STATIC_URL = '/static/'
I should note that I am using the django development server, not apache.
The error in my browser (developer console) says beers.css is 404.
The url is localhost:8000/beers/ and my urls.py file correctly points to this and the views.py correctly serves the beersall.html template. How can I properly link my media?
EDIT
When I change the css link href value to /Users/username/Projects/django_books/media/ it still doesn't work.
EDIT 2
I've updated the href values to show things I've tried. Still not working...
User-uploaded files (by admins or site users) go to media directory.
Static files are files provided by developers (or from third-party apps).
Set something like this:
# Local disk paths
MEDIA_ROOT = '/Users/username/Projects/django_books/media/'
STATIC_ROOT = '/Users/username/Projects/django_books/static/'
# URLs visible in the browser's address bar
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
And in Your template use the STATIC_URL variable:
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/beers.css" />
And move your styles to the django_books/static/ directory.
in my project:django_learn, in template: check_uncheck_all.html, part pf the code:
{% load staticfiles %}
<html>
<head>
<script type=text/javascript src="{% static 'js/jquery-1.9.1.js' %}"></script>
</head>
you can also have a look at my settings.py
The static files were being found, but the permissions were not set up correctly. (i.e. a 403 not a 404 server error)
<VirtualHost *:80>
WSGIScriptAlias / /Users/username/Projects/django_books/django_books/django.wsgi
#the directory tag before was /Users/username/Projects/django_books/django_books/>
#this was one directory too deep
#from my structure above, you can see I needed to change my directory tag to this:
<Directory /Users/username/Projects/django_books/>
Order Allow,Deny
Allow from All
</Directory>
<Directory /Users/username/.virtualenvs/django_books/lib/python2.7/site-packages/django/contrib/admin/static/admin/>
Order Allow,Deny
Allow from All
</Directory>
Alias /static/admin/ /Users/username/.virtualenvs/django_books/lib/python2.7/site-packages/d$
Alias /static/ /Users/username/Projects/django_books/static/
</VirtualHost>