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

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.

Related

Django file structure w/ venv

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

manage.py - not in project folder?

I'm trying out Django for the first time, and I'm trying to follow the tutorial provided by the django team.
After I've created a new project I get the following folder/file structure, just as the tutorial says I should:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
When I create an app I run:
python manage.py startapp polls
...which creates the app 'polls' in the same folder as the manage.py file - which gives me:
mysite/
manage.py
polls/
__init__.py
admin.py
models.py
tests.py
views.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
This means the app I created gets created outside my project folder, which, of course, should not be the case. I have tried to move manage.py inside the project folder. But when I do that and run:
python manage.py syncdb
...I get the following error:
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'testproject.settings' (Is it on sys.path?): No module named testproject.settings
I guess I could move the app manually to the project folder, but this is something I shouldn't have to do.
So, either something is wrong in the tutorial (which I have very hard to believe), or I'm missing something out here (more likely).
Thanks in advance.
This is the new proper layout. "mysite/mysite" is an app, and "mysite/polls" is an app. The "mysite" parent folder is your project folder.
#holyredbeard that is the correct layout are you reading the older documentation?
Useful reading: http://www.tdd-django-tutorial.com/blog/articles/2012/tutorials-updated-django-14-and-its-weird-new-fold/
Don't move the manage.py it should sit outside the apps and the project folder.
since 1.4 common layout example...
project_root/
project_name/
media/
static/
static_root/ (in production)
templates/some_app/foo.html (overriding some_app at project level)
/admin/some_app/some_model/change_list.html
(overriding admin changelist for some_app.models.some_model)
settings.py
settings_deployment.py
urls.py
some_app/
templates/some_app/foo.html
urls.py
views.py
models.py
manage.py
This is the official layout since version 1.4.
The rationale behind is explained well in the release notes:
https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py
Do not move manage.py. In general you can expect that Django's own scripts always do the right thing, you never need to move any files just to get it working.

Django Directory Structure / Module Not Found

I use WebFaction for hosting.
On their website I created a Django project.
It gave me this directory setup:
/home/account_username/webapps/app_name/myproject/myproject/
Why "myproject" is used twice here?
I do not understand what each directory should be used for.
I do not understand which files should be in each directory.
Here is what's inside each directory:
app_name
apache2
bin
myproject
manage.py
myproject
init.py
init.pyc
settings.py
settings.pyc
tests.py
urls.py
urls.pyc
views.py
views.pyc
wsgi.py
What is the first "myproject" directory supposed to be used for?
What do most people name this directory?
What is the second "myproject" directory supposed to be used for?
What do most people name this directory?
WebFaction is using Django 1.4 and default project layout is been changed. See the Django 1.4 release notes for more details. You will get all answers.

Cannot start any django app

I am a newbie at Django and everytime I try to run
python panel/manage.py startapp %app% (panel is my project) it gives me the error:
Error: '%app%' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
Am I doing something wrong?
Surely companies or contacts or stats is not the name of an existing Python module?
This is a fun one - your project and your app need to have different names. You probably created a project, then tried to startapp with the same name.
I was confused as well, until I realized that a Django project is a container for applications; this sequence makes it a bit clearer:
# first create a Project (container).
django-admin.py startproject Project
# create multiple apps
cd Project
python manage.py startapp polls
python manage.py startapp hello
...
Perhaps you need to
cd panel
python manage.py startapp yourappname
I'm not sure running the command from a directory above your project will work properly.
I had the same issue because I was trying to "restart" my app after carrying out changes, but startapp is meant to be used once to create a new app. To view changes, syncronize app with DB with python manage.py migrate and restart the server with python manage.py runserver instead.
From the django-admin docs
(manage.py does essentially the same thing as django-admin)
startapp <app_label> [destination]
django-admin startapp
Creates a Django app directory structure for
the given app name in the current directory or the given destination.
By default the directory created contains a models.py file and other
app template files. (See the source for more details.) If only the app
name is given, the app directory will be created in the current
working directory.
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
This message is displayed if you run "startapp" twice with the same app name. As pointed out above by the OP it doesn't reload the app, it creates one.
You should choose different names for your project and app in Codes:
django-admin startproject **my_project**
python manage.py startapp **my_app**
You need to create the directory before using the commands. Suppose you want a polls app inside apps folder.
mkdir apps apps/polls
python manage.py startapp polls apps/polls
I guess maybe you have already created the app's dir in panel dir manually. The command 'startapp' is to create an app automatically. If you already have one there, it fails.
I reproduced the issue and there's actually something not working as I expected.
I wonder if we stumbled upon a Django's bug, or a limitation that I don't understand.
Having a project called "project" and an empty folder app/newapp
…I tried:
python manage.py startapp newapp apps/newapp
It returns:
CommandError: 'newapp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
But if I target ANY other route in which the last folder is not called the same name as the app I'm starting, it works.
So I ended up doing:
python manage.py startapp newapp apps/main
Using Django 2.1.3.
if you want to make an empty directory that will contain your new app
project-dir
└── blog
├── __init__.py
├── ...
├── blog-ext #this empty dir that will contain the new app
└── views.py
so instead of typing :
python manage.py newapp blog/blog-ext
it should be :
django-admin startapp newapp blog/blog-ext
Try classic "mysite" or "myproject". You can delete it anytime you want, so if it will accepted, then all your privious ideas conflict with Python modules.
Edit: I tried all your ideas, there was no error for me. So, if you installed support libraries or modules for django, then some of them can contains such names.
this error is because of the name conflicts between the app name and project name.you had given same name for your app and project .your project and app need to be different name .if you had given the same name the above mentioned error will occur .
understand the difference between app and project
Projects vs. apps
What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.
first create the project.
then create the app.
NOTE: name for app and project should be different
first create a project with projectname
django-admin.py startproject Projectname .
Then create app with appname. (to create your app make sure you are in the same directory manage.py and type this command)
python manage.py startapp Appname
It's the process how I got my doubt clear.
First, I created a directory inside my project directory and put __init__.py, models.py, admin.py, apps.py & views.py.
Then I ran python manage.py runserver & It work well.
Then as suggested on that page I used startapp command. I got this error :
CommandError: 'ucportal' conflicts with the name of an existing Python
module and cannot be used as an app name. Please try another name.
After that I deleted that directory and ran startapp command with same name and it worked fine.
So 'startapp' command is to create an app automatically. If you already have one there, it fails.
Answer given by #DAG worked for me.
I ran into this issue while trying to set up a Wagtail project.
Before creating the app, I had created and activated a virtualenv (using virtualenvwrapper) with the same name: $APPNAME. When I then ran wagtail start $APPNAME, Django looks for naming conflics in the $PYTHONPATH which in this instance points to /Users/User/.virtualenvs.
Naturally, this results in a conflict as /Users/User/.virtualenvs/$APPNAME already exists.
None of these answers helped me. In the end I ended up creating an app with a different name and then just renaming the directory to the app name I wanted all along. Note that you also will need to change the class name in apps.py to match your app name.
Just Simply Use This command
for Django Project Creation
python -m django startproject name_of_django_Project
for Django App Creation
python -m django startapp App_name
I had the same issue when working with wagtail cms. I got this error even there is no such a created app. This occurs when there is an app already that has the same name you need to create inside the site-packages directory.
Once you get this error, you need to check the following directory,
C:\Users\{user}\AppData\Local\Programs\Python\Python38-32\Lib\site-packages
If there is a package with the name same you want to create then you need to remove that package. Also make sure to check that package is important or not before deleting.
The application directory should be created first.
Example: apps/practice
The command appears to be duplicated, but it is correct.
Example: python manage.py startapp practice ./apps1/practice

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.