0% found this document useful (0 votes)
27 views

Django Basics

The document outlines the steps to setup a Django project and create a basic todo list application. It includes instructions for initializing a virtual environment, installing Django and other packages, creating an app, adding models, views, URLs, templates, and migrating the database. The todo list app allows users to add, update, delete and mark todos as complete.

Uploaded by

aaa zzz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Django Basics

The document outlines the steps to setup a Django project and create a basic todo list application. It includes instructions for initializing a virtual environment, installing Django and other packages, creating an app, adding models, views, URLs, templates, and migrating the database. The todo list app allows users to add, update, delete and mark todos as complete.

Uploaded by

aaa zzz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Setup

1. python -m venv venv


2. . venv/bin/activate
3. pip install Django

1.

4. pip install pytest-django


5. pip install mypy
6. pip install pip install django-stubs
7. django-admin startproject MotionSnake

1.
8. pip install python-dotenv

1. settings.py

2. .env

9. python manage.py migrate


10. python manage.py runserver
1.

2.

Creating Website
Create an App
python manage.py startapp [name]

Add App

settings.py

Add views

views.py

from django.shortcuts imort render, redirect

from djando.views.decorators.http import require_http_methods

from .models import Todo

# Create views here

def index(request):
todos = Todo.objects.all()

return render(request, 'base.html', {'todo_list': todos})

@require_http_methods(['POST'])

def add(request):

title = request.POST['title']

todo = Todo(title=title)

todo.save()

return redirect('index')

def update(request, todo_id):

todo = Todo.objedcts.get(id=todo_id)

todo.complete = not todo.complete

todo.save()

reuturn redirect('index')

def delete(request, todo_id):

todo = Todo.objedcts.get(id=todo_id)

todo.delete()

reuturn redirect('index')

Add URL

urls.py

from django.urls import path

from . import views

urlpatterns = [

path('', views.index, name='index')

path('add', views.add, name='index'),

path('delete/<int:todo_id>', views.delete, name='delete'),

path('update/<int:todo_id/' views.update, name='update'),

todoapp/urls.py

Add HTML Template

templates/base.html

settings.py

Create DB Model

todolist/models.py

from django.db import models

# Create models here

class Todo(models.Model)

title=models.CharField(max_length=350)

complete=models.BooleanField(default=False)

def __str__(self):

return self.title

Migrate
python manage.py makemigrations

python manage.py migrate

Creating Admin

python manage.py createsuperuser

located at /admin

Register Model

todolist/admin.py
from django.contrib import admin

from .models import Todo

# Regester models here

admin.site.register(Todo)

You might also like