Django default settings 1.6 BASE_DIR acting up - django

In the new template for settings django 1.6 generates the following code:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
However this has never worked for me and I keep changing it to
import os
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
Is their code wrong or am I missing something? The idea of this BASE_DIR is to avoid hardcoding dir names.

The idea behind os.path.dirname(os.path.dirname(__file__)) is to get two directories above your settings directory.
Your code os.path.abspath(os.path.dirname(__file__)) is the same as os.path.dirname(__file__).
You probably changed where your settings.py resides and for that you must change its path.

Related

How to read shapefile in Django view helper function?

As part of my view logic I need to check if Latitude and Longitude points are withing city boundaries.
To do that, I am using city shapefiles with geopandas. It all works ok locally in plain python code.
However, when I run the following code in Django:
LA_geo_df = gpd.read_file('./City_Boundaries/City_Boundaries.shp')
I get the error:
DriverError at /
./City_Boundaries/City_Boundaries.shp: No such file or directory
What is the proper Django way of loading such files?
Auto-generated Django settings file will have a BASE_DIR setting that looks like this
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
This is an absolute file path to the root of your Django app. You can use this setting to build an absolute path to your file
CITY_BOUNDARIES_FILE_PATH = os.path.join(BASE_DIR, 'City_Boundaries', 'City_Boundaries.shp')
Then where you want to load the file you can use the setting
from django.conf import settings
LA_geo_df = gpd.read_file(settings.CITY_BOUNDARIES_FILE_PATH)

Django settings: How to access variables from the settings folder in an app

I have a Django project with the following structure:
--|src
--project
--|settings
--__init__.py
--production.py
--local.py
--|app1
In my app I import the settings (from django.conf import settings) and then as I was following a tutorial they said to do this getattr(settings, VARIABLE). That doesn't work for me. Instead I can do this: settings.VARIABLE. What's the difference?
Oh and I ran type(settings) and it outputted <class 'django.conf.LazySettings'>.
in order to access variables in settings.py file, you can do like this:
for example, I define STATIC_ROOT variable in settings.py file like this:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static', 'static_root')
and I can access to this variable like this:
from django.conf import settings
document_root=settings.STATIC_ROOT
The difference is that for various reasons (see the documentation for details), the settings object is not loaded unless an object is referenced from it.
The LazySettings object is special and you have to access it with settings.SOMETHING.
The reason its called "Lazy" is because the entire object is not loaded and made available to you when you import it. This LazySettings object acts like a proxy to the actual settings object.
project DIR
--|app DIR
--|settings.py <<< your variable API_KEY = '28234-jns-23-23n'
from app.settings import API_KEY

abspath definition Django

What's the difference between these two in settings.py of Django project
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
The function abspath will create a clean directory name, from root. For example ../dir may be converted to /path/to/dir (Or C:\... for Windows).
However the double dirname call does not seem useful.

Django: reading from STATIC_ROOT during testing

I render some pdf from some templates in my project. Well, it seems to work, but...
...I wanna test it!
Problem is that some files I read during rendering are in STATIC_ROOT - so I need to collectstatic when I run tests (kinda wierd!).
Question is: is there a way to work with STATIC_ROOT during testing, but without 'real' collecting static?
I use following way. In you project directory create a file named dev_static_finder.py with these contents.
# -*- coding: utf-8 -*-
from django.core.files.storage import FileSystemStorage
from django.contrib.staticfiles.finders import BaseStorageFinder
from django.conf import settings
class StaticRootFinder(BaseStorageFinder):
storage = FileSystemStorage(settings.STATIC_ROOT, settings.STATIC_URL)
Add this line to your settings.py
STATICFILES_FINDERS += ('dev_static_finder.StaticRootFinder',)
I don't know an easer way.

django project root self discovery

Ok so I recall there are some commands you could put in the settings.py file so that basically when you move your django project to another directory it won't get foo-bar'd up.
I know I could just do this by having a string variable everywhere it mentions the home directory but is there a more elegant way of doing this?
The architecture of a project in Django
root/
app1/
app2/
...
main/
settings.py
Inside settings.py:
SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) -> gives the path of the file settings.py: root/main/. This is NOT THE ROOT OF THE PROJECT
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__)) -> gives the root of the project: root/. This is THE ROOT OF THE PROJECT.
Django 1.8 already includes the project root directory as BASE_DIR:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
And you can use it in your app by importing settings:
from django.conf import settings
...
...
print(settings.BASE_DIR)
Grab the __file__ global, and use the various functions in os.path on it.
import os.path
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
PROJECT_PATH = os.path.abspath(os.path.dirname(__name__))