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/'
)
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/'),
)
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"),
)
Have a problem with connecting css to my template.
My project root is
"D:/birzha/", static path is "D:/birzha/static/", css in
"D:/birzha/static/css/template.css".
What STATIC_ROOT or STATICFILES_DIRS should I use for correctly viewing css file? I tried so much turns, but nothing happens, css still off.
First of all you need to tell where all your static files will be "collected", place the following lines in settings.py:
BASE_DIR = (os.path.dirname(__file__))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
Then you need to make a static folder within your app and the hierarchy
should be like this appname/static/appname/css.
Finally run the command python manage.py collectstatic and type yes on prompt. This will copy all of your static files within a folder that you specified in STATIC_ROOT
Now you can access your static files by giving the path like,/static/appname/css/mystyle.css
You can try this
Site root will contain you project root path. You can change "/templates" with static
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
# calculated paths for django and the site
# used as starting points for various other paths
TEMPLATE_DIRS = (
os.path.join(SITE_ROOT, 'templates/'),
)
MEDIA_ROOT = os.path.join(SITE_ROOT, 'templates/media/')
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(SITE_ROOT, 'templates/'),
)
I'm trying to start a new project. In the tutorial it says I need to store the templates and static folders in the project folder. My project folder name is twjp. But for some reason if is store the the templates folder in twjp folder it doesn't work. it works only if I store it in the twjp/twjp folder. Below is my settings.py
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
TEMPLATE_PATH = os.path.join(PROJECT_PATH,'templates')
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
TEMPLATE_PATH,
)
STATIC_PATH = os.path.join(PROJECT_PATH,'static')
STATICFILES_DIRS = (
STATIC_PATH,
)
"domainname/rango/ --> Doesn't work if I store templates in twjp/templates it works only if I store it in twjp/twjp/templates"
Tell me what ever you specs ie, Which version of django,python etc.
I hope this works for you, if not tell me i will update my answer too.
PROJECT_PATH = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_PATH, "static"),
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH,'templates')
)
This should your templates work with twjp/templates and twjp/static and not twjp/twjp.