Django Tutorial

Django Create A Complete Project

Django Template

Django Database Query

Django Form

Django Authentication and Permission Management

Django unittest

Django Advanced

Create The First Application in Django Project

In this tutorial, we'll walk you through the process of creating your first application within a Django project.

Prerequisites:

  • Basic knowledge of Python
  • Django installed on your computer
  • A new Django project created

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.

  1. Setting up Django project and app:

    • Description: Start a new Django project and create an app within it to organize your code.
    • Code Example:
      # 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
      
  2. Defining models in the first Django app:

    • Description: Create models to define the data structure of your Django app using Python classes.
    • Code Example:
      # models.py in your app
      from django.db import models
      
      class MyModel(models.Model):
          name = models.CharField(max_length=100)
          description = models.TextField()
      
  3. Configuring URLs in a Django app:

    • Description: Set up URL patterns to map specific URLs to views within your Django app.
    • Code Example:
      # urls.py in your app
      from django.urls import path
      from .views import my_view
      
      urlpatterns = [
          path('my-view/', my_view, name='my_view'),
      ]
      
  4. Adding static files to Django app:

    • Description: Include static files (CSS, JavaScript) in your Django app and configure their usage.
    • Code Example:
      # settings.py in your project
      import os
      
      STATIC_URL = '/static/'
      STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
      
  5. Django migrations in the first app:

    • Description: Use Django migrations to manage database schema changes as you modify your models.
    • Code Example:
      # Create initial migration
      python manage.py makemigrations
      
      # Apply migrations
      python manage.py migrate
      
  6. Creating Django admin interface for the first app:

    • Description: Enable the Django admin interface for managing your app's data through a user-friendly interface.
    • Code Example:
      # admin.py in your app
      from django.contrib import admin
      from .models import MyModel
      
      admin.site.register(MyModel)
      
  7. Testing Django app functionality:

    • Description: Write tests to ensure the correctness and reliability of your Django app's functionality.
    • Code Example:
      # 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')