How to use startproject --template? (Django 2.2) - django

I'm attempting to create a Django templates directory via django-admin startproject and --template.
I've tried: django-admin startproject main_project --template=./templates
However it only creates an empty main_project directory there is nothing reflecting a Django instance in this directory (ie. no urls.py, settings.py, wsgi.py, etc.)
- desktop/
|_ main_project/ # directory where startproject will be executed via CLI
|_ templates/
I'm expecting a project Python package to be created as described here:
https://docs.djangoproject.com/en/2.2/intro/tutorial01/#creating-a-project
mysite/
manage.py
mysite/ # or main_project for my example
__init__.py
settings.py
urls.py
wsgi.py
I'm only getting:
- desktop/
|_ main_project/ # directory where startproject will be executed via CLI
|_ main_project
|_ #empty directory
|_ templates/
|_ venv

As per the docs https://docs.djangoproject.com/en/2.2/ref/django-admin/#cmdoption-startapp-template
Perhaps try the command with the —template flag before the project name

Related

Where should i put django application folder

I have followed several django tutorials on the web.
There is 2 folders: One for django project and another for application. We can have multiple applications for the same project but i my case, i have just one.
Sometimes, the application folder is located at the same level than project folder and sometimes, the application folder is located inside project folder.
I do not know what is the best solution... It works in both case. It is just a state of the art question
Thanks
Mostly it is a matter of choice and the organization of your Project. Even thought, i will post you here a recomented layout and good reasons to choose this
myproject/
manage.py
myproject/
__init__.py
urls.py
wsgi.py
settings/
__init__.py
base.py
dev.py
prod.py
blog/
__init__.py
models.py
managers.py
views.py
urls.py
templates/
blog/
base.html
list.html
detail.html
static/
…
tests/
__init__.py
test_models.py
test_managers.py
test_views.py
users/
__init__.py
models.py
views.py
urls.py
templates/
users/
base.html
list.html
detail.html
static/
…
tests/
__init__.py
test_models.py
test_views.py
static/
css/
…
js/
…
templates/
base.html
index.html
requirements/
base.txt
dev.txt
test.txt
prod.txt
Allows you to pick up, repackage, and reuse individual Django applications for use in other projects
Environment specific settings. This allows to easily see which settings are shared and what is overridden on a per environment basis.
Environment specific PIP requirements
Environment specific PIP requirements
Small more specific test files which are easier to read and understand.

Django 1.4 Deployment and os.environ.setdefault

In the new Django 1.4 project layout, I have a declaration of os.environ.setdefault on manage.py and wsgi.py inside the project folder. What is the difference between the two?
Also, if I have this settings structure:
mysite
|-- mysite
| |-- settings
| |-- base.py
| |-- dev.py
| |-- production.py
| wsgi.py
|-- myapp
|-- manage.py
which os.environ.setdefault should I edit? The one in manage.py or the one in wsgi.py?
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
also, in which file should I detect the current env? and how do I do so?
So manage.py is never executed once deployed, its mainly for development and/or executing commands against your project so its the wsgi.py that you're interested in.
You can check this by looking at the file itself which has: if __name__ == "__main__": that should tell you that the file is meant to be executed directly from command line.

Django directory structure?

I would like to implement a simple queueing service specific to a project. Where should the code go into in the Django directory structure?
Currently the structure is:
sound/
__init__.py
models.py
tests.py
views.py
static
[edit] I am asking where to place the queue service code I created within the direcotry structure above. Should I create a new directory?
Common structures
In Django 1.4+
project_root/
project_name/
media/
static/
some_app/css/app.css # overriding an app css file from project level
css/
project.css
static_root/ # in production using the collectstatic command
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/
__init__.py
base.py # settings common to all instances of the project
dev.py
staging.py
test.py
prod.py
urls.py
some_app/
static/
css/
app.css
templates/some_app/foo.html
urls.py
views.py
models.py
manage.py
In Django 1.3 and prior
project_root/
some_app/
templates/some_app/foo.html
static/
css/
app.css
urls.py
views.py
models.py
media/
static/
some_app/
css/
app.css # overriding an app css file from project level
css/
project.css
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/
__init__.py
base.py # settings common to all instances of the project
dev.py
staging.py
test.py
prod.py
urls.py
manage.py
Alternative approach
project_root/
.gitignore
README.md
docs/
venv/
src/
main/
media/
static/
some_app/css/app.css # overriding an app css file from project level
css/
project.css
static_root/ # in production using the collectstatic command
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/
__init__.py
base.py
dev.py
staging.py
test.py
prod.py
urls.py
some_app/
static/
css/
app.css
templates/some_app/foo.html
urls.py
views.py
models.py
manage.py
wsgi.py
If you need to use the database, you should add the data models to models.py. For your program I recommend writing it in new python files (e.g. queuing.py) that you would import when and where you want to use it.
You could create another django app just for this as well.

Where should i create django apps in django 1.4?

I've just started a new project in django 1.4 and since they've changed the default layout for manage.py and the whole folder hierarchy (see https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py) i cannot decide where should i put my app packages - inside mysite or outside it? What's the best practice? For some reason, startapp command creates the app outside of the mysite package, but this feels somehow wrong.
So, what's the best? This:
manage.py
mysite/
__init__.py
settings.py
urls.py
myapp/
__init__.py
models.py
or this:
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py
?
The second way.
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py

djangorecipe test command

I've got a question about testing my Django applications in a built out Django project.
First, I've got the same project not built out and everything works fine. This project follows the standard Django project architecture apart from putting my tests in their own directory:
django_project/
manage.py
settings.py
urls.py
app1/
models.py
views.py
urls.py
tests/
app2/
...
If I run the tests in this situation great!
This is all a little bit different when it comes to buildout environment. There I've tried to svn check my project and applications and make the paths to all of them available in my bin directory (and I hope that also means making it available for whatever magic djangorecipe is doing). Anyway this is what my buildout looks like:
[buildout]
parts =
django_project
app1
app2
django
extra-paths =
${buildout:directory}
${buildout:directory}/parts
${buildout:directory}/parts/django_project
${buildout:directory}/parts/app1
${buildout:directory}/parts/app2
[django_project]
recipe = infrae.subversion
urls =
https://svn/django_projects/trunk/ .
[app1]
recipe = infrae.subversion
urls
= https://svn/path/app1/trunk/ .
[app2]
recipe = infrae.subversion
urls =
https://svn/path/app2/trunk/ .
[django]
recipe = djangorecipe
version = 1.2
project = django_project
projectegg = django_project
wsgi=true
settings=settings
extra-paths = ${buildout:extra-paths}
test =
app1
app2
When I run the buildout I get the following directory tree.
django_buidout/
...
bin/
django
django.wsgi
test
parts/
django/
django_project/
__init__.py
settings.py
...
app1/
setup.py
app1/
__init__.py
tests/
app2/
setup.py
app2/
__init__.py
tests/
I can get to the django shell so that works. But if I run ./bin/test I get an 'ImportError: No module named django.project.urls'. (the dot notation is not a mistake my django_project.settings.URL_CONF=django_project.urls) This is interesting because if I start the shell I can import django_project.urls. In addition to that if I run the tests through the ./bin/django test app1 all the test run. app1 is interesting because it has no tests on views.
In the views tests I'm using urlresolvers.reverse and that also shows up in the traceback along with the django.tests Client() class.
Is there something in my architecture that is messed up, or is urlresolvers.reverse doing something I'm not aware of?
Many thanks,
Todd