
Creating Your First Django Project
Welcome! If you're new to Django, you're in for a treat. Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It’s designed to help you build web applications quickly and with less code. In this guide, I'll walk you through creating your very first Django project from scratch. By the end, you'll have a working project you can expand upon.
Before we begin, make sure you have Python installed on your system. Django requires Python, so if you haven't installed it yet, head over to the official Python website and download the latest version. Once Python is set up, you'll also need pip, Python's package installer, which usually comes bundled with Python.
To install Django, open your command line or terminal and type:
pip install django
This command will download and install the latest version of Django. Once the installation is complete, you can verify it by running:
django-admin --version
You should see the version number printed, confirming that Django is ready to go.
Now, let's create your first Django project. In Django, a project is a collection of settings and configurations for a specific website. Think of it as the container for your entire web application. To start a new project, use the django-admin
command:
django-admin startproject myfirstproject
Replace myfirstproject
with whatever name you prefer for your project. This command will create a new directory with your project name, containing the initial structure of a Django project.
Let's take a quick look at what’s inside your new project folder. You should see something like this:
myfirstproject/
manage.py
myfirstproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
The outer myfirstproject
directory is just a container for your project. The inner one is the actual Python package for your project. manage.py
is a command-line utility that lets you interact with your Django project. You'll use it frequently for tasks like running the development server or creating apps.
Now, navigate into your project directory:
cd myfirstproject
To make sure everything is set up correctly, let's run the development server. Django comes with a lightweight web server that you can use for development and testing. Run the following command:
python manage.py runserver
You should see output indicating that the server is running. Open your web browser and go to http://127.0.0.1:8000/
. You should see a congratulatory page from Django, confirming that your project is working.
Congratulations! You’ve just created and run your first Django project. But a project alone isn't very useful without apps. In Django, an app is a web application that does something – like a blog, a poll, or a contact form. A project can contain multiple apps.
Let’s create your first app. While the development server is running, open a new terminal window, navigate to your project directory, and run:
python manage.py startapp myapp
This command creates a new directory called myapp
with the basic structure of a Django app. You'll see files like models.py
, views.py
, and admin.py
, which you'll use to define your application's data, logic, and administration interface.
Now, you need to tell your project about this new app. Open the settings.py
file inside the inner myfirstproject
directory. Look for the INSTALLED_APPS
list and add your app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp', # Add your app here
]
Saving the file registers your app with the project.
Next, let’s create a simple view. Open myapp/views.py
and add the following code:
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, world! This is my first Django app.")
This function defines a view that returns a simple HTTP response. Now, you need to map this view to a URL. Create a new file inside the myapp
directory called urls.py
and add:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
This sets up a URL pattern that directs the root URL to your home
view. Finally, you need to include these URLs in your project. Open the project's urls.py
file (inside the inner myfirstproject
directory) and modify it to include your app’s URLs:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
Now, if you restart the development server (if it’s not running, use python manage.py runserver
again) and visit http://127.0.0.1:8000/
, you should see your "Hello, world!" message.
You’ve just built a basic Django project with a custom app and view! This is a solid foundation. From here, you can explore databases by defining models, create templates for HTML responses, and even set up an admin interface.
Here’s a quick reference table of the commands we used:
Command | Description |
---|---|
django-admin startproject |
Creates a new Django project |
python manage.py runserver |
Starts the development server |
python manage.py startapp |
Creates a new app within the project |
And here are the key files we modified:
- settings.py: Project settings, including installed apps.
- views.py: Where you define the logic for your views.
- urls.py: URL configurations for both project and app.
Remember, practice makes perfect. Try creating more views, experiment with templates, or dive into the Django documentation to learn about models and databases. Happy coding!
As you continue, you might find these steps helpful:
- Always activate a virtual environment for your projects to manage dependencies.
- Use
python manage.py migrate
to set up your database when you add models. - Explore the Django admin interface by creating a superuser with
python manage.py createsuperuser
.
Django has a lot to offer, and you're now on your way to building powerful web applications. Keep experimenting, and don’t hesitate to look up resources or ask questions in the community. Enjoy your Django journey!