Django rest framwork folder structure - python

i found this question in stackoverflow link.
but I still haven't received the answer to my question, what exactly is the standard project structure in Django?

my_project/
├─ my_project/
│ ├─ settings.py
│ ├─ wsgi.py
│ ├─ asgi.py
│ ├─ __init__.py
│ ├─ urls.py
├─ my_app/
│ ├─ __init__.py
│ ├─ urls.py
│ ├─ models.py
│ ├─ views.py
│ ├─ admin.py/
│ ├─ apps.py
│ ├─ tests.py
Does that answer your question?
If not, please write down what you do not understand

Related

Pytest location issues

I have one problem with pytest. I am creating objects and I like to test these objects if they are complete or buggy. Now the thing: When I put the test script into the src-folder, it works fine. But when I want to use it in a tests folder, I get always an error, that if I like to load my object, a module is not found.
in this structure it works:
my-app/
├─ output/
├─ input/
├─ src/
│ ├─ some_package/
│ ├─ some_code.py
│ ├─ test.py
This structure gives me a ModuleNotFoundError:
my-app/
├─ output/
├─ input/
├─ src/
│ ├─ some_package/
│ ├─ some_code.py
├─ test/
│ ├─ test.py (with changed the relative paths)
I guess that pytest has some problems with using the correct path.

How to structure Sphinx documentation to prevent overriding documentation if files have same name

I have multiple small projects that are somewhat related to eachother so i want them to be in single sphinx documentation.
Each handler.py module have different docstring but when i build documentation both modules reference handler.py from App1.
Each App is single project but containing similar file names:
/
├─ conf.py
├─ index.rst
├─ App1/
│ ├─ handler.py
│ ├─ errors.py
│ ├─ DOCS/
│ │ ├─ modules.rst
│ │ ├─ handler.rst
│ │ ├─ errors.rst
├─ App2/
│ ├─ DOCS/
│ │ ├─ modules.rst
│ │ ├─ handler.rst
│ ├─ handler.py
in root "conf.py" i insert paths for them:
sys.path.insert(0, os.path.abspath("App1/"))
sys.path.insert(0, os.path.abspath("App2/"))
in root "index.rst" i include them in toctree:
.. toctree::
:maxdepth: 12
:caption: Content:
app1/docs/modules
app2/docs/modules
each hander.rst contains:
handler module
================
.. automodule:: handler
:members:
:undoc-members:
:show-inheritance:
How should i structure or reference python files so that each App.handler page contains docstring only from that app handler.py?

redirect to routes that has the same url in multiple apps

I'm new to coding so excuse me if this was a silly question but I couldn't find a clear answer anywhere.
So I have a django project with two apps inside it, both of them has the root '/',what is the exact format and implementation to use return redirect('/')to let django differentiate between these two roots in different apps maybe using naming routes.
a step by step guide will be appreciated, thank you in advance.
For example you have a django project called InitialProject and the project has 2 apps inside, let's call them MyApp1 and MyApp2.
Your directory structure should look something like this:
PythonProject/
├─ InitialProject/
│ ├─ __init__.py
│ ├─ asgi.py
│ ├─ settings.py
│ ├─ urls.py
│ ├─ wsgi.py
├─ MyApp1/
│ ├─ migrations/
│ ├─ filters.py
│ ├─ tests.py
│ ├─ urls.py
│ ├─ views.py
│ ├─ models.py
│ ├─ __init__.py
│ ├─ admin.py
│ ├─ apps.py
├─ MyApp2/
│ ├─ migrations/
│ ├─ filters.py
│ ├─ tests.py
│ ├─ urls.py
│ ├─ views.py
│ ├─ models.py
│ ├─ admin.py
│ ├─ __init__.py
│ ├─ apps.py
So firstly, URLs are unique, that is 1 URL can only lead to one path or one views function in this case. But what we can do is that we can say that URL starting with 'APP1' should look into MyApp1/urls.py for the function and URL starting with 'APP2' should look into MyApp2/urls.py
So in InitialProject/urls.py, we define it as follows:
from django.urls import path
from django.urls.conf import include
urlpatterns = [
path('APP1/', include('MyApp1.urls')), # Note the trailing forward slash, it is important
path('APP2/', include('MyApp2.urls')),
]
So, what this does is that is:
if your URL is: HTTP://localhost/APP1/
it will tell Django to look into MyApp1/urls.py to resolve and get the function and similarly for APP2.
Now in MyApp1/urls.py we can do is that:
from . import views
from django.urls import path
urlpatterns = [
path('', views.my_function_1), # This URL will resolve as HTTP://localhost/APP1/
path('second_url', views.my_function_2), # This URL will resolve as HTTP://localhost/APP1/second_url
]
And similarly can be done for MyApp2. By this method, you have a cleaner URL structure and it is also easy to navigate and see which app is being called.
I hope you are able to understand how URL Patterns work.
You might as why did we define the initial URL pattern in InitialProject/urls.py? Because by default Django will first go to the project directory to see which URL patterns and from there only we can link other App's URL patterns.

Python: 'ModuleNotFoundError' when trying to import module from files

i want to access config/cache.py from test.py And ,I need internal access like from config/env.py to database/modules/game.py but i get this error always:
ModuleNotFoundError: No module named 'modules'
my project tree:
project
├─ modules
│ ├─ cache
│ │ ├─ playtime.py
│ │ └─ __init__.py
│ ├─ config
│ │ ├─ cache.py
│ │ ├─ database.py
│ │ ├─ env.py
│ │ ├─ yaml.py
│ │ └─ __init__.py
│ ├─ database
│ │ └─ models
│ │ ├─ game.py
│ │ ├─ member.py
│ │ ├─ room.py
│ │ ├─ wiki.py
│ │ └─ __init__.py
│ ├─ room
│ ├─ utils
│ │ ├─ functions.py
│ │ ├─ logger.py
│ │ └─ __init__.py
│ └─ __init__.py
├─ run.py
└─ test
└─ test.py
and inits file are like that:
from .config import *
from .utils import *
from .database import *
from .cache import *
Python automatically insert the folder in which the main script resides to sys.path, which is a list containing folders where to find packages and modules.
So, if test.py were in folder project, that folder would be in sys.path and then you could import the modules package and any of its subpackages or modules.
import module # main package module
from module import config # subpackage config
from module.config import env # module env
(Just in case, a module is a file ending in .py, and a package is a folder with a file called __init__.py inside and optionally subpackages and/or modules.)
So one solution wood be to move the file test.py to projects
Another alternative is to add the folder projects (as a str) manually to sys.path which is a simple python list before importing.
import sys
sys.append('.../projects')
from modules.config import env

How to deploy a Vue.js App + Django Database to Heroku?

I created a Vue.js App with a Django Database. The Vue.js App is running with router and webpack-bundle. I want to deploy that App to Heroku, so I can access it from there. When I separately deploy them to Heroku it works, but not together in one combined app. My folder structure looks like that:
core/
├─ views.py
├─ __init__.py
frontend/
├─ node_modules/
├─ public/
│ ├─ favicon.ico
│ ├─ index.html
├─ src/
│ ├─ views/
│ ├─ App.vue
│ ├─ main.js
│ ├─ router.js
├─ .gitignore
├─ package.json
├─ README.md
├─ vue.config.js
├─ webpack-stats.json
my-app-products/
├─ api/
├─ admin.py
├─ apps.py
├─ models.py
├─ views.py
my-app-shop/
├─ asgi.py
├─ settings.py
├─ urls.py
├─ wsgi.py
├─ __init__.py
The main problem I encounter is, that the vue app is in a subdirectory and I don't know how to deploy two apps in Heroku. If that is even possible, to deploy two apps in one project.
I have knowledge on using git and on changing the heroku buildpack.
Thanks in advance

Categories