Skip to content

Commit a72aed0

Browse files
Merge pull request #263 from realpython/django-flashcards-app
Add Django Flashcards App project files
2 parents 87a4437 + 469647e commit a72aed0

File tree

137 files changed

+2707
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+2707
-0
lines changed

django-flashcards-app/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Build a Flashcards App With Django
2+
3+
Follow the [step-by-step instructions](https://realpython.com/django-flashcards-app/) on Real Python to build your own flashcards app with Django.
4+
5+
## Setup
6+
7+
You can run the provided example project on your local machine by following the steps outlined below.
8+
9+
Create a new virtual environment:
10+
11+
```bash
12+
$ python3 -m venv venv
13+
```
14+
15+
Activate the virtual environment:
16+
17+
```bash
18+
$ source venv/bin/activate
19+
```
20+
21+
Navigate to the folder for the step you're currently on.
22+
23+
Install the dependencies for this project if you haven't installed them yet:
24+
25+
```bash
26+
(venv) $ python -m pip install -r requirements.txt
27+
```
28+
29+
Make and apply the migrations for the project to build your local database:
30+
31+
```bash
32+
(venv) $ python manage.py makemigrations
33+
(venv) $ python manage.py migrate
34+
```
35+
36+
Run the Django development server:
37+
38+
```bash
39+
(venv) $ python manage.py runserver
40+
```
41+
42+
Navigate to `http://localhost:8000/` to see your flashcards app in action.

django-flashcards-app/source_code_final/cards/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin # noqa: F401
2+
3+
# Register your models here.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class CardsConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "cards"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django import forms
2+
3+
4+
class CardCheckForm(forms.Form):
5+
card_id = forms.IntegerField(required=True)
6+
solved = forms.BooleanField(required=False)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 4.0.4 on 2022-06-03 16:10
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Card',
16+
fields=[
17+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('question', models.CharField(max_length=100)),
19+
('answer', models.CharField(max_length=100)),
20+
('box', models.IntegerField(choices=[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)], default=1)),
21+
('date_created', models.DateTimeField(auto_now_add=True)),
22+
],
23+
),
24+
]

django-flashcards-app/source_code_final/cards/migrations/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from django.db import models
2+
3+
NUM_BOXES = 5
4+
BOXES = range(1, NUM_BOXES + 1)
5+
6+
7+
class Card(models.Model):
8+
question = models.CharField(max_length=100)
9+
answer = models.CharField(max_length=100)
10+
box = models.IntegerField(
11+
choices=zip(BOXES, BOXES),
12+
default=BOXES[0],
13+
)
14+
date_created = models.DateTimeField(auto_now_add=True)
15+
16+
def __str__(self):
17+
return self.question
18+
19+
def move(self, solved):
20+
new_box = self.box + 1 if solved else BOXES[0]
21+
22+
if new_box in BOXES:
23+
self.box = new_box
24+
self.save()
25+
26+
return self
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<title>Flashcards</title>
6+
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
7+
</head>
8+
9+
<style>
10+
/* Custom CSS */
11+
12+
:root {
13+
--accent: #6e48ad;
14+
--accent-bg: white;
15+
--bg: #fff;
16+
--text: #212121;
17+
--text-light: #585858;
18+
--border: #d8dae1;
19+
--code: #d81b60;
20+
--preformatted: #444;
21+
}
22+
23+
article {
24+
background-color: var(--marked);
25+
border: 1px solid var(--border);
26+
border-radius: 5px;
27+
padding: 1em;
28+
margin: 1em 0;
29+
}
30+
31+
h2 {
32+
text-align: center;
33+
}
34+
35+
h2, h4 {
36+
margin-top: 0;
37+
}
38+
39+
hr {
40+
background: var(--accent);
41+
border-color: var(--accent);
42+
}
43+
44+
button,
45+
a[role="button"] {
46+
color: white;
47+
text-decoration: none;
48+
}
49+
50+
input, select, textarea {
51+
width: 100%;
52+
}
53+
</style>
54+
55+
<body>
56+
<header>
57+
<h1>Flashcards App</h1>
58+
{% include "cards/navigation.html" %}
59+
</header>
60+
<main>
61+
{% block content %}
62+
<h2>Welcome to your Flashcards app!</h2>
63+
{% endblock content %}
64+
</main>
65+
</body>
66+
67+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% extends "cards/base.html" %}
2+
{% load humanize %}
3+
4+
{% block content %}
5+
<h2>🗃 {{ box_number | ordinal }} Box</h2>
6+
<mark>{{ object_list | length }}</mark> Card{{ object_list | pluralize }} left in box.
7+
<hr/>
8+
{% if check_card %}
9+
{% include "cards/card.html" with card=check_card %}
10+
{% endif %}
11+
{% endblock content %}

0 commit comments

Comments
 (0)