Structure in Django tutorial does not match match my strucuture - django

Starting a new project and realized that the structure for the tutorial and my structure do not match. It looks like a directory was created for the myproject and a directory AND a file were created for myapp... I don't understand. Is this correct?
Tutorial structure shows:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
My structure shows:
mysite/
myapp
myappp
__init__.py
settings.py
urls.py
wsgi.py
manage.py

#NinaPy, Your directory structure seems to be correct but there exist an issue.
Please follow the steps:
1) Go to your directory where you want to keep your project. Example--myproject folder
cd myproject
2) Now you are within your desired folder. run the command from CLI(Command Line Interface)
django-admin startproject mysite //mysite is the project name
it will create
1 directory[mysite] and 1 file[manage.py]
3) Now if you want to create an app: Go to mysite directory because manage.py exist in there.
cd mysite
run the command from CLI
python manage.py startapp myapp
It will create myapp in same level.
myproject/
mysite
manage.py
myapp
If you use the CLI then you are quite safe for folder directory and file structure. Do not try it to do manually unless you need some extra files.
Please have a look on here:
https://docs.djangoproject.com/en/3.0/intro/tutorial01/#creating-a-project

your can run python manage.py startproject to create a new project

yes its correct
before run type on terminal(enter the directory mysite)
cd mysite
then run
python manage.py runserver
python manage.py startapp appmane
........
..........

Related

Django heroku : Unable to deploy application (ModuleNotFoundError)

When I am deploying my Django application on Heroku, the application crash and Heroku's logs notify me about this error :
2020-12-07T12:55:55.982458+00:00 app[web.1]: ModuleNotFoundError: No module named 'WebSite'
The thing is, WebSite is not a Python module, but a folder in my Django application (Achievement Comparer being my main application name).
AchievementComparer/
AchievementComparer/ ← My Django project
static/
staticfiles/
WebSite/ ← A Django app
__init__.py
manage.py
wsgi.py
Procfile
requirements.txt
runtime.txt
Of course, if I start my application locally, everything works just fine (running py manage.py runserver on Windows, as gunicorn is only available on unix).
The current issue is that you have an extra directory layer:
AchievementComparer/ ← This shouldn't be here
AchievementComparer/
WebSite/
manage.py
…
Procfile
requirements.txt
runtime.txt
Move everything that's currently in the top-level AchievementComparer/ directory up a level and delete the now-empty old AchievementComparer/ directory:
AchievementComparer/
asgi.py
settings.py
wsgi.py
WebSite/
manage.py
Procfile
requirements.txt
runtime.txt
A few other issues that are likely to bite you next:
The only wsgi.py file you should need is the one that's currently in your AchievementComparer/ project directory, beside your settings.py. Delete the other one.
Your Procfile is hard-coding a port. You'll need to bind to whatever port Heroku assigns you via the PORT environment variable.
You don't need --pythonpath in your Procfile, but you should update the module to use the full module name AchievementComparer.wsgi.
All in all, try changing your Procfile to something like this:
web: gunicorn AchievementComparer.wsgi:application --bind=0.0.0.0:$PORT --log-file -

Django: how to startapp inside an "apps" folder

I feel like I must be looking over something obvious because I am having trouble finding anything online about this. But basically here it is, I have a django Project, and I would like to store my apps inside of an apps folder, like thus...
myproject/
client-side/
media/
static/
templates/
apps/
app1/
app2/
etc.
__init__.py
manage.py
etc. (rest of root directory)
but have been unable to figure out how to >>>python manage.py startapp newapp and have it placed into the /apps/ folder. Is it ok to simply ok to startapp at the root level and manually move them into the /apps/ folder?
I greatly appreciate your thoughts and answers.
FYI running python 3.4.2 and Django 1.9.5 on Windows 10
You can specify the app's directory as a second parameter:
python manage.py startapp <app_name> <app_directory>
Note that no directory will be created, the app's files will be created directly in the specified directory. Example:
python manage.py startapp myapp apps/myapp
Will result in the given directory structure:
apps
└── myapp
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py
Also note that the command won't create the directory for you.
Edit: as another (now deleted) answer pointed out, running the command from the apps directory would also work:
cd apps
python ../manage.py startapp myapp
You can also use the django-admin command as well.
cd apps && django-admin startapp app_1
this will work as well

Should I work and create files in mysite or mysite/mysite?

I just started using django 1.4 and I realized that inside mysite there is another folder called mysite which has the same files as mysite.
Should I work and create files in mysite or mysite/mysite?
Starting with django 1.4, the default project structure changed, moving your main project files down into a packages. Apps will live in packages parallel to the main project.
Updated default project layout and manage.py
Though your layout example does not look like the default 1.4. Models should live in apps that you create. Run the following command in your project:
python manage.py startapp myapp
To get a layout like this:
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py

Django, can't create app in subfolder

I'm on windows and trying to create a new app inside a common folder named Apps.
The myApp folder already exists inside Apps. I'm running from the project root:
python manage.py startapp myApp Apps\myApp
and I get:
Error: 'Apps\\myApp' is not a valid app name. Please use only numbers, letters and underscores.
I don't know why that double backslash.
I tried also with a forward slash just to be sure:
python manage.py startapp myApp Apps/myApp
and I get this:
Error: 'myApp' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
I can't understand if it is a Windows or a Python problem.
Try this:
mkdir Apps\newapp
python manage.py startapp NewApp Apps/newapp
And you will to create a app called "NewApp" inside folder "Apps/newapp".
Create your Apps directory from your project's root directory-
mkdir Apps
Move to your Apps directory-
cd Apps
Run python by calling the manage.py in your root project directory-
python ../manage.py startapp newapp
There you go
from the docs:
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.
django-admin.py startapp myapp /Users/jezdez/Code/myapp
So try python manage.py startapp myApp ./Apps/myApp or with the full path.
Manage.py file is a thin wrapper of django-admin.py
In case, you want to create a new app in any directory
Try this:
$ cd <directory path>
$ django-admin.py startapp <app-anme>
For django 3.2.9
Create a sub directory Apps to hold all the apps, move into it, create a directory for the app myApp, then come back to root directory
mkdir Apps && cd Apps && mkdir myApp && cd ..
add a __init__.py file (is a python way to treat a directory as a package)
Create a myApp inside Apps sub directory
manage.py startapp myApp Apps/myApp
(in windows, please use backslash)
above two lines of code will create myApp
Now, in settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Local
'Apps.myApp.apps.MyAppConfig', # new
]
in ./Apps/myApp/apps.py file, change name='myApp' to name='Apps.myApp' will solve the issue
I had the same trouble on my Mac as well. I did solve it upgrading Django from vervion 1.3 to version 1.4.
As the documentation says you can use the command
django-admin startapp name [directory]
but in the example
django-admin startapp myapp /Users/jezdez/myapp
the documentation does not say that python creates the myapp folder. You should do it before the startapp command.

Understanding directory structure advice

I started to create in Django sample project, first command:
django-admin.py startproject test
gives me:
- root
- test
- __init__.py
- settings.py
- urls.py
- wsgi.py
- manage.py
Now I create first app:
python manage.py startapp foo
it created for me folder root/foo
so how I should understand my root/test folder. Is this folder for global config of my project and nothing more? (similar to Symfony 2 app folder)
I am confused because Django docs tells:
The inner mysite/ directory is the actual Python package for your
project
but manage.py startapp foo create app under root, not under root/test (mysite equivalent)
[EDIT]
Two commands:
python manage.py startapp app
and:
django-admin.py startapp app
gives me app inside project root, not under root/name_of_generated_project
Django 1.4
[EDIT] 2
Sorry guys, my fault, now is everything ok.
[EDIT] 3
I want to create another project again:
django-admin.py startproject jobeet
my initial structure is similar to above.
Now I want to try create app (inside jobeet folder):
django-admin.py startapp jobs
and I end up with jobeet/jobs not jobeet/jobeet/jobs
again :/
so inside my project root I have:
- jobeet
- jobs
- manage.py
another command:
python manage.py startapp app
gives me the same result
So let's say you create a new Django project testproject:
django-admin.py startproject testproject
This creates a new project with the following minimal set of files:
testproject/
├── __init__.py
├── manage.py
├── settings.py
└── urls.py
To create a new app for your first site mysite1 go into testproject directory and run:
python manage.py startapp mysite1
which results in a new directory mysite1 for the mysite1 app.
I.e. with just these two commands you would arrive at this hierarchy:
testproject/
├── __init__.py
├── manage.py
├── mysite1
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── settings.py
└── urls.py
Refer to the django-admin.py and/or manage.py individual commands here.
In Django there is a one-to-many relationship between a project and an app. An app is usually one individual site component (e.g. comments, ratings), whereas a project is an organisation of several apps and can power many different sites. That's why you get the sites framework. In practice, one project usually serves one website, but with Django sites app with one project you can serve as many websites as you like, for reusability's sake.
P.S. I think creating a project simply called test is not a good practice because with Django unit tests at app level unit tests will go into a file called tests.py or within a folder called tests.
UPDATE for Django 1.4
As #zeantsoi has commented below, my answer:
applies to Django 1.3 and prior. Per the docs, beginning in 1.4, base
configuration files (including settings.py, urls.py, and wsgi.py) are
abstracted into a subdirectory titled the same name as the project.
"test" is the top level of your project. I've never used symphony 2 so I can't comment on that, but it seems like you have a grasp on it. The files that live in there are basically all global config files. Inside your "test" folder you should also have one or more app folders. Inside these app folders live the apps specific models, views, urls, etc.
It seems you've got something a little wrong your foo app should live in root/test/foo not root/foo.
Now in my projects I tend to have things like a virtualenv folder live in the root dir, but you definitively shouldn't have apps at that level (it just won't work)
manage.py doesn't provide a startproject command - that's usually a django-admin command. I'd check which manage.py you're executing, and ideally, use the manage.py from the project directory you've created.