Getting Started with Django and Basic Configuration
Django is a powerful and efficient Python web framework that follows the “Don’t Repeat Yourself” (DRY) principle, enabling developers to quickly build high-quality web applications. Today, we’ll explore Django’s basic configuration and core concepts!
Django installation and project creation
First, make sure you have a Python environment installed, then install Django via pip:
pip install django
Creating a new project is very simple:
django-admin start project myproject
This will generate a Django project structure named `myproject` in the current directory:
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Detailed basic configuration
settings.py is the core configuration file for a Django project. Let’s take a look at some key settings:
# Application Register
INSTALLED_APPS=[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp', # your application name
]
# Database Config
DATABASES={
'default':{
'ENGINE':'django.db.backends.sqlite3',
'NAME':BASE_DIR/'db.sqlite3',
}
}
# Static File Config
STATIC_URL='/static/'
STATICFILES_DIRS=[BASE_DIR/"static"]
Create an application
A Django project consists of multiple applications. To create a new application:
python manage.py start app myapp
Each application has its own `models.py`, `views.py`, and `urls.py` files, implementing the MVC architecture.
Running the development server
Start the development server to view your project:
python manage.py run server
You can see Django’s welcome page by visiting `http://127.0.0.1:8000/`.