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/'
Related
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>
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 getting broken images on django. Below are the relevant information with code, I guess. I am new to django and any information would be greatly appreciated.
name of project/site=imagesite
name of app=gallery
in models.py,
class Photo(models.Model):
pub_date = timezone.now()
image = models.ImageField(upload_to='images')
in views.py,
def view_gallery(request):
photo_list = Photo.objects.all()
return render(request, 'gallery/view_gallery.html', {'photo_list' : photo_list})
in view_gallery.html,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gallery</title>
</head>
<body>
{% csrf_token %}
{% for photo in photo_list.all %}
<img src="{{ photo.image.url }}" width="240">
<h2> {{ photo.pub_date }} </h2>
<br>
{% endfor %}
</body>
</html>
in settings.py,
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'imagesite/media')
in imagesite/urls.py,
urlpatterns = [
path('imagesite/', include('gallery.urls')),
path('admin/', admin.site.urls),
]
in gallery/urls.py,
urlpatterns = [
path('', views.view_gallery, name='view_gallery'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Your static() urls should be added to the main urls.py file, not to your gallery/urls.py. Now you're prepending them with /imagesite/ so basically you're telling django to create urls /imagesite/media/ rather than /media/.
Also, just for clarity: This is a url setting that should only be used for development, not for production. So it's better to do like this in your main urls.py:
from django.conf import settings
urlpatterns = [
path('imagesite/', include(gallery.urls)),
...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Finally, don't assume '/' for paths, as you see you're on Windows, so the path separator is a '\'. Your MEDIA_ROOT setting creates a folder "imagesite/media" rather than a folder "media" inside a folder "imagesite":
MEDIA_ROOT = os.path.join(BASE_DIR, 'imagesite', 'media')
I'm having trouble sorting my static directory and linking css files through templates in html pages with django. I keep getting the error "Not Found: /CSS/bootstrap.min.css"
I know this is a problem with how my directory is set up in settings.py but I can't seem to fix the issue. Below is my code for settings.py and layout.html (the page i'm using the call the css file).
layout.html
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Testing {% block title %}{% endblock %}</title>
<link href="{% static 'css/bootstrap.min.css' %}" type="text/css" rel="stylesheet">
</head>
<body>
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = 'C:/Users/Luke/Desktop/Capstone/CapstoneNotespool/capstonenotespool/capstonenotespool/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
You are almost there! You just need to add the correct static file directory to your STATICFILES_DIRS.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# STATIC_URL tells django the url path to use for all static files. It
# doesn't really have anything to do with your file locations on your
# computer.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
# Add this line here.
os.path.join(BASE_DIR, "capstonenotespool", "static"),
]
I think You must check again your static url, I think you config be wrong.
Here is answer you looking for.
Example of tree file
And this is my config for this
STATIC_URL = '/static/'
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", >"static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", >"media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static"),
)
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"