Dec. 19, 2024
Django is a high-level Python web framework that allows developers to build robust and scalable web applications. Starting your first Django project might seem daunting, but this guide simplifies the process and helps you get up and running in no time.
Before starting, ensure you have Python installed on your machine. Install Django using pip:
To verify the installation, run:
A Django project serves as the configuration hub for your entire application. To create a project, use the following command:
This creates a folder structure as follows:
manage.py
: A command-line utility for managing the project.settings.py
: Configurations like database, installed apps, and middleware.urls.py
: URL routing for your application.Navigate into the project folder:
Start the Django development server to see if everything is working:
Visit http://127.0.0.1:8000 in your browser. You should see the default Django welcome page.
In Django, an app is a web component that performs a specific function. To create an app named blog
, run:
This creates the following structure:
To include your app in the project, register it in INSTALLED_APPS
in settings.py
:
Open blog/views.py
and define your first view:
To make the view accessible, add a URL mapping. Create a new file blog/urls.py
and add:
Now, connect this app's URLs to the main project. In myproject/urls.py
, include the blog
URLs:
Start the server again:
Visit http://127.0.0.1:8000 in your browser. You should see:
"Welcome to my first Django app!"
Instead of returning plain text, let’s use an HTML template. Create a templates
folder inside blog
and add a file named home.html
:
Content of home.html
:
Update the home
view in views.py
to render the template:
Refresh your browser to see the styled HTML page.
Congratulations! You've created your first Django project and app. Here’s what you can explore next:
This guide introduced the basics of creating a Django project and app. With Django’s robust framework, you can now start building dynamic and feature-rich web applications. Keep experimenting and exploring Django’s features to enhance your development skills!