Django file structure w/ venv - django

I'm fairly new to Django and a bit confused about venvs and the file structure. I'm using VSCode to create all of this.
projectname?/
.vs/
....
.vscode/
....
myproject/ #1
myprojectapps?/
manage.py
myvenv/ #2
myproject/
Include/
Lib/
Scripts/
myprojectapps?/
manage.py
I create a top folder and point VSCode to it. I then create a new venv in this folder (#2).
Where exactly do I then create a new project? Inside my venv or inside my "root" folder?
If I create it in the root folder, will that use my OS Python installation instead of the venv, or is VSCode smart enough to use the venv that I created, if I select it from the dropdown?
Do I create new "apps" in the 'myproject' folders, or in the 'myprojectapps?' folders?

You'r folder structure is ok. you can have myenv in the same root folder.
Note:- mvenv is a place where we will have all thirdparty apps or repositories. So you dont want to have any of your code inside it. infact no code in menv should be under version control/git
So we will not create any django-app inside menv. it can be at same level as menv.
to your .gitignore file please add
.vs
.vsode
myvenv/*
etc
Normally folder structure of django project is something like this ..
projectname/
django-app1/
django-app2/
projectname/ # this is your main folder for settings.
settings.py
urls.py
...
manage.py
etc
so you would create django project you would use django-admin startproject projectname.
And to create an django app from rootfloder you can use python manage.py startapp django-app1
further if you want you can create django app inside a folder called apps/ for that you need to create the folder manually and then run python manage.py startapp django-app1 apps/django-aap1

Insert anything related to vscode inside project directory and open the project at the VScode but don't forget to add .vscode* at .gitignore
projectname?/
myproject/ #1
.vs/
....
.vsode/
....
myprojectapps?/
manage.py
myvenv/ #2
myproject/
Include/
Lib/
Scripts/
myprojectapps?/
manage.py

Related

How are the directories of a Django Project tree referred to?

Assume you have a directory where you keep all your Django projects:
$HOME/Programming/Django_Projects
If you in this directory call
django-admin startproject blog
you'll end up with the following director tree:
$HOME/Programming/Django_Projects/blog
$HOME/Programming/Django_Projects/blog/blog
What's the first blog and whats blog/blog?
When you use the command:
django-admin startproject blog
The first blog is just a website folder or simply a container for your project. The blog subfolder is the actual project folder which contains the following:
$HOME/Programming/Django_Projects/blog/blog/
__init__.py
settings.py
urls.py
wsgi.py
Bottom line is, the first blog is simply a container of your django project/files. You can always change its filename and it won't affect anything Django-related.
Also, what you can do is make use of:
django-admin startproject blog .
Notice the . after the name of your project, this will allow you to create a project directly in the current directory without having blog/blog
So, if you do:
$HOME/Programming/Django_Projects> mkdir first_django_project
$HOME/Programming/Django_Projects> cd first_django_project
$HOME/Programming/Django_Projects> django-admin startproject blog .
What you'll get is this project tree:
$HOME/Programming/Django_Projects/first_django_project/
blog/
__init__.py
settings.py
urls.py
wsgi.py
manage.py
Therefore no duplicate/confusing names.

Creating migrations for a reusable Django app

I am writing a reusable Django app and having problems creating migrations.
I have looked at this question, and I'm still confused. I have the following sort of directory structure:
django-mycleverapp/
django-mycleverapp/django_mycleverapp/
django-mycleverapp/django_mycleverapp/__init__.py
django-mycleverapp/django_mycleverapp/apps.py
django-mycleverapp/django_mycleverapp/models.py
django-mycleverapp/django_mycleverapp/urls.py
django-mycleverapp/django_mycleverapp/views.py
django-mycleverapp/example/
django-mycleverapp/example/manage.py
django-mycleverapp/example/example/
django-mycleverapp/example/example/__init__.py
django-mycleverapp/example/example/settings.py
django-mycleverapp/example/example/urls.py
django-mycleverapp/setup.py
As you can see, the directory "django_mycleverapp" contains my reusable app and the directory "example" contains a test project.
I include the models of "django_mycleverapp" in the INSTALLED_APPS section of the settings for "example".
However, running python ~/example/manage.py makemigrations django_mycleverapp doesn't build any migrations.
Any suggestions?
How am I meant to have a test project that will build migrations in "/django-mycleverapp/django_mycleverapp/migrations"?
Your app should be in the directory of your project. Your directory hierarchy should look like this.
django-mycleverapp/
django-mycleverapp/example/
django-mycleverapp/example/django_mycleverapp/
django-mycleverapp/example/django_mycleverapp/__init__.py
django-mycleverapp/example/django_mycleverapp/apps.py
django-mycleverapp/example/django_mycleverapp/models.py
django-mycleverapp/example/django_mycleverapp/urls.py
django-mycleverapp/example/django_mycleverapp/views.py
django-mycleverapp/example/manage.py
django-mycleverapp/example/example/
django-mycleverapp/example/example/__init__.py
django-mycleverapp/example/example/settings.py
django-mycleverapp/example/example/urls.py
django-mycleverapp/example/setup.py
If you do not want your app to be part of your "example" project, but rather want it to be separated and used by your project "example", you'll have to install it in your project using pip (this requires to have a setup.py at the root of your app).
For instance if you have published your app on a git repository "https://gitlab.com/myuser/myproject.git", you can add to our requirements.txt:
git+https://gitlab.com/myuser/myproject.git#v1.0#egg=myapp_name
If you don't have your app published on a git repository yet, you can add the absolute path to your app to you requirements.txt:
/path/to/django-mycleverapp/django_mycleverapp/
Don't forget to work in a virtualenv when you use pip.

django-tastypie: how to add custom commands

I know how to add custom commands for django projects. But when I use tastypie, the way to add custom commands just doesn't work. My currently file layout looks like below:
api/
__init__.py
manage.py
api/
models/
resources/
management/
__init__.py
commands/
__init__.py
createapikey.py
I don't know where should I put the management directory. In fact, I have already tried to put the whole management directory in up-level directory and inside the api/api/ folder. Neither way works.
I found the solution by following this link, since my project has no app in it, there is no way to put the management directory. So the solution is just create a core app and put the management directory into that core app. Problem solved : )

Deploy Django project: folder/project structure

My django project in eclipse has this project structure:
main-project-folder/
src/
main-app/
app1/
app2/
settings.py
manage.py
urls.py
__init__.py
media/
templates/
Can i deploy the project with this structure? In other words, is right way to put src and other folders (media, tempaltes, etc.) in the root folder of my server (where my domain is linked)?
Like:
my-server-folder/
src/
media/
...
I imagine that in my-server-folder i should put the entry point of project, but in my project i haven't an entry point in main-project-folder, or does django automatically redirect to an entry point of src/main-app folder (i think that it doesn't because i don't find any options that say to django to do it)?
Sure. That's a fine directory structure.
Keep in mind your web server isn't going to know what to do with the Django project unless you tell it. If your web server is Apache (which it probably is if you don't know) look here for instructions to set it up to run the Django app:
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/
And here for WSGI:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
Django apps aren't like PHP where you just upload them to the web server and they work.

How do I create sub-applications in Django?

I'm a Django newbie, but fairly experienced at programming. I have a set of related applications that I'd like to group into a sub-application but can not figure out how to get manage.py to do this for me.
Ideally I'll end up with a structure like:
project/
app/
subapp1/
subapp2/
I've tried manage.py startapp app.subapp1 and manage.py startapp app/subapp1
but this tells me that / and . are invalid characters for app names.
I've tried changing into the app directory and running ../manage.py subapp1 but that makes supapp1 at the top level. NOTE, I'm not trying to directly make a stand-alone application. I'm trying to do all this from within a project.
You can still do this :
cd app
django-admin startapp subapp1
This will work (create the application basic structure), however app and subapp1 will still be considered as two unrelated applications in the sense that you have to add both of them to INSTALLED_APPS in your settings.
Does this answer your question ? Otherwise you should tell more about what you are trying to do.
According to Django documentation,
If the optional destination is provided, Django will use that existing directory rather than creating a new one. You can use ‘.’ to denote the current working directory.
For example:
django-admin startapp myapp /Users/jezdez/Code/myapp
So, you can do it by this method:
Create sub_app1 directory in app directory
python manage.py startapp sub_app1 app/sub_app1
Django doesn't support "subapplications" per se. If you want code to be collected in packages within an app then just create them yourself. Otherwise you're just asking for pain.
Its Simple
Step 1 - Create Project
django-admin startproject app
cd app
Step 2 - Create api folder
mkdir api
cd api
touch __init__.py
cd ..
Step 3 - Create nested apps
python manage.py startapp user ./api/user
python manage.py startapp post ./api/post
python manage.py startapp comment ./api/comment
Step 4 - Register nested apps
INSTALLED_APPS = [
...
'api.user',
'api.post',
'api.comment',
]
Step 5 - Change name of Apps
Update the name in apps.py files in all three apps
class UserConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api.user'
class PostConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api.post'
class CommentConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api.comment'
and We are done!
Note : Step 2 is important
Go to your apps folder. Try:
python ../manage.py startapp app_name
django-admin.py startapp myapp /Users/jezdez/Code/myapp
Reference: Django Admin documentation
In order to create a nested app in the Django project, you have to follow the following steps:
Create a subdirectory in the parent directory where you want to add a sub-app.
mkdir finances/expences
N.B: Here finances is the existing parent app and expenses will be the next nested sub-app.
If the subdirectory is created successfully then run the following command in the terminal.
python manage.py startapp expenses finances/expenses
Please check the expenses subdirectory is now looks like a Django app and will behave like that.
Don't forget to register this app in the settings file and change the name from the config file of the app expenses, like -
class ExpensesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'finances.expenses'
Here, the name was only 'expenses' but it has been changed to 'finances.expenses' otherwise no migrations will be applied.
Use this command in the terminal:
python manage.py startapp application_name
there is really no any differ if you use django-admin or manage.py in this case - both will create
https://docs.djangoproject.com/en/4.0/ref/django-admin/#django-admin-and-manage-py
In addition, manage.py is automatically created in each Django project. It does the same thing as django-admin but also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.