Django - app in subfolder - python

I've created new django app in a subdirectory using the command:
python manage.py startapp appName subFolder/appName
but if I try to add this app to INSTALLED_APPS at the end of the list I see the following error:
ImportError: No module named appName
Does anyone know what I am doing wrong?

You need to include the subfolder when you add the app to INSTALLED_APPS, for example:
'subFolder.appName',
or
'subfolder.appName.apps.AppNameConfig',

I tried different options but no one can solve that issue.
Finally, I found a solution.
Simply go to the subFolder/appName/app.py file and
replace this line name = 'appname' with name = 'subfolder.appname'
And then you can simply add to the installed apps list.
'subfolder.appname'

Related

ModuleNotFoundError : no module found named <module_name>django

I am trying to make a web application using django. so in the website folder, I made another app named U0.
I made sure to add it in the settings section of the website. but when I try to migrate the manage.py file it throws an error saying, No Module found named U0django
In order to create a new app in Django, you have to use the Django builtin command to create the correct structure of an app for you like this:
django-admin startapp u0
btw I believe if you want to create it on your own you have to create a file named apps.py in your app folder and add these lines to that to help Django identify it as an app :
from django.apps import AppConfig
class U0(AppConfig):
name = 'u0'

How do I get all files for my django app into one folder?

I currently have a Django app named reserve. In the folder named "reserve" I have most of the content of my app (views.py, urls.py, models.py, templates folder). However, I have a folder outside of "reserve" named "booking" that has only my settings.py. I tried consolidating by putting the settings.py in "booking" into "reserve" but I seem to be getting an error. Any advice on how to have only one folder with all contents?
The error I get is: ImportError: Could not import settings 'booking.settings' (Is it on sys.path?): No module named booking.settings
overall project folder
booking folder
settings.py
init.py
reserve folder (the app)
views.py
admin.py
models.py
...
what version of django are you using? what command are you running ? assuming that you are just trying to run a command using manage.py you could just chnage manage.py to reflect this if you are going to permanently keep this project structure
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
you should change os.environ.setdefault to the correct directory for your settings.py
I'm assuming you have manage.py at the overal project directory? I'm also assuming you want to just runserver.
It seems you moved the settings.py file outside the overal project directory. If you do this, you'll have to specify the settings file path when you do runserver, or you can also export the django settings module to point to your new settings path (using dot notation). In this case, I think it would be (in the command line):
export DJANGO_SETTINGS_MODULE=<overal-project-folder-name>.booking.settings
python manage.py runserver

Django not able to find the installed app

I have copied the djnago app from here
https://github.com/sigurdga/django-jquery-file-upload
I have put that folder in my root dir as fileupload.
But then go to .new/upload
i get the error no module named fileupload
But if go to python shell then i can import fileupload without any error
Based on the app you are installing, perhaps you should try putting this in the URL
upload/new
instead of
new/upload
Add the installed app in settungs.py file at INSTALLED_APP of the project then try to import it.

Django settings.py Error: Import by filename is not supported

I am running Django in a virtual environment (using virtualenv), and I'm trying to add a custom development environment settings file to simplify app configuration when I'm developing. My plan was to do this with two lines of code
if os.environ.get('DEVELOPMENT', None):
from login import settings_dev
I've also tried import settings_def and from login.settings_dev import *. My settings_dev.py file is sitting in the same directory as my settings.py file and my app is sitting in a folder called login. When I run python login/manage.py syncdb I get this error:
Error: Import by filename is not supported.
My searching keeps bringing up DJANGO_SETTINGS_MODULE (though I'm not sure how it plays into all this - first Django app :]), so just an FYI it is set in my settings.py file like so:
os.environ['DJANGO_SETTINGS_MODULE'] = 'login.settings'
I've also tried exporting it in my terminal, but I get the same error.
Does anyone know how I can fix this/what I'm doing wrong here?
Make sure while passing relative address of file to use "." instead of "/".
I faced the same error what I actually did
"music/urls"
But it should be
"music.urls"
In the original settings.py, at the very end:
try:
from settings_dev import *
except ImportError:
pass
Create settings_dev.py in the same directory as settings.py, and in it, add these two lines at the very top:
import sys
globals().update(vars(sys.modules['settings']))
Now add whatever development settings you want in this file.
I had similar error in runserver command execution and finally I've found that this error raises because of python version incompatibility by the django version installed. There is two versions of python on my system and I had running django server by the wrong one. Hope it could be helpful to someone.

Trouble running example django code

Im trying to learn Django by looking at examples, but Im having a bit of a problem running the examples I find.
I downloaded 'cheeserator' from https://github.com/jacobian/cheeserater
and I tried running it with python manage.py runserver
but I get the following error -
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.)
What am I doing wrong?
You need to have a settings.py file.
As per the instructions in the link provided:
Then you'll want to create a settings.py file in this directory containing::
from settings_template import *
# Override any settings you like here.
Or if you don't want to override anything rename settings_template.py to settings.py

Categories