Creating Your First Django App Explained Simply

Welcome to our beginner’s guide on creating your first Django app! If you’re new to web development and looking to build your own web applications, Django is a powerful framework that can help you get started. In this post, we’ll walk you through the process of creating your first Django app in a simple and easy-to-understand way.

What is Django?
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It follows the MVC (Model-View-Controller) pattern, which helps organize your code and keep it maintainable. Django comes with a built-in admin panel, authentication system, and ORM (Object-Relational Mapping) to interact with your database.

Setting up your Django project
To get started with Django, you’ll first need to install it on your machine. You can do this by running the following command in your terminal:

“`
pip install django
“`

Next, you can create a new Django project by running:

“`
django-admin startproject myproject
“`

This will create a new directory called `myproject` with the following structure:

“`
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
wsgi.py
“`

Creating your first Django app
Now that you have set up your Django project, it’s time to create your first Django app. To do this, run the following command in your terminal:

“`
python manage.py startapp myapp
“`

This will create a new directory called `myapp` within your project directory. Inside the `myapp` directory, you’ll find the following files:

“`
myapp/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
“`

Defining your models
In Django, models are used to define the structure of your database tables. Open the `models.py` file in your `myapp` directory and define your models using Django’s ORM. For example, you can create a simple model called `Post` like this:

“`python
from django.db import models

class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
“`

Registering your models in the admin panel
Django comes with a built-in admin panel that allows you to manage your database records easily. To register your `Post` model in the admin panel, open the `admin.py` file in your `myapp` directory and register your model like this:

“`python
from django.contrib import admin
from .models import Post

admin.site.register(Post)
“`

Creating your views
Views in Django are used to handle incoming web requests and return responses to the client. Open the `views.py` file in your `myapp` directory and define a simple view to display a list of posts like this:

“`python
from django.shortcuts import render
from .models import Post

def post_list(request):
posts = Post.objects.all()
return render(request, ‘myapp/post_list.html’, {‘posts’: posts})
“`

Creating your templates
Templates in Django are used to generate dynamic HTML content. Create a new directory called `templates` within your `myapp` directory and create a new HTML file called `post_list.html`. In this file, you can display a list of posts like this:

“`html



Post List

Post List

    {% for post in posts %}

  • {{ post.title }}
  • {% endfor %}



“`

Mapping your URLs
Finally, you’ll need to map your views to URLs so that users can access your web application. Open the `urls.py` file in your `myapp` directory and define a URL pattern to route requests to your `post_list` view like this:

“`python
from django.urls import path
from . import views

urlpatterns = [
path(”, views.post_list, name=’post_list’),
]
“`

That’s it! You’ve successfully created your first Django app. To run your Django development server, navigate to your project directory in the terminal and run the following command:

“`
python manage.py runserver
“`

You can now access your Django app in your web browser by visiting `http://localhost:8000/`. Congratulations on taking your first step into the world of Django web development!