Create a Django Project Crash to Build

Install Django

pip install django

Create a new Django project.

django-admin startproject myproject
cd myproject

Create a new app

python startapp myapp

Modify the file in your myapp folder.

# myapp/
from django.db import models

class MyModel(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()

    def __str__(self):
        return self.title

Apply migrations and create the database.

python makemigrations
python migrate

Create a simple view in myapp/

# myapp/
from django.shortcuts import render
from .models import MyModel

def index(request):
    my_objects = MyModel.objects.all()
    return render(request, 'myapp/index.html', {'my_objects': my_objects})

Create a template in myapp/templates/myapp/index.html.

<!-- myapp/templates/myapp/index.html -->
<!DOCTYPE html>
    <title>My Django App</title>
    <h1>Welcome to My Django App</h1>
        {% for obj in my_objects %}
            <li>{{ obj.title }} - {{ obj.content }}</li>
        {% endfor %}

Configure the URLs

Configure the URLs in myapp/

# myapp/
from django.urls import path
from .views import index

urlpatterns = [
    path('', index, name='index'),

Include the app's URLs in the project's

# myproject/
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('myapp.urls')),

Run the development server:

python runserver

Now, you can visit http://localhost:8000 in your browser and see the simple Django app in action.

Similar Articles