What I have:
a Django Project that is acting as a REST API through Django Rest Framework.
a number of Django Apps that are controlling the logic of my Postgres DB.
What I am trying to do:
Create a new Django App that represents a Service / Integration with Robinhood
Within ^^ I want to structure my logic in subApplications in order to separate all logic for a user vs a transactions vs a transfers etc ...
This entire Django App & all subApplications are APIs ONLY ... they will never need models / migrations but they will eventually communicate with the other Django Apps
CODE STRUCTURE:
Main Django Project
├── APP_holdings
├── APP_logs
├── APP_unique
├── APP_users
├── ⭐️⭐️ DJANGO
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── fixtures
│ │ ├── platforms.json
│ │ └── symbols.json
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── EXAMPLE.env
├── Pipfile
├── Pipfile.lock
├── Procfile
├── README.md
├── ⭐️⭐️ SERVICE_robinhood
│ ├── README.md
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ ├── ⭐️⭐️ user
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── api
│ │ │ ├── __init__.py
│ │ │ └── login.py
│ │ ├── apps.py
│ │ ├── urls.py
│ │ └── utils.py
│ │ ├── __init__.py
│ │ └── printSomething.py
│ └── views.py
├── manage.py
├── requirements.txt
├── static
│ └── __init__.py
└── staticfiles
SETTINGS.py
INSTALLED_APPS = [
# Pre Installed
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Apps
'APP_users.apps.AppUserConfig',
'APP_holdings.apps.AppHoldingsConfig',
'APP_logs.apps.AppLogsConfig',
'APP_unique.apps.AppUniqueConfig',
# !!! --- I AM ASSUMING I AM IMPORTING THE APP INCORRECTLY (and have tried many different ways mentioned on various SO threads -> See `.apps` code --- !!!
'SERVICE_robinhood.apps.ServiceRobinhoodConfig',
'SERVICE_robinhood.user.apps.UserConfig',
# Helpers
'corsheaders',
# Django Rest Framework
'rest_framework',
'rest_framework.authtoken',
]
SERVICE_robinhood/apps.py
class ServiceRobinhoodConfig(AppConfig):
name = 'SERVICE_robinhood'
SERVICE_robinhood/user/apps.py
class UserConfig(AppConfig):
# name = 'user'
name = 'SERVICE_robinhood.user'
DJANGO.urls.py
urlpatterns = [
path('admin/', admin.site.urls),
# ...auth...
path('user/', include('APP_users.urls')),
# ...otherApps...
path('RH/', include('SERVICE_robinhood.urls'))
]
SERVICE_robinhood/urls.py
# ?? do I need to import user here and pass it differently into `include` ??
# I thought that since `users` should be a module in and of itself in the overall Django Project you could just reference the app by name
urlpatterns = [
❌ path('user/', include('user.urls')),
✅ UPDATE - FROM ANSWER: ✅ path('user/', include('SERVICE_robinhood.user.urls')),
]
SERVICE_robinhood/user/urls.py
from . import utils
urlpatterns = [
path('printSomething/', utils.printSomething, name='printSomething')
]
Error I am getting:
SERVICE_robinhood/urls.py", line 23
...
ModuleNotFoundError: No module named 'user'
I am assuming that this is an issue with how I am importing the sub application into the overall app but have added all the relevant code I can think of to uncover any other problems.
Per Amine's answer (ProfileLink) do the following in your Django App to import a subApplications routes:
SERVICE_robinhood.urls.py
urlpatterns = [
path('user/', include('SERVICE_robinhood.user.urls')),
]
Related
I do not understand what is wrong with my Django backend specification
These are my urls
from django.contrib import admin
from django.urls import path,include
from django.conf.urls import url
from store import views
urlpatterns = [
url(r'^', include('store.urls')),
url(r'^accounts', include('registration.backends.default.urls')),
path('admin/', admin.site.urls),
]
This is the tree structure
bookstore
├── bookstore
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ ├── settings.cpython-36.pyc
│ │ ├── urls.cpython-36.pyc
│ │ └── wsgi.cpython-36.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── db.sqlite3
├── manage.py
├── requirements.txt
└── store
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│ ├── 0001_initial.py
│ ├── 0002_auto_20180604_0751.py
│ ├── __init__.py
│ └── __pycache__
│ ├── 0001_initial.cpython-36.pyc
│ ├── 0002_auto_20180604_0751.cpython-36.pyc
│ └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│ ├── admin.cpython-36.pyc
│ ├── __init__.cpython-36.pyc
│ ├── models.cpython-36.pyc
│ ├── urls.cpython-36.pyc
│ └── views.cpython-36.pyc
├── templates
│ ├── registration
│ │ ├── activate.html
│ │ ├── activation_complete.html
│ │ ├── activation_email_subject.txt
│ │ ├── activation_mail.txt
│ │ ├── registration_complete.html
│ │ └── registration_form.html
│ ├── store.html
│ └── template.html
├── tests.py
├── urls.py
└── views.py
Now when I try
python manage.py runserver
I got this
Also in terminal shows me
Not Found: /accounts
[05/Jun/2018 09:08:17] "GET /accounts HTTP/1.1" 404 5632
If I go for
url(r'^accounts/', include('registration.backends.default.urls')),
then I have
1. ^accounts/
....
The current path, accounts, didn't match any of these.
How to fix this?
How does Django backend work?
I am using 2.0.5 version.
Use path instead of url:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('store.urls')),
path('accounts/', include('registration.backends.default.urls')),
path('admin/', admin.site.urls),
]
By the way, if Django version <= 1.11, use url. (path was added after 2.0)
Try using
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^/', include('store.urls')),
Rather than
url(r'^accounts', include('registration.backends.default.urls')),
url(r'^', include('store.urls')),
When I run python /manage.py runserver , it generates the following error.
ImportError: No module named 'users'
I was thinking about this error, maybe I had a mistake about app setting.
$tree
.
├── LICENSE
├── README.md
├── functional_test.py
├── requirement
│ ├── development.txt
│ └── production.txt
├── users
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-35.pyc
│ │ ├── tests.cpython-35.pyc
│ │ └── views.cpython-35.pyc
│ ├── tests.py
│ └── views.py
└── wef
├── db.sqlite3
├── manage.py
└── wef
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-35.pyc
│ ├── settings.cpython-35.pyc
│ └── urls.cpython-35.pyc
├── settings.py
├── urls.py
└── wsgi.py
I think it is not a problem.
2nd, maybe I don't insert 'users' in settings.py
In settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'django_extensions',
'users',
]
I have to double check about these situations.
Here's my code:
urls.py
from django.conf.urls import url
from django.contrib import admin
from users.views import JoinUsView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', user, name='home'),
]
users/views.py
from django.http import HttpResponse
def user(request):
return HttpResponse("hello world")
You get the import error because the users directory is not on the Python path. The easiest solution is to move the users directory into the project wef directory (the one that contains manage.py).
└── wef
├── db.sqlite3
├── manage.py
├── users
│ ├── __init__.py
│ ├── __pycache__
...
└── wef
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-35.pyc
│ ├── settings.cpython-35.pyc
│ └── urls.cpython-35.pyc
├── settings.py
├── urls.py
└── wsgi.py
This will work because ./manage.py adds the project directory to the Python path. If the users directory is outside of the project directory, then you will have to modify the python path yourself.
I am working through Django 1.9 Django 1.9 Tutorial Part 5.
I am using Python 2.7.6 and Django 1.9.4.
The tree structure of my folders is:
django-mysite/
├── db.sqlite3
├── manage.py
├── mysite
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── __pycache__
│ │ ├── __init__.cpython-34.pyc
│ │ ├── __init__.cpython-34.sublime-workspace
│ │ ├── settings.cpython-34.pyc
│ │ ├── urls.cpython-34.pyc
│ │ └── wsgi.cpython-34.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
└── polls
├── admin.py
├── admin.pyc
├── apps.py
├── apps.pyc
├── __init__.py
├── __init__.pyc
├── migrations
│ ├── 0001_initial.py
│ ├── 0001_initial.pyc
│ ├── __init__.py
│ └── __init__.pyc
├── models.py
├── models.pyc
├── templates
│ └── polls
│ ├── detail.html
│ ├── index.html
│ ├── results.html
│ └── tests.py
├── tests.py
├── tests.pyc
├── urls.py
├── urls.pyc
├── views.py
└── views.pyc
When I run tests through command:
python manage.py test polls
or
python manage.py test polls.tests
It does not run tests. The output is:
Creating test database for alias 'default'...
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
The tests.py file has the code (as in tutorial)
import datetime
from django.utils import timezone
from django.test import TestCase
from .models import Question
class QuestionMethodTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() should return False for questions whose
pub_date is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertEqual(future_question.was_published_recently(), False)
Whats wrong?
Remove the tests.py from template/polls folder and then try.
To run the test, try python manage.py test polls
I know there are a lot of questions related to this, but those answers don't seem to work in my situation. I'm new to Django (I've done the tutorial), but I'm fixing someone else's code who I can no longer contact.
I'm running django 1.5 on Debian with python 2.7.
I received this error.
File "views-full.py", line 1, in <module>
from lop.models import File, V1, V2
ImportError: No module named lop.models.
views-full.py:
from lop.models import File, V1, V2
...
My tree is this (to save time, my views-full.py is under lop):
Main
├── Main
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ ├── urls.pyc
│ ├── wsgi.py
│ └── wsgi.pyc
├── manage.py
├── lop
│ ├── admin.py
│ ├── admin.pyc
│ ├── forms.py
│ ├── forms.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── migrations
│ │ ├── 0001_migrate.py
│ │ ├── 0001_migrate.pyc
│ │ ├── 0002_migrate.py
│ │ ├── 0002_migrate.pyc
│ │ ├── 0003_auto__add_category.py
│ │ ├── 0003_auto__add_category.pyc
│ │ ├── 0004_auto__add_field_script_category.py
│ │ ├── 0004_auto__add_field_script_category.pyc
│ │ ├── __init__.py
│ │ └── __init__.pyc
│ ├── models.py
│ ├── models.pyc
│ ├── tests.py
│ ├── urls.py
│ ├── urls.pyc
│ ├── views
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── viewsb.py
│ │ ├── viewsb.pyc
│ │ └── viewsb.py.save
│ ├── views-full.py
│ ├── views.pyc
│ ├── views.py.save
│ └── views-test.py
├── scripts [39 entries exceeds filelimit, not opening dir]
├── sqlite3.db
├── static [29 entries exceeds filelimit, not opening dir]
├── templates
│ ├── entry2-full.html
│ ├── entry2.html
│ ├── entry3-full.html
│ ├── entry3.html
│ ├── entry.html
│ ├── index.html
│ ├── index.html.old
│ ├── scriptlist.html
│ └── testData.html
└── user-dirs [109 entries exceeds filelimit, not opening dir]
As you see, both my __init__.py and models.py are in the same folder (which I know that them not being there was the problem in other cases).
settings.py:
...
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
# 'django.contrib.admindocs',
'lop',
'south',
)
...
I feel like I'm making some rookie mistake, but I can't figure it out.
I am not familiar with django, but here's what I do in that kind of situation:
import sys
sys.path.append('/path/of/folder/where/module/is/')
from new_module import new_function
So I figured out the problem which was from a fault on my part.
For the longest time, I thought that to test for any compilation errors (syntax), I would run "python < filename > migrate" or "python < filename > makemigrations" on the shell. My belief was that all changes needed to be migrated and not just ones in the database. I get errors messages when syntax mistake including this one and it has worked in other cases before. Someone eventually pointed out to me that is NOT how you test it and running "python manage.py runserver 0.0.0.0:8000" would point out any errors in the code in your local machine. I got no error messages for my specific file after that.
Gabriel L'Heureux, I still appreciated your help (I would give you a point up for it but I don't have enough points to do so) so you have my thanks.
I am trying to serve my Django project, set up with django-skel 1.4, using the development server. My site runs as expected except for my images, they are not served.
Part of templates/home.html
<img width="65px;" src="assets/img/pic.png" alt="" id="symbol" />
I'm guessing I should change something in this part: src="assets/img/pic.png".
I've looked around in SO threads and tweaked according to the given answers but I could not manage to make it work.
So how do I properly set images in templates?
Other relevant information:
settings.common.py
DJANGO_ROOT = dirname(dirname(abspath(__file__)))
MEDIA_ROOT = normpath(join(DJANGO_ROOT, 'media'))
MEDIA_URL = '/media/'
STATIC_ROOT = normpath(join(DJANGO_ROOT, 'static'))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
normpath(join(DJANGO_ROOT, 'assets')),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
Tree of project
.
├── apps
│ └── __init__.py
├── assets
│ ├── css
│ │ └── base.css
│ ├── img
│ │ └── pic.png
│ └── js
├── default.db
├── __init__.py
├── libs
│ ├── core
│ │ ├── admin.py
│ │ ├── __init__.py
│ │ ├── models.py
│ │ ├── views.py
│ │ └── views.pyc
│ └── __init__.py
├── settings
│ ├── common.py
│ ├── dev.py
│ ├── __init__.py
│ └── prod.py
├── templates
│ ├── 404.html
│ ├── 500.html
│ ├── home.html
│ └── install.html
└── urls.py
Btw: Please no solutions using if settings.DEBUG, preferably if possible without needing to adapt urls.py.
Edit
Tree of the top level directory after doing collectstatic
.
├── fabfile.py
├── gunicorn.py.ini
├── manage.py
├── Procfile
├── project_name
│ ├── apps
│ │ └── __init__.py
│ ├── assets
│ │ ├── css
│ │ │ └── base.css
│ │ ├── img
│ │ │ └── pic.png
│ │ └── js
│ ├── default.db
│ ├── __init__.py
│ ├── libs
│ │ ├── core
│ │ │ ├── admin.py
│ │ │ ├── __init__.py
│ │ │ ├── models.py
│ │ │ └── views.py
│ │ └── __init__.py
│ ├── settings
│ │ ├── common.py
│ │ ├── dev.py
│ │ ├── __init__.py
│ │ └── prod.py
│ ├── static
│ │ │ └── js
│ │ ├── css
│ │ │ └── base.css
│ │ └── img
│ │ └── pic.png
│ ├── templates
│ │ ├── 404.html
│ │ ├── 500.html
│ │ ├── home.html
│ │ └── install.html
│ └── urls.py
├── README.md
├── reqs
│ ├── common.txt
│ ├── dev.txt
│ └── prod.txt
├── requirements.txt
└── wsgi.py
Edit 2
My understanding how Django reads the path:
Let src="static/img/pic.png", from my settings.common.py:
>>> DJANGO_ROOT
'/home/my_username/web/my_project/my_project'
>>> j = os.path.join(DJANGO_ROOT, 'static/')
>>> print j
/home/my_username/web/my_project/my_project/static
But
>>> STATIC_URL
'/static/'
>>> j = os.path.join(DJANGO_ROOT, STATIC_URL)
>>> print j
/static/
So somewhere Django probably does os.path.join that is the only reason I can think of why
src="static/img/pic.png" works but src="{{STATIC_URL}}img/pic.png" doesn't. But why then does this apparently work for other people but not for me?
I think you have to add the following to urls.py
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
at least thats how I always do it...
You need to add
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.static',
)
and add staticfiles to your INSTALLED_APPS
You need to modify your img tag
<img width="65px;" src="{{STATIC_URL}}assets/img/pic.png" alt="" id="symbol" />
If it is user uploaded content then replace {{STATIC_URL}} with {{MEDIA_URL}}
Also see related Django cannot find my media files (on development server)