This content originally appeared on DEV Community and was authored by kihuni
Setting up Django involves a series of steps to get the framework installed, configured, and ready for development. Here’s a detailed guide:
Prerequisites
- Python Installation
Ensure Python is installed on your system. Django is a Python-based framework, so you need Python 3.6 or higher.
- Check Installation:
Run python --version
in your command line to check your Python version.
- Download and Install:
If Python is not installed, download it from python.org and follow the installation instructions for your operating system.
Installing Django
- Using pip
pip
is the Python package installer, used to install Django and other packages.
- Install pip:
Most Python installations include pip. Verify by running:
pip --version
- Creating Virtual environment:
To create a virtual environment, decide upon a directory where you want to place it and run the venv module
as a script with the directory path:
python3 -m venv venv
Activating virtual environment:
On Windows, run:
venv\Scripts\activate
On Unix or MacOS, run:
source venv/bin/activate
- Install Django:
Run pip install django
to install the latest version of Django.
pip install django
Creating a Django Project
- Project Initialization
Django projects contain all settings and configurations for your website.
Use django-admin startproject projectname
command to create a new project. Replace projectname with your desired project name.
- Project Structure:
The command creates a directory structure that includes:
- manage.py:
A command-line utility for interacting with your project.
A directory named projectname containing:
init.py:
An empty file that indicates this directory is a Python package.
- settings.py:
Configuration settings for your project.
- urls.py:
URL declarations for your project.
- wsgi.py:
An entry-point for WSGI-compatible web servers.
- asgi.py:
An entry-point for ASGI-compatible web servers.
projectname/
manage.py
projectname/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Running the Development Server
- Starting the Server
Django includes a lightweight web server for development purposes.
- Command:
Navigate to your project directory and run:
python manage.py runserver
You’ll see the following output on the command line:
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
June 09, 2024 - 15:50:53
Django version 5.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
- Accessing the Server:
Open a web browser and go to http://127.0.0.1:8000/
. You should see the Django welcome page.
Create New App
To create your app, make sure you’re in the same directory as manage.py and type this command:
$ python manage.py startapp newApp
Replace the newApp
name with your desired App name
That’ll create a directory NewApp. which is laid out like this:
newApp/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
Configuring the New App
- Adding to INSTALLED_APPS
Include your new app in the project settings to make Django aware of it.
- Edit settings.py:
Add 'appname', to the INSTALLED_APPS list in settings.py.
INSTALLED_APPS = [
'newApp',
]
Database Setup
- Default Database
Django uses SQLite by default, which is suitable for development and testing.
- Settings:
The database settings are located in settings.py. No additional configuration is required for SQLite.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
- Using Other Databases
For production, you might use databases like PostgreSQL or MySQL.
- Install Database Adapter:
Use pip install psycopg2
for PostgreSQL or pip install mysqlclient
for MySQL.
- Update settings.py:
Modify the DATABASES setting with the appropriate configuration for your database.
Applying Migrations
- Database Schema
Migrations are Django’s way of propagating changes to your models (adding a field, deleting a model, etc.) into your database schema.
- Create Migrations:
Run python manage.py makemigrations
to create new migrations based on the changes you made to your models.
python manage.py makemigrations
Migrations for 'newApp':
posts/migrations/0001_initial.py
- Create model newModel
- Apply Migrations:
Run python manage.py migrate
to apply the migrations and synchronize the database state with your models.
python manage.py migrate
Apply all migrations: admin, auth, contenttypes, newApp, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying posts.0001_initial... OK
Applying sessions.0001_initial... OK
Creating a Superuser
- Admin Interface Access
The Django admin interface allows for easy management of site content.
- Command:
Run
python manage.py createsuperuser
and follow the prompts to create an administrative user.
python manage.py createsuperuser
Username (leave blank to use 'virus'):
Email address:
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
Running the Development Server Again
- Start Server
With the initial setup complete, start the development server again to ensure everything is working.
- Command:
Run python manage.py runserver
and verify that your project and apps are functioning correctly by visiting http://127.0.0.1:8000/.
python manage.py runserver
Thank you for reading this far. Stay tuned for a glimpse into application Configuration!
References
Django Documentation: docs.djangoproject.com
Django Project Website: djangoproject.com
Python Official Website: python.org
This content originally appeared on DEV Community and was authored by kihuni
kihuni | Sciencx (2024-06-21T18:19:27+00:00) Comprehensive Guide to Setting Up Django. Retrieved from https://www.scien.cx/2024/06/21/comprehensive-guide-to-setting-up-django/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.