Setting Up Your First Django Project from Scratch
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It simplifies building robust and scalable web applications by providing built-in features like ORM, templates, and authentication.
How to Install Django?
To get started, you'll need Python installed on your machine. Use the following steps to install Django:
- Ensure Python and pip are installed. Run
python --version
andpip --version
in your terminal. - Create a virtual environment to isolate your project dependencies:
python -m venv myenv
- Activate the virtual environment:
# On Windows myenv\Scripts\activate # On macOS/Linux source myenv/bin/activate
- Install Django using pip:
pip install django
How to Create a Django Project?
Once Django is installed, you can create a new project by running the following command:
django-admin startproject myproject
This will create a directory called myproject
containing the necessary files for a Django project:
manage.py
: A command-line tool to interact with the project.myproject/
: The main project folder containing configuration files.
How to Run the Development Server?
Navigate to the project directory and start the development server to see your project in action:
cd myproject
python manage.py runserver
Open a web browser and go to http://127.0.0.1:8000. You should see the Django welcome page, indicating your project is running successfully.
How to Create an App in Django?
Django projects are organized into apps. To create your first app, run:
python manage.py startapp myapp
This will create a folder named myapp
with necessary files for the app. Don't forget to register the app in your project settings by adding it to the INSTALLED_APPS
list in settings.py
:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp', # Add this line
]
What’s Next?
From here, you can start building your app by defining models, views, and templates. Explore Django’s documentation to dive deeper into each feature and build a fully functional web application!