Unable to connect background Image with static file Django - python

I have following html template
<div class="owl-slide cover" style="background-image: url({% static 'img/slides/slide_home_2.jpg'%});">
</div>
But I am not able to load static background.
my static directory is same as it is in django project path along with templates.
here is my part of setting.py for static files
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/',)
Everything seems look ok but my background is not working any suggestion will be helpful

Sorry to hear that. I don't know if you have solved you issue meanhwile.
In my settings.py I have the following STATIC setting
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Within every Django-App in my Django-Website I have a directory like this
<app-name>/static/<app-name>/...
And within the templates I am using the following code:
At the very beginning
{% load static %}
What I also recognized in your code is that your path starts with "img/"
If I want to refer to an image then I use
{% static "<app-name>/image.png" %}

at least when deploy on a server I have to execute the following command so Django collects all static files and make it available.
python manage.py collectstatic
Maybe this helps?

Related

img not showing in Django static

I use Django 2.0.3 and I want try static , I create one in app folder and my code like this.
{% load static %}
And for pic is :
<img src="{% static 'images/ic_team.png' %}">
in setting the dir of static is :
STATIC_URL = '/static/'
but the pic comes like this :
enter image description here
you have to actually route /static to serve your static files ...
normally you would do this in production using apache or nginx or simillar
if you are running with debug=True in your settings you can use django static file finders (something like the below)
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [ os.path.abspath(os.path.join(BASE_DIR,"..","usr","local","www","documents","media")),
]
but be aware it wont work unless debug is set to true, and this is never recommended for production servers
Make sure that your static folder is inside the app folder in your case will be something like:
your_app_name/static/images/ic_team.png
https://docs.djangoproject.com/en/2.0/howto/static-files/
You have this in the settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
put your static files in the project root folder

importing image to django page

I am trying to attach an image to my page (per https://docs.djangoproject.com/en/1.10/howto/static-files/#configuring-static-files), but it cannot be displayed: http://prntscr.com/evwern
settings.py
STATIC_URL = '/portfolio/'
urls.py
urlpatterns = [
url(r'^$', index, name='index'),]
index.html
{% load static %}
<img src="{% static '/catalog/static/portfolio/oh_i_ah.jpg' %}" alt="My image" style="width:640px;height:473px;"/>
Path to the image: mysite/portfolio/catalog/static/portfolio/oh_i_ah.jpg
If I try to open the image in the new tab it throws:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/portfolio/catalog/static/portfolio/oh_i_ah.jpg
1) You did not specify your app directory name. Need to know the app/directory name to answer your question properly.
2) You did not use 'static' properly.
3) You did not specify whether you are using default django development server or some production server. I am assuming that it's default django development server with DEBUG=True.
Solution
Let's imagine: You have an app named app0 (let's say you created that with "python manage.py startapp app0") and you have created a directory called "static " inside of that (following default django convention over configuration). Inside of this 'static' directory you have created another directory called 'app0'. So, you are going to put your file inside "/app0/static/app0". So, your file path becomes: "/app0/static/app0/oh_i_ah.jpg"
Now, set STATIC_ROOT properly in the settings.py
In the template file refer the file as {% static "app0/oh_i_ah.jpg" %} (without leading forward slash).
Now, everything will be ok.
Yes, you can put the static files outside of any app directory if you configure STATICFILES_DIRS, STATIC_ROOT in settings.py correctly.
Please properly describe your problem with as much details as possible so that we can help you in the right way.
in settings.py
import os
STATIC_URL = '/portfolio/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), )
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
and
python manage.py collectstatic
in index.html
{% load staticfiles %}
<img src="{% static '/portfolio/oh_i_ah.jpg' %}" alt="My image" style="width:640px;height:473px;"/>

Django: Where to store global static files and templates?

So I am working on a Django (1.9) project, and have the need to use the same templates and static files across multiple apps. I know how to use templates and static files stored outside each app (using STATICFILES_DIRS and TEMPLATEFILES_DIRS in settings.py), but where should I store them?
My guess would be in folders in the project directory.
home/user/myproject/staticfiles
home/user/myproject/templates
But if there are official reconsiderations (which I have been unable to find in the documentation), I would prefer to comply with those.
Also, if there are any other recommendations for using templates and static files outside of the normal django app directories, that you think I should know about, I'd really appreciate you telling me (or telling me where to look).
Cheers :)
Assume there is a global css file located here:
home/user/myproject/staticfiles/css/global.css
Change settings.py to match this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "staticfiles"),
]
Inside of a template, for example, index.html:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/global.css' %}" />
Now Django can find global.css.
in your settings.py file add the following
STATIC_URL = '/static/' # this will be added before your file name
STATIC_ROOT = 'path/to/your/files/' # this will help django to locate the files
read more about it here: https://docs.djangoproject.com/en/1.9/howto/static-files/
While nu everest's answer is correct and working (after you restart the server), in my case it lacks the advantage of my IDE autosuggesting the static files' paths. So I went for specifying a relative path inside my project. For simplicity I named the directory 'static' (same as the STATIC_URL variable) but of course any name is possible.
settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
"myproject" + STATIC_URL
]
What's important here: The root is actually not the settings.py's parent, but its parents's parent, as in:
BASE_DIR = Path(__file__).resolve().parent.parent

How to add my own files to django 'static' folder

I've read django static files document and made my django static files settings like this
setting.py
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'
html page
<img src="{% static "admin/img/author.jpg" %}" alt="My image"/>
So if I address one of django default static files, it works fine. But if I add my own file and folders to the static folder, it doesn't show it.
I tried
python manage.py collectstatic
But nothing changed. How can I make it work?
A few things...
STATICFILES_DIRS = (
'path/to/files/in/development',
)
STATIC_ROOT = 'path/where/static/files/are/collected/in/production'
When DEBUG = True, Django will automatically serve files located in any directories within STATICFILES_DIRS when you use the {% static 'path/to/file' %} template tag.
When DEBUG = False, Django will not serve any files automatically, and you are expected to serve those files using Apache, Nginx, etc, from the location specified in STATIC_ROOT.
When you run $ manage.py collectstatic, Django will copy any and all files located in STATICFILES_DIRS and also files within any directory named 'static' in 3rd party apps, into the location specified by STATIC_ROOT.
I typically structure my project root as such:
my_project/
/static_assets/ (specified in STATICFILES_DIRS)
/js
/images
/static (specified in STATIC_ROOT)
I hope that helps you understand how the staticfiles app works.
STATIC_ROOT is where files are collected and moved to when collectstatic runs.
If you want files to be consolidated to there they should be found by the static file finder.
As an example, include the following in your settings
STATICFILES_FINDERS = ("django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder")
now for one of the apps that are a part of your project, create a folder called static and put something in it.
When you run collectstatic you should see that file mentioned and it copied to your STATIC_ROOT
You can try adding
STATICFILES_DIRS = (
STATIC_PATH,
)
to your settings.py.
Also, if you haven't already, make sure to include
{% load static from staticfiles %}
in each of your templates where you wish to reference static files.
Lastly, make sure that the file you are referencing actually exists and the file path to it is correct.
Try to:
<img src="{{ STATIC_URL }}admin/img/author.jpg" alt="My image"/>
And in your view must be something like this:
...
from django.shortcuts import render_to_response
from django.conf import settings
def my_view(request):
...
static_url = getattr(settings, 'STATIC_URL')
...
return render_to_response(
template_name,
RequestContext(request, {
'STATIC_URL': static_url,
}))

Django 1.4 serving media files works, but not static

I am using Django 1.4 and for some reason i am able to serve media files, but not the static ones...
Here is my code:
settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
urls.py:
(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT}),
base.html:
<link href="{{ STATIC_URL }}css/bootstrap.css" rel="stylesheet">
<link href="{{ MEDIA_URL }}css/bootstrap-colorpicker.css" rel="stylesheet">
i get a 404 http not found... what am i doing wrong? An i did create the static folder in my project right next to media
http://mysite.com:8000/static/css/bootstrap.css
Your static folder should be under one app that you use it for.
For example, I have a project named my_project and an application named my_app, I have some static files used in my_app so I put them under ~/project_path/my_project/my_app/static
NB: my_app must be in INSTALLED_APPS. See STATICFILES_FINDERS documentation.
Edit:
As a best practice, you should have a global static folder in one app (the main one), for example a static folder how contains your html template basic resources as jquery, bootstrap, your global style.
And for the static files how's required only for one app, for example app foo, these files should be under foo/static folder
I suggest removing the explicit media and static views and allowing the staticfiles app to create them (when DEBUG is True under development).
Check the default finders are present in your settings.py
https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#std:setting-STATICFILES_FINDERS
Either add your project static directory to STATICFILES_DIRS (https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS) or place app specific static folders under each app. The app needs to be listed in the INSTALLED_APPS for the finders to locate the static content.
Do not place static files into STATIC_ROOT yourself. This directory is managed by the collectstatic command. See https://docs.djangoproject.com/en/dev/howto/static-files/#deployment
Here is how i define my media url in settings.py:
import os
ABSOLUTE_PATH = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(ABSOLUTE_PATH, '../media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(ABSOLUTE_PATH, '../static')
STATIC_URL = "/static/"
So like you see the difference is ../media and ../static
Is mysite in your installed apps ? Django look inside your installed apps and check if there's a static folder there.

Categories