Skip to content

Instantly share code, notes, and snippets.

View aliceridgway's full-sized avatar

Alice Ridgway aliceridgway

View GitHub Profile
@aliceridgway
aliceridgway / forms.py
Created September 27, 2020 09:43
How to use the user object in Django forms. Used when choices in a multi-select-field are unique to a given user.
from django import forms
from .models import Meal
from core.models import Member
class CustomModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, member):
return "%s" % member.name
@aliceridgway
aliceridgway / forms.py
Last active June 25, 2022 17:19
How to pass the request object to a form class. Used when form choices are specific to a particular user
class CustomModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, member):
""" Customises the labels for checkboxes"""
return "%s" % member.name
class CreateMealForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
""" Grants access to the request object so that only members of the current user
are given as options"""
@aliceridgway
aliceridgway / urls.py
Last active June 25, 2022 17:31
Django: How to automatically log in users after registration
from django.urls import include, path
from . import views
urlpatterns = [
path('signup', views.SignUp.as_view(), name='signup'),
]
@aliceridgway
aliceridgway / test_demo.py
Created March 28, 2021 12:53
A simple Django test
from django.test import TestCase
def add_two_numbers(a, b):
return a + b
class TestExample(TestCase):
def test_add_two_numbers(self):
@aliceridgway
aliceridgway / base.html
Created October 23, 2021 15:14
Django Todo App - Base Template
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To Do</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
@aliceridgway
aliceridgway / index.html
Created October 23, 2021 15:22
Django Todo App - index.html - no links
{% extends 'base.html' %}
{% block content %}
<div class="jumbotron">
<div class="container">
<h1>My To Do List</h1>
<p>{{todos|length}} items</p>
<button type="button" class="btn btn-primary">Add New</button>
</div>
@aliceridgway
aliceridgway / views.py
Created October 23, 2021 15:27
Django Todo App - views.py - list todos
from django.shortcuts import render
from .models import Todo
# Create your views here.
def list_todos(request):
""" Displays a list of posts """
todos = Todo.objects.filter(completed=False).order_by('-created_on')
@aliceridgway
aliceridgway / models.py
Created October 23, 2021 15:45
Django Todo App - models.py Todo
from django.db import models
class Todo(models.Model):
title = models.CharField(max_length=255)
notes = models.TextField(blank=True, null=True)
created_on = models.DateTimeField(auto_now_add=True)
completed = models.BooleanField(default=False)
def __str__(self):
@aliceridgway
aliceridgway / tests.py
Created October 23, 2021 15:48
Django Todo App - model tests
from django.test import TestCase
from .models import Todo
# Create your tests here.
todo_title = "Book Dentist Appointment"
class TestTodoModel(TestCase):
def setUp(self):
@aliceridgway
aliceridgway / tests.py
Last active October 23, 2021 16:39
Django Todo App - tests.py (model and list_todos view)
from django.test import TestCase, Client
from django.urls import reverse
from .models import Todo
# Create your tests here.
todo_title = "Book Dentist Appointment"
class TestTodoModel(TestCase):