DJANGO
Django is a high-level Python Web-Development framework. In this post, we will learn how to set up our first Django project with MySql database.
Contents
- Virtual Environment
- Install Django
- MySql Setup
- Create Application
- Other Setting
Virtual Environment
Virtual Environment is an extremely useful tool that helps us to keep our coding environment tidy on our computer. Let's create the virtual environment.
Create a directory for your project
Create a directory for your project
mkdir Django
cd Django
Install a virtual environmentpython3 -m venv your_virtual_env_name
Working with the virtual environment
source your_virtual_env_name/bin/activate
Install Django
Before installing Django make sure that you are inside your virtual environment.
your_virtual_env_name$
Install Django
pip install django~=1.11.0
Create Project
django-admin startproject your_site_name .
MySql Set Up
Open your_site_name/setting.py and find DATABASES and replace code inside this withDATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_db_name',
'USER': 'root',
'PASSWORD': 'your_mysql_db_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
Create a database in MySQL with the same name as in setting.py above and run the below commandpython manage.py makemigrations
python manage.py migrate
python manage.py runserver
Create Application
python manage.py startapp app_name
you also need to tell Django that it should use it. We do that in the setting.py file. Find INSTALLED_APPS and add your application name in it.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'your_application_name',
]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'your_application_name',
]Other Setting
Static File Setting
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/'Time Zone Setting
TIME_ZONE = 'Asia/Kolkata'
LANGUAGE_CODE = 'en-us'
Comments
Post a Comment