Django 1.8 import issue - django

This is the first time I need to run my app deeper in the directory structure, I have obviously messed up my app structure while setting this up and can't get it right anymore. Could somebody help me out here?
In my start.sh file I define following env vars:
export PYTHONPATH=${PYTHONPATH}:'/home/ubuntu/workspace/skw/3rd_apps/'
export PYTHONPATH=${PYTHONPATH}:'/home/ubuntu/workspace/skw/cskw/'
And my dir structure looks like this:
/home/ubuntu/workspace/skw/start.sh (also manage.py and gulpfile)
/home/ubuntu/workspace/skw/3rd_apps/ (django is here)
/home/ubuntu/workspace/skw/cskw/ (contains __init__.py)
/home/ubuntu/workspace/skw/cskw/apps/ (contains __init__.py)
/home/ubuntu/workspace/skw/cskw/core_app/ (settings.py is here and __init__.py)
I cycle between two errors here:
1. When trying to setup a custom signup form:
ACCOUNT_SIGNUP_FORM_CLASS = 'core_app.forms.SignupForm'
The form is in place but i get this error:
django.core.exceptions.ImproperlyConfigured: Module "core_app.forms" does
not define a "SignupForm" class
And if I fix this I can not import anything from apps, it expects them to be in core_app and does not see the app module at the same level as core_app.
manage.py points to "core_app.settings", so do the variables in settings.py.

Related

Unable to import apps from settings.py's INSTALLED_APPS list

I'm hitting an error when running makemigrations, and it seems to be related to my INSTALLED_APPS list.
Here is a screenshot of my project:
and here is the error:
I've also attempted to use the following strings in the INSTALLED_APPS list, but I got the same error:
'.api.apps.ApiConfig',
'.pizzas.apps.PizzasConfig',
The classes ApiConfig and PizzaConfig both follow this structure in their respective apps.py files:
from django.apps import AppConfig
class PizzasConfig(AppConfig):
name = 'pizzas'
Does anyone have any ideas as to what I could be missing here?
In some cases when you make a circular import between two or more apps (could be on models, views, any place) the django can't import propely when startup and shows this error.
Try comment all the code from the views.py in api and restart the project to see if works.
This happened to me when I had created an application with name t. However, it resolved when I used configs for registering it inside installed apps.
I believe you have a typo in the config names. An extra dot . at the beginning.
Try this one.
INSTALLED_APPS = [
...
'api.apps.ApiConfig',
'pizzas.apps.PizzasConfig',
]

Do I need to place __init__.py in each directory in a directory tree leading to my class?

I'm building a Django project with Python 3.6. I have created this directory structure ...
project
- manage.py
- scripts
- run_commands.py
- commons
- util
- __init__.py
- my_class.py
The contents of init.py are
from . import my_class
In another class, I attempt to import my MyClass like this
from commons.util import MyClass
but I'm getting this error
ModuleNotFoundError: No module named 'commons'
Am I creating my init.py properly?
It looks like the problem is that MyClass is not located in commons.util, because you only imported the module named my_class, not the class itself.
Instead the file commons/util/__init__.py should contain the following import:
from .my_class import MyClass
I don't think this will solve your problem, because you would be getting a different error than the one shown, but you will get errors for this eventually.
Update
First, I'd recommend reading this answer for a good explanation for how imports work in python.
Basically, when you execute from commons.util import MyClass, the interpreter scans the contents of sys.path for a module named commons.
I assume you didn't set sys.path to include your project folder, hence the ModuleNotFoundError.
TLDR; you have 2 options:
Manually set sys.path in run_commands.py to check your project folder (Don't do this!)
Use Django's Command class
To use Django's Command class, you will need to adjust your project folder similar to the following:
project
- manage.py
- commons
- management
- commands
run_commands.py
- util
- __init__.py
- my_class.py
Now in run_commands.py:
from django.core.management.base import BaseCommand
from commons.util import MyClass
class Command(BaseCommand):
def handle(*args, **options):
print("handling", MyClass)
You can execute the new command with the following:
python3 manage.py run_commands
It used to be the case that yes, you need to put an __init__.py in every directory that is going to be treated as a python module as without an __init__.py python wouldn't look inside that directory for importable code.
- project
- __init__.py
- commons
- __init__.py
- util
- __init__.py
- my_class.py
But as Reinstate Monica points out below this is no longer true as of Python 3.3+. So, depending on your version of Python you will need to make an informed decision.
Note, you might or might not need an __init__.py in the root project directory (if you need them at all), it depends if it has definitions that are part of the source tree. But you won't need it if it's just a container, like you see in the way most Django projects are organised, for example.
https://docs.python.org/3/tutorial/modules.html
https://docs.python.org/3/tutorial/modules.html#packages

Django: How to specify path to settings file

I know this question has already been asked multiple times but I still can't seem to find a solution that works for me. My Django project folder looks like this:
my_project
__init__.py
manage.py
my_first_app
my_second_app
core
Now the "core" folder looks like this:
__init__.py
some_other_stuff.py
settings
__init__.py
prod.py
dev.py
local.py -> dev.py
local.py is a symbolic link pointing to the right settings file, dev.py on my machine, prod.py in production.
Here's my problem: when I try to use manage.py, I get a weird error ImproperlyConfigured: The SECRET_KEY setting must not be empty. When I pass the path to the settings file local.py as an argument (--settings=core.settings.local) it runs fine. I figured the problem was that Django didn't know where to look for the settings file. How can I tell him (her?) where to look?
I already tried exporting the path to the env (export DJANGO_SETTINGS_MODULE=core.settings.local) and setting the PYTHONPATH to the parent directory, to no avail.
The primary use of __init__.py is to initialize Python packages. The easiest way to demonstrate this is to take a look at the structure of a standard Python module.
package/
__init__.py
file1.py
file2.py
As you can see in the structure above the inclusion of the __init__.py file in a directory indicates to the Python interpreter that the directory should be treated like a Python package
__init__.py can be an empty file but it is often used to perform setup needed for the package(import things, load things into path, etc).
One common thing to do in your __init__.py is to import selected Classes, functions, etc into the package level so they can be convieniently imported from the package.
In our example above we can say that file.py has the Class File. So without anything in our __init__.py you would import with this syntax:
from package.file import File
However you can import File into your __init__.py to make it available at the package level:
# in your __init__.py
from file1 import File
# now import File from package
from package import File
Source
So for conclusion, when you call import in __init__.py in a package mypackage, it's like you use package as a simple python file, that's what my solution do:
from .local import * in __init__.py
I haven't use this before in the settings case but I use it when I wanna to subdivide my models in a Django app, models.py --> models package, ./manage syndb doesn't discover my models declared, I found so this solution that's similar. You can find more details here
Last thing, I'm sure there's others solution to your problem, but this can be the most simple.
Good luck
You are in import hell somewhere. Had this problem too one time. The only way to find out where the root of your problem is, might be to disable all apps, try starting the server, enable the first app, start the server, enable the next etc.
BTW: your project layout should not be used from Django 1.4 onward. https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py
I'd try to use the new layout and hope that it 'just works'.
I think you need to change the name of the file that the manage.py is looking for.
try:
imp.find_module('settings') # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
sys.exit(1)
If you had the settings.py file in the same directory simple changing the 'settings' to 'local' would have worked.
But, since you have it in a different directory, I think you need to configure the settings. Refer to this: https://docs.djangoproject.com/en/dev/topics/settings/#using-settings-without-setting-django-settings-module
from django.conf import settings
settings.configure(DEBUG=True, TEMPLATE_DEBUG=True,
TEMPLATE_DIRS=('/home/web-apps/myapp', '/home/web-apps/base'))
Hope that helps.

What is the minimal ammount of code I need for using django-cms-search 0.2.6?

I want to use django-cms-search in my project and have installed it together with xapian and the xapian_backend. In my settings file, I have defined HAYSTACK_SEARCH_ENGINE, HAYSTACK_SITECONF and HAYSTACK_XAPIAN_PATH (because the django system is a bit old). Furthermore, I've included haystack.urls and created a corresponding template.
The index is not created when I run manage.py update_index.
Is it necessary to put any code into code into HAYSTACK_SITECONF?
The docs suggest that I sould create a models.py file in the directory defined in HAYSTACK_SITECONF and put this code into it:
from cms_search.cms_app import HaystackSearchApphook
from cms.apphook_pool import apphook_pool
apphook_pool.register(HaystackSearchApphook)
But this has no effect.

Using a settings file other than settings.py in Django

I want to use a different settings file in django -- specifically settings_prod -- yet whenever I try to do a syncdb with --settings=settings_prod, it complains:
python2.6 manage.py syncdb --settings=settings_prod
Error: Can't find the file 'settings.py' in the directory containing 'manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)
I've also tried setting the environment variable DJANGO_SETTINGS_MODULE=settings_prod to no end.
Edit: I have also set the environment variable in my wsgi file, also to no end:
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings_prod'
application = WSGIHandler()
Suggestions?
Try creating a settings module.
Make a settings folder in the same directory as manage.py.
Put your different settings files in that folder (e.g. base.py and prod.py).
Make __init__.py and import whatever settings you want to use as your default. For example, your __init__.py file might look like this:
from base import *
Run your project and override the settings:
$ python2.6 manage.py syncdb --settings=settings.prod
I do know that no matter what you do with manage.py, you're going to get that error because manage.py does a relative import of settings:
try:
import settings # Assumed to be in the same directory.
http://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-option---settings
Note that this option is unnecessary
in manage.py, because it uses
settings.py from the current project
by default.
You should try django-admin.py syncdb --settings=mysettings instead
this works for me:
DJANGO_SETTINGS_MODULE=config.settings.abc python manage.py migrate
this will help you:
create a another file "setting_prod.py" with your original settings.py file.
write down your setting which you need to run, in setting_prod.py file.
Then import setting_prod.py file in your settings.py file.
for ex.
settings.py:
VARIABLE = 1
import setting_prod
setting_prod.py
VARIABLE = 2
After importing setting_prod.py file in settings.py file, VARIABLE will set to new value to "2" from "1".
We can use this method to set different settings file, for example, I use different settings file for my unit test (settings_unit_test.py). Also I do have other settings file for different infrastructure environment settings_dev.py, settings_test.py and settings_prod.py.
In windows environment(same can done in linux as well)
set DJANGO_SETTINGS_MODULE=settings_unit_test
set PYTHONPATH=<path_of_your_directory_where_this_file_'settings_unit_test.py'_is_kept>