Django is a wonderful Python web framework, and its command line utility is indispensable when developing Django sites. However, the command to start new projects is a bit tricky. The official tutorial shows the basic case – how to start a new project from scratch using the command:
$ django-admin startproject [projectname]
This command will create a new directory using the given project name and generate the basic Django files within it. However, project names have strict rules: they may contain only letters, numbers, and underscores. So, the following project name would fail:
$ django-admin startproject my-new-django-project CommandError: 'my-new-django-project' is not a valid project name. Please make sure the name is a valid identifier.
Another problem is initializing a new Django project inside an existing directory:
$ mkdir myproject $ django-admin startproject myproject CommandError: '/path/to/myproject' already exists
These two problems commonly happen when using Git (or other source control systems). The repository may already exist, and its name may have illegal project name characters. The project could be created as a sub-directory within the repository root, but this is not ideal.
Thankfully, there’s a simple solution. The “django-admin startproject” command takes an optional argument after the project name for the project path. This argument sidesteps both problems. The project root directory and the Django project file directory can have different names. The example below shows how to change into the desired root directory and start the project from within it using “.”:
$ cd my-django-git $ django-admin startproject myproject . $ ls manage.py myproject
This can be a stumbling block because it is not documented in Django’s official tutorial. The “django-admin help startproject” command does document the optional directory argument but does not explain when this option is useful. Hopefully, this article makes its use case more intuitive!
It’s going to be finish of mine day, but before finish I am reading this
enormous article to increase my experience.
LikeLike
Thanks .. it saved my hours
LikeLike
Dude, you are the man. This really made my project workflow with all my django projects and github go much smoother now in my quest of becoming a django master.
LikeLike
how can I fix this ? could you help me?
(venv) (base) MacBook-Pro-de-Fabio:Teste toscani$ django-admin startproject teste.
CommandError: ‘teste.’ is not a valid project name. Please make sure the name is a valid identifier.
LikeLike
Remove the period (“.”) from the project name.
LikeLike
The problem is not the “.” but the lack of a space between it and the project name “teste”. By adding the “.” you prevent an extra folder from being made.
LikeLike
Thank you, this was what I was looking for, I just started the django tutorial but already created a directory with the virtualenv in it.
LikeLike
very helpful Thanks for sharing
LikeLike
this was very helpful, thank you
LikeLike
You are the absolute saviour of my sanity today! I just started learning django and for the life of me couldn’t figure out why the official tutorial insists on creating a new folder inside my project folder (I was running `django-admin` from within project directory) with the name of my project.
I am going to try and suggest making the official document clearer.
LikeLike