- Install Django
pip install django
- Create a new project. Replace "myproject" with your project name. It's common to use uppercase letters at the start of each word, such as "BookStore"
python -m django startproject myproject
- Enter the project folder
cd myproject
- Install Virtualenv. Virtualenv allows you to create isolated Python environments. This is especially useful for managing dependencies and avoiding conflicts between packages used in different projects.
pip install virtualenv
- Create a Virtual Environment. Replace "myenv" with the name of your virtual environment. I usually use the name "venv"
python3 -m venv myenv
- Activate the Virtual Environment. This depends on your operating system.
- Windows:
venv\Scripts\activate
- Linux:
source venv/bin/activate
- Now you can install dependencies, and they will be isolated. To use it, just install a dependency.
pip install django
Then you must run:
pip freeze > requirements.txt
With this, you can store all the project dependencies in this file, which is useful for Docker and server deployments.
- Use deactivate to exit the Virtual Environment.
deactivate
- Run the project by executing:
python manage.py runserver
- The basic project will be available at http://127.0.0.1:8000/
- First, we need to create an app. A Django app is a modular component representing a discrete part of a larger project. The app name should be short, all-lowercase, and not include numbers, dashes, periods, spaces, or special characters. It should generally be the plural form of an app's main model.
python3 manage.py startapp appname
- Add the new app to the config/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'appname', <--
]
- Define the model. In appname/models.py, define the model. For example:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)
class Meta:
db_table = 'books'
def __str__(self):
return self.id
- Create forms to manage the CRUD. In appname, create a file named forms.py for the Book model
from django import forms
from .models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['title', 'author', 'description', 'price']
- In appname/views.py, create functions for the CRUD operations, for example:
from django.shortcuts import render, get_object_or_404, redirect
from .models import Book
from .forms import BookForm
def book_list(request):
books = Product.objects.all()
return render(request, 'books/book_list.html', {'books': books})
- Define the URL patterns in a urls.py file within your app:
from django.urls import path
from .views import book_list
urlpatterns = [
path('', book_list, name='book_list'),
]
- Include the app's URLs in the main project. In myproject/urls.py, add:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('books.urls')),
]
- Create the template to add HTML content. Create a folder named templates in the app's directory.
pip install python-decouple
Then, create a ".env" file and define your variables:
DB_HOST=localhost
DB_PORT=3306
DB_NAME=my_database
DB_USER=my_user
DB_PASSWORD=my_password
Import the Decouple package wherever you need it.
from decouple import config
- Install te database.
- Mysql
pip install mysqlclient
- MongoDB
pip install djongo
- Postgress
pip install psycopg2
- Depending on our database, we need to modify the DATABASE configuration in myproject/config.py:
- Mysql:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": config('DB_NAME'),
"USER": config('DB_USER'),
"PASSWORD": config('DB_PASSWORD'),
"HOST": config('DB_HOST'),
"PORT": config('DB_PORT'),
}
}
- Sqlite:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
- MongoDB:
DATABASES = {
'default': {
'ENGINE' : 'djongo',
'NAME' : config('DB_NAME'),
'HOST': config('DB_HOST'),
'PORT': config('DB_PORT'),
'USERNAME': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'MONGO_URI': "mongodb://admin:mypwd@127.0.0.127017/bookstorage"
}
}
- Postgres:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config('DB_NAME'),
"USER": config('DB_USER'),
"PASSWORD": config('DB_PASSWORD'),
"HOST": config('DB_HOST'),
"PORT": config('DB_PORT'),
}
}
- Make a migration to apply changes to the database. MAKE SURE THE DATABASE IS CREATED.
python3 manage.py makemigrations
python3 manage.py migrate
- Clone this repository.
git clone https://github.com/yourusername/your-django-project.git
- Install dependencies.
pip install -r requirements.txt
- Create the .env file and define the following variables:
DB_HOST=localhost
DB_PORT=3306
DB_NAME=my_database
DB_USER=my_user
DB_PASSWORD=my_password
-
Create the database.
-
Run migrations to apply changes to the database.
python3 manage.py makemigrations
python3 manage.py migrate
- Run the project:
python manage.py runserver
Contributions are welcome!