Read configuration file in Django views.py - django

My question is simple: how to read any .txt or .ini file from views.py in Django project?
I need to get a simple parameter (ex. 4) from settings. I dropped my setting.ini in one dir with views.py (in app folder). And nothing from the previous answers seem to work:
In views.py:
filename = os.path.join(os.path.dirname(__file__), 'setting.ini')
returns FileNotFoundError, stating the exact right location, but being unable to read it: [Errno 2] No such file or directory: 'C:\\Users\\user\\Desktop\\XXXX\\XX\\XXXX\\XXXX\\setting.ini'
Setting PROJECT_ROOT in settings.py also doesn't help. When I drop setting.ini in one folder with settings.py and try
import os
from django.conf.settings import PROJECT_ROOT
file_ = open(os.path.join(PROJECT_ROOT, 'filename'))
FileNotFoundError as well.
Current app folder contents:
migrations/
static/
templates/
__init__
admin.py
models.py
setting.ini
tests.py
views.py

In views.py where you have the setting.ini file in the same directory.
filename = os.path.join(os.path.dirname(__file__), 'setting.ini')
print('My settings', open(filename).readlines())

Related

FileNotFoundError for the media folder after deploying Django app on Apache

I have a Django app that I just added to the already deployed Django web on Apache.
Because it is ran by Apache, path of the media folder seems to be different.
My app lets the user upload an excel file which then changes numbers and save as csv file.
(only showed relevant folders/code snippets)
Current directory
converts\
_init_.py
apps.py
forms.py
models.py
converter.py
urls.py
views.py
main\
settings.py
urls.py
wsgi.py
meida\
excels\
example.xlsx
csvs\
example.csv
static\
manage.py
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
main\urls.py
urlpatterns = [
path('', RedirectView.as_view(url='/converts/')),
path('converts/', include('converts.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The part that causes the problem is the following in converts/converter.py:
def convertExcel(name):
path = 'media/excels/'
key = path + name
wb = load_workbook(key)
Originally in development, a function in view calls convertExcel(example.xlsx) and the workbook, via media/excels/example.xlsx, finds the correct file to work with the loaded workbook. But in production server, it gives
FileNotFoundError: [Errno 2] No such file or directory: 'media/excels/example.xlsx'
My Question is:
Do I have to back track from where apache2.conf is to find the path? Or is there a way to set path to my django project so i can just set path in my convertExcel() as 'media/excels'? Or is there any other way I can call the uploaded workbook?
Any kind of help/comment would be appreciated.
Please comment if additional information is needed.
My guess is that you should use MEDIA_ROOT variable because it points to the uploaded files. So you would have
def convertExcel(name):
from django.conf import settings
path = os.path.join(settings.MEDIA_ROOT, 'excels')
key = os.path.join(path, name)
wb = load_workbook(key)

How to create hierarchical urls within django app?

I have multiple app based django project and within some apps url scheme getting complicated due to number of models. Hence I'm looking a way to make a hierarchical url structure within the app.
In my project's urls file I do the following.
from order import urls as order_urls
In the order app I have urls.py and urls directory which contains separate url patterns for each model as follows.
In the app's urls.py file I import the model's urls as follows.
from urls import rental as rental_urls
urlpatterns = [
url(r'^rental-request/', include(rental_urls)),
]
This gives me the error: ModuleNotFoundError: No module named 'urls'
If I put __init__.py it gives me circular import error.
I'm not sure this is the correct way/possible for my requirement. Anyone could explain the correct way to achieve it?
Having a folder called urls (with an __init__.py file) as well as a file urls.py in the same folder will probably cause trouble to load the module order.urls from anywhere in your project. How does Python will know which file must be loaded ?
Consider this structure:
├── main.py
├── urls
│   └── __init__.py
└── urls.py
And this content for each file:
# urls/__init__.py
urlpatterns = "I'm in folder"
# urls.py
urlpatterns = "I'm in file"
# main.py
import urls
print(urls.urlpatterns)
When you run main.py, the result is:
% python main.py
I'm in folder
Possible solutions :
You may delete the urls.py and move its content to urls/__init__.py, or rename the folder urls to avoid conflicts and updates imports accordingly (in urls.py)

Issue loading admin.site.urls after changing the settings folder

I re-organized Django,the following way:
config
- settings
- base.py
- local.py
urls.py
wsgi.py
In base.py/local.py:
ROOT_URLCONF = 'config.urls'
WSGI_APPLICATION = 'config.wsgi.application'
In manage.py I changed:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
In wsgi.py I changed:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
I have the following error on runserver:
\django\contrib\admin\sites.py", line 269, in get_urls
path('%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
AttributeError: 'AccountAdmin' object has no attribute 'urls'
It is related to this line:
path('admin/', admin.site.urls), # Django 2.0 syntax
If I comment that line I get the following error:
django\contrib\admin\sites.py", line 79, in check
if modeladmin.model._meta.app_config in app_configs:
AttributeError: 'AccountAdmin' object has no attribute 'model
The app admin is in installed app, I don't know what is creating this issue.
Hmm... Several things happen here. One thing at a time:
Under your settings dir put an __init__.py file with the following contents in it:
from .base import *
try:
from .local import *
LIVE = False
except ImportError:
LIVE = True
if LIVE:
try:
from .production import *
except ImportError:
pass
By putting this inside the __init__.py file, you can reference to your settings file simply with 'config.settings', leaving local or production unreferenced (the __init__.py will handle them).
Now that this is out of way, change both uwsgi.py and manage.py to:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
Assuming that you have done all that, it should work (that's how I structure my projects for years and had never any problems). Otherwise, please update your question with project structure and base.py and local.py contents to work it out.

ImportError After Moving App to Nested Folder

My application was working fine when I wanted to see whether I could organize my project in a better way. I read through this tutorial on structuring a django project.
Before my project structure was as follows:
camucamu
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
What I wanted to do was move the books app into an apps folder. Thus I did that and changed the project structure to the following:
camucamu
apps
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
I then changed the imports in views.py and admin.py
from books.models to apps.books.models.
I also changed INSTALLED_APPS in settings.py from books to apps.books.
When I then tried to run syncdb, I get the following error:
raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
django.core.exceptions.ImproperlyConfigured: ImportError apps.books: No module named apps.books
What am I messing up here so it can't find my app anymore?
Your apps folder does not have an __init__.py file so it cannot be recognized as a python module
I got the same error, following the same guide, as the last point of the following list was not cited. Make sure you performed the following changes:
Create a blank __init__.py file inside the apps folder (needed for python to recognize it as a package)
Update the import statements wherever you refer to an external app:
from projectname.apps.appname.models import YourModel, YourOtherModel
Inside settings.py edit INSTALLED_APPS such that it looks like this:
INSTALLED_APPS = (
...
# apps
'projectname.apps.appname1',
'projectname.apps.appname2',
)
This one is not specified in the guide: In all your urls.py files, update the urlpatterns!
BEFORE:
# client views
urlpatterns += patterns('appname',
...
)
AFTER:
# client views
urlpatterns += patterns('projectname.apps.appname',
...
)
Finally remember to update your changes by calling python manage.py syncdb
Hope that helped.

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__))