This is my project tree:
projectname
projectname
init.py
settings.py
urls.py
wsgi.py
appname
init.py
admin.py
models.py
test.py
views.py
urls.py
templates
base.html
login.html
Now in the settings.py I am using this code:
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "projectname", "templates"),
)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "projectname", "static", "static-only")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "projectname", "static", "media")
How can I get the path of the project directory, so that I don't need to type the project name projectname in the code and use that code in any other django project?
Update
Or can I just use this
BASE_DIR+'/templates'
BASE_DIR+'/static/media'
Or is it a bad idea?
I would suggest you to use os.path.abspath:
# Project root is intended to be used when building paths,
# e.g. ``os.path.join(PROJECT_ROOT, 'relative/path')``.
PROJECT_ROOT = os.path.abspath(os.path.dirname(__name__))
# Absolute path to the directory where ``collectstatic``
# will collect static files for deployment.
#
# For more information on ``STATIC_ROOT``, visit
# https://docs.djangoproject.com/en/1.8/ref/settings/#static-root
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
# Absolute path to the directory that will hold uploaded files.
#
# For more information on ``MEDIA_ROOT``, visit
# https://docs.djangoproject.com/en/1.8/ref/settings/#media-root
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'uploads/')
BASE_DIR already includes "projectname". When you do os.path.dirname(BASE_DIR), you go up a level from projectname; only to add it back in. Don't do that.
Instead, just use BASE_DIR directly:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, "templates"),
)
Related
This worked fine everytime used to do django websites but this time it is giving me an error.
Settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'portfolio/static/')
]
STATIC_ROOT = os.path.join(BASE_DIR , 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
I have a profile.jpg in my directory Portfolio-Project/Portfolio/static/profile.jpg. It should collectstatic from here and paste the staticfiles in Portfolio-project/static as mentioned in my code. but it is giving ,me some error.
Error After using the command "Python manage.py collectstatic"
django.core.exceptions.SuspiciousFileOperation: The joined path
(C:\Users\Kiran\Desktop\portfolio-project\portfolio\static\Profile.jpg) is
located outside of the base path component
(C:\Users\Kiran\Desktop\portfolio- project\portfolio\static\)
Please Help.
Thanks
In your line:
os.path.join(BASE_DIR, 'portfolio/static/')
Delete the last slash:
os.path.join(BASE_DIR, 'portfolio/static')
Anyway, this is the ideal:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
I recently faced this error but it‘s actually simple:
Chances are you downloaded a Template, here is the solution:
First you have to check your css file for stuff like relative links
For example your CSS might be referencing a file outside your django project itself.
e.g. backround:url('...\image\Profile.jpg') this actually worked for me
Note:
The key fact is just to check your CSS or (maybe js) file first if it‘s a referencing file that you‘re not using or file that is referring to something that is not in your django project directory.
So I upload my site to digitalocean and when I went to the admin page the CSS was not showing
I visit all these sites but nothing seems to work
Django doc-static files
Pythonanywhere-DjangoStaticFiles
StackOverflow -why my django admin site does not have the css style
I set up my STATIC_ROOT and STATIC_URL, and then I ran
python manage.py collectstatic
And here is my seting
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/home/django/django_project/django_project/static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static/'),
]
You are specifying an Absolute Path for your join.
os.path.join(BASE_DIR, arg2) means join the current directory that is being executed and append the second argument.
add these lines into your settings.py file
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
I have a django application that sends off an email containing image files as MIME attachments. The emails are sent from views.py, but in order to attach the files, I need to get the full path name of the image(s), so python can open them. These files are in the static folder in my app, but I can't seem to find a way that I can get the full path of the file on the filesystem that works in development mode - It works fine in production after collecting static, but in dev, it can't find the file as the static files are served from individual app folders in development.
Use finders-module of Django
from django.contrib.staticfiles import finders
result = finders.find('css/base.css')
searched_locations = finders.searched_locations
String result is the file-system path, and if not found, double check searched_locations
project_dir.url add to the end of file
if DEBUG:
urlpatterns += patterns(
'',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': STATIC_ROOT}),
)
project_dir.settings
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_debug')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
make dirs media & static_debug (add them to .gitignore)
project_dir/
static/
image.jpg
settings.py
urls.py
apps/
__init__.py
some_app/
static/
some_app/
css/
style.css
media/
static_debug/
Now you can run project or directly
python manage.py collectstatic
access from views
from django.templatetags.static import static
static('image.jpg')
static('some_app/css/style.css')
access from templates
{% load staticfiles %}
{% static 'image.jpg' %}
{% static 'some_app/css/style.css' %}
After doing lots of mistakes the following thing worked for me..
Suppose you have following directory structure (Thanks #madzohan)
project_dir/
static/
image.jpg
settings.py
urls.py
apps/
__init__.py
some_app/
static/
some_app/
css/
style.css
And now if you want to get the path of project_dir/static/image.py then
In your project_dir/settings.py file define a STATIC_DIR varaible on the file scope like. ( If not done before )
project_dir/settings.py
import os
# after your other file variables
STATIC_DIR = os.path.join(BASE_DIR, 'static')
Now in your app_name/views.py file from where you want to acess the /static/image.jpg file
app_name/views.py
import os
from project_dir.settings import STATIC_DIR
# here I want the /static/image.jpg file then
image = os.path.join(STATIC_DIR, 'image.jpg')
And then you can have acces to image.jpg file throught image variable
Hope this works well for you
Since you want an app's static, here's what you need!
from django.contrib.staticfiles import finders
APP_LABEL = 'app_youre_looking_at'
FILE_NAME = 'file_name_you_want_to_read_or_write.ext'
stores = finders.AppDirectoriesFinder(app_names={APP_LABEL}).storages
print(f'Here it is: {stores[APP_LABEL].path(FILE_NAME)}')
The FILE_NAME is not required to exist. You can use stores[APP_LABEL].path('') to get path only.
I am new to django ! When I use the command python manage.py collectstatic I get this error
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path
But I can successfully run the server .
My static files declarations are :
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('assets', os.path.join(PROJECT_DIR, '../static')),
)
and debug is set to true
DEBUG = True
How can I fix this? Else am missing any installation packages ?
Try this,
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
Look at https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATIC_ROOT
You must have to give path in STATIC_ROOT in settings.py where all your static files are collected as for example:-
STATIC_ROOT = "app-root/repo/wsgi/static"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('assets', 'app-root/repo/wsgi/openshift/static'),
)
you can create 'static' folder in any subfolder and have required files in it.
In settings.py add the following lines of code:
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATIC_URL = '/static/'
After running
python manage.py collectstatic
a new static folder will be created in your parent App folder
well had this error as well. I fixed:
STATIC_URL = '/static/'
if DEBUG:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
else:
STATIC_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = os.path.join(BASE_DIR, 'assest')
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')
]
I had to put STATIC_ROOT and STATIC_URL above the STATICFILES_DIRS declaration.
STATIC_ROOT = "/var/www/YourSiteFolder/static/"
STATIC_URL = '/static/'
look at https://docs.djangoproject.com/en/1.11/howto/static-files/#deployment
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
This works for me
if you want to load static files rather than admin panel files or getting errors while loading webpage static files like CSS js etc
I suggest you change the folder name of 'static' to 'staticfiles'
and then add this code in your settings.py
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
then after run python manage.py collectstatic
Then the problem will be fixed
I am new to Django. I am using django administration for basic crud purpose.
I found that template for django admin resides at
C:\Python27\Lib\site-packages\django\contrib\admin\templates\admin
I need to change it as my own location .. i created one folder "template" on base dir of project and added following lines
STATIC_URL =os.path.join(BASE_DIR, 'templates')+'/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
i copied all files from C:\Python27\Lib\site-packages\django\contrib\admin\templates to basedir/templates
but still it is referencing to C:\Python27\Lib\site-packages\django\contrib\admin\templates
what is the best way?
try this,hope this helps you
import os
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
...
#MEDIA_ROOT = PROJECT_PATH + '/media/'
TEMPLATE_DIRS = (
PROJECT_PATH + '/templates/'
)