Welcome to our Django tutorial! In this comprehensive guide, we'll walk you through creating a Django project step-by-step. Django is a powerful, open-source web framework that encourages rapid development and clean, pragmatic design. Let's dive in!
Django is a high-level Python web framework that simplifies the creation of complex web applications. It emphasizes reusability and the DRY (Don't Repeat Yourself) principle.
Before we start, ensure you have Python and Pip installed on your system. To install Django, open your terminal and type:
pip install djangoNow that Django is installed, let's create a new project. Open your terminal and type:
django-admin startproject my_django_projectReplace my_django_project with the name you'd like for your project. This command creates a new Django project with a manage.py file and a project directory that contains an app called my_django_project.
Let's explore the structure of our newly created Django project:
my_django_project/
βββ manage.py
βββ my_django_project/
β βββ __init__.py
β βββ settings.py
β βββ urls.py
β βββ wsgi.py
βββ my_django_project/
βββ __init__.py
βββ migrations/
βββ static/
βββ templates/
βββ apps/
βββ my_app/
βββ __init__.py
βββ migrations/
βββ models.py
βββ tests.py
βββ views.py
βββ admin.py
Navigate to your project directory and start the server using:
cd my_django_project
python manage.py runserverNow, open your browser and visit http://127.0.0.1:8000/. You should see the Django welcome page!
Let's create a new app called my_app within our project:
python manage.py startapp my_appIn your my_django_project/urls.py file, add the following to include the new app:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_app.urls')),
]Now, create a urls.py file in my_app directory and add the following:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]In my_app/views.py, create a simple view for our app's homepage:
from django.http import HttpResponse
def home(request):
return HttpResponse("Welcome to my_app!")Now, if you refresh your browser, you should see "Welcome to my_app!" on the homepage.
What command is used to create a new Django project?
What is the purpose of the `manage.py` file in a Django project?