Django Tutorial
Django Create A Complete Project
Django Template
Django Database Query
Django Form
Django Authentication and Permission Management
Django unittest
Django Advanced
In this tutorial, we'll walk you through the process of creating your first application within a Django project.
Prerequisites:
Step 1: Create the new application
1.1. Open the terminal/command prompt and navigate to the root directory of your Django project.
1.2. Run the following command to create a new application called 'myapp':
python manage.py startapp myapp
This command will generate a new directory named 'myapp' with the following structure:
myapp/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
Step 2: Register the new application in your Django project
2.1. Open your Django project's settings.py
file.
2.2. Locate the INSTALLED_APPS
list and add your new application to the list:
INSTALLED_APPS = [ # ... 'myapp', # ... ]
Step 3: Create a simple view
3.1. In your app's views.py
, define a simple view function that returns an HttpResponse:
from django.http import HttpResponse def hello_world(request): return HttpResponse("Hello, World!")
Step 4: Create a URL pattern for the new view
4.1. In your app's directory, create a new file named urls.py
.
4.2. In the urls.py
file, define the URL patterns for your app:
from django.urls import path from . import views app_name = 'myapp' urlpatterns = [ path('hello_world/', views.hello_world, name='hello_world'), ]
Step 5: Include the app's URLs in the project URLs
5.1. In your project's urls.py
file, include your app's URLs using the include
function:
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('myapp/', include('myapp.urls')), ]
Replace 'myapp/'
with the desired URL prefix for your app, and 'myapp.urls'
with the appropriate path to your app's urls.py
file.
Step 6: Test your new view
6.1. Run your Django development server:
python manage.py runserver
6.2. Visit the URL that corresponds to the hello_world
view, such as http://127.0.0.1:8000/myapp/hello_world/
. You should see the message "Hello, World!" displayed in your browser.
You've now created your first application within a Django project and set up a simple view with its corresponding URL pattern. You can continue building your application by creating more views, models, templates, and other components as needed.
Setting up Django project and app:
# Create a new Django project django-admin startproject myproject # Navigate to the project directory cd myproject # Create a new app within the project python manage.py startapp myapp
Defining models in the first Django app:
# models.py in your app from django.db import models class MyModel(models.Model): name = models.CharField(max_length=100) description = models.TextField()
Configuring URLs in a Django app:
# urls.py in your app from django.urls import path from .views import my_view urlpatterns = [ path('my-view/', my_view, name='my_view'), ]
Adding static files to Django app:
# settings.py in your project import os STATIC_URL = '/static/' STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
Django migrations in the first app:
# Create initial migration python manage.py makemigrations # Apply migrations python manage.py migrate
Creating Django admin interface for the first app:
# admin.py in your app from django.contrib import admin from .models import MyModel admin.site.register(MyModel)
Testing Django app functionality:
# tests.py in your app from django.test import TestCase from .models import MyModel class MyModelTests(TestCase): def test_model_creation(self): my_model = MyModel.objects.create(name='Test', description='Test description') self.assertEqual(my_model.name, 'Test')