In my project, I just added new security methods, separating the .env from the project into another folder and adopting the use of new processes. To do this, I followed the python-dotenv lib documentation, which I use in my app.
But even following the suggestion I can not find all the parameters, since I end up having the following error:
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty
My settings:
from dotenv import load_dotenv
env_path = Path('/home/user/configs') / '.env'
load_dotenv(dotenv_path=str(env_path))
How can I resolve this problem from the specific path?
Note: I use in my project, python 3.4.5 and django 1.9.4, but I do not believe that the problem is caused by the old versions adopted in the project.
Another important detail is that the .env file is correct, because when I use it in the same folder where I find the file settings.py it works without problems!
I'm also using this approach and from past few months I'm following a very good developer and he also writes about a python dependency "python-decouple" and you will going to get it in detail at this link
I think this will definitely going to solve your problem.
Python-decouple github repo : here
You can add the folder containing .env to the path. Here's how I did it:
load_dotenv(os.path.join(BASE_DIR, "project", ".env"))
Answered it here.
Related
Im about to deploy my django-app to heroku. I will then use whitenoise to handle the static files. The tutorial im following tells me i have to add this to my wsgi.py file:
So the problem is that pycharm tells me: "Cannot find reference 'DjangoWhiteNoise' in 'django.py'"
However i have installed whitenoise! and it is located in the "External libraries". I even went down into the whitenoise.django file, and there is nothing named DjangoWhiteNoise there...
Thanks in advance. Havent found anythin about this concrete problem anywhere.
The tutorial you are following is for an older, outdated version of WhiteNoise.
There are up-to-date instructions here:
http://whitenoise.evans.io/en/stable/django.html
I want to run pylint recursively on all my project apps from the root of the app. If i use : pylint . on the root of the project base, it gives me the error that there is no init.
How can I run the pylint from my project root without having to specify all the apps one by one.
EDIT
Thank you heartily for the downvotes. But since I am a novice at that and still couldnt find a solution to my issue, can the downvoters please explain or the give the oh-so-obvious solution? If this is how pylint is designed to be called recursively on each app, then can you point me to the documentation?
So by default the pylint is requires the module name or the file name to be linted to be provided to it as args and it does not automatically recursively search from the pwd. It would however lint every python file in a module if a module name is provided.
In order to run the pylint without the hassle of having to maintain the apps, I am using :
PyCQA/prospector
After a long time away from an app i wrote in Django and didn't complete, I've come back to it on a new Mac.
I'm struggling to get the code to refer to the apps and the files within them without the explicit path. For instance:
from myproject.app.file import object
Whereas I remember not having to use myproject every time.
Is this something that has changed? I seem to remember being about to add to the path in manage.py which is called every time you run the dev server, but this hasn't worked this time.
sys.path.append /path/to/myproject
Should that fix the issue I'm having?
I started with a simple answer and it grew into more details on how to add subdirectories of your project to python path. Maybe a bit off-topic, but it could be useful to you so I'm pushing the post button anyway.
I usually have a bunch of small re-usable apps of mine I keep inside my project tree, because I don't want them to grow into independent modules. My projet tree will look like this:
manage.py
myproject/apps
myproject/libs
myproject/settings
...
Still, by default, Django only adds the project root to python path. Yet it makes no sense in my opinion to have apps load modules with full path:
from myproject.apps.author.models import Author
from myproject.libs.rest_filters import filters
That's both way too verbose, and it breaks reusability as I only use absolute imports. Not to mention if I someday build an actual python package out of some of the libs, it will break.
So, I took the following steps. I added the relevant folders to the path:
# in manage.py
root = os.path.dirname(__file__)
sys.path.append(os.path.realpath(os.path.join(root, 'myproject', 'apps')))
sys.path.append(os.path.realpath(os.path.join(root, 'myproject', 'libs')))
But you must ensure those packages cannot be loaded from the root of the project, or you will have odd issues as python would load another copy of the module. For instance, isinstance(libs.foo.bar(), myproject.libs.foo.bar) == False
It's not hard though : just remove __init__.py from the folders you add to the path. That will make sure they cannot be descended into from the project.
Also, Django's discover runner will not descend into those paths unless you specify them manually. That may be fine with you (if every module has its own test suite). Or you can extend the runner, so it knows about this: sample code.
It seems to be a Django best practice to add the settings.py file to the ignore list of the version control system. But how does one start to set up a project when the settings.py file was not included in the (Github) project*?
Starting from a default settings file is not at all easy because I have no experience with Django, and see that there are an specific settings for included modules/frameworks that need to be included.
Is there a way to create a settings.py file from the code in an existing Django project? Is there any other alternative to trial and error if the author cannot be contacted or is not responding to the request for an example file?
In fact is not a best practice to put it on the ignore list based on the opinion of Daniel Greenfeld (AKA Pydany) and Audrey Roy on "Two Scoops of Django". In fact is recommended that you keep one setting file for every setup your team members have.
Of course the easiest way to proceed is to contact the author and if he is that kind you can get it.
I would recommend the following:
Take a default settings.py from an new project
Locate the requirements.txt and verify which one of the apps located there are
django base and include them to the settings.py
Include all the
apps explicitly defined on the project (all the folders different to
the appname folder)
After that is just trial en error( or more
like trial and exception).
I have a pycharm project and, presumably, a Django project. Perhaps they are one and the same, perhaps not - I'm unsure as to the distinction.
Anyway, in my settings.py file (which is in the root of project directory, which is what I presume is my pycharm project) I have:
ROOT_URLCONF = 'dumpstown.urls'
Does that mean dumpstown is my project name? Or my pycharm project name? What is the distinction? Because I also have a folder called dumpstownapp and this has all my models.py and view.py files. I would have thought that dumpstownapp was the Django project, but I really don't know!
So, to be concise:
In this folder setup
folderA
folderB
views.py
models.py
<other stuff>
templates folder
settings.py
<other stuff>
which is the "Django project name" ~ and by that I mean, if I have a UserProfile defined in my models.py (shown above) what would be the AUTH_PROFILE_MODULE entry I'd need for it? I'm getting several understandings from the django docs - I'd assume
dumpstownapp.models.UserProfile
But from the docs I'd get
dumpstownapp.UserProfile
Or maybe my app is called dumpstown? and then what do I get?
FolderA is the Django project folder, and folderB is the Django app folder.
I haven't used a UserProfile, but according to the docs ( https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users) it should contain a string containing the Django app name and the model name separated by a dot, so "dumpstownapp.UserProfile" should be correct. If you show the error someone can probably give you better help..
Django documentation used to say that the parent of the project folder (i.e. the parent of folderA) should be on the path, but I believe that has been changed to also include the project folder itself (i.e. folderA) -- since it makes sharing of Django apps much easier. PyCharm seems to assume that is the case, since pressing Alt+F7 to auto-add an import for a newly used module create an import statement that assumes folderA is on the import path (I'm a relative newcomer to PyCharm, and I'm using it on a project that started in the Django 0.96 era, so I might just have things set up wrong..) But folderA is both the Django and the PyCharm project (the .idea file is where PyCharm stores its project data).
In one of my django-app git-submodule projects I needed to find out the name of the Django project that django-app/library was used in. To that end, I tried to get the path of the file that was being executed, find my package in the path and take the folder above it. However, it turned out that on the production server the project was deployed in a folder with a different name (a standard name like www or something along those lines). So this way is not fully reliable.
So I ended up setting a PROJECT variable in the django settings file and using that instead.