I'd like to give some advice before start your Django journey and Django project configurations
1. master Python "class" bacause you might use django class-based view a lot, learn "decorator" and standard library "datetime" and "re"
2. choose "Python version" for your django project, I recommend Python 3.10 or 3.11. You should check current installed Python version
in your terminal --> py --version
** if you use Unix OS like MacOS or Linux, type "python3" instead of "py"
3. I highly recommend create virtual environment for each django project, install virtualenv with pip
windows cmd --> py -m pip install virtualenv
then --> py -m virtualenv app-env
macos terminal --> python3 -m pip install virtualenv
then --> python3 -m virtualenv app-env
4. activate virtual environment you just created
windows cmd --> app-env\Scripts\activate.bat or app-env\Scripts\activate
macos terminal --> source/bin/activate
to stop virtualenv just type --> deactivate
5. install django latest version via pip (you must activate virtualenv first)
--> py -m pip install django
---------- Start Django ----------
1. start new django project (don't forget to activate virtualenv first)
--> django-admin startproject my-app
2. go inside project folder and create "requirements.txt" and type all requirement library for your project
--- requirements.txt ---
django >= 4.2.0, < 4.3.0
wheel >= 0.4.0
ipython >= 8.11.0 # python shell for django project
python-decouple >= 3.7.0 # for hiding sensitive data in settings.py before uploading to git
setuptools >= 67.7.0
psycopg >= 3.0.0 # postgresql database driver. if you use django 4.1 or lower you must install psycopg2 instead of psycopg
mysqlclient => 2.1.1 # mysql database driver. if you already use postgresql you don't need this
3. install specified dependencies
--> py -m pip install -r requirements.txt
for upgrade your dependencies --> py -m pip install --upgrade -r requirements.txt
by doing this you have more control to your project dependencies
for example if you specified "django >= 4.2.0, < 4.3.0" it will upgrade django between 4.2.0 to 4.2.* but never 4.3.0
4. in your main project directory it will have 3 files now
my-app, manage.py and requirements.txt
5. open "settings.py" in "my-app"
first thing you need to be cautious is SECRET_KEY constant. You must never lose it or expose this to public that's why "python-decouple" library needed
6. in DATABASE section change it to your database
--- for postgresql ---
DATABASES = {
'default': {
DATABASE_ENGINE='django.db.backends.postgresql'
DATABASE_NAME='mydatabase'
DATABASE_USER='root'
DATABASE_PASSWORD=''
DATABASE_HOST='localhost'
DATABASE_PORT='5432'
}
}
--- for mysql ---
DATABASES = {
'default': {
DATABASE_ENGINE='django.db.backends.mysql'
DATABASE_NAME='mydatabase'
DATABASE_USER='root'
DATABASE_PASSWORD=''
DATABASE_HOST='localhost'
DATABASE_PORT='3306'
}
add your "user" and "password", both database port above are default port though
7. start your first database migrate
--> py manage.py migrate
I hope this help you avoid some problems in the future.