01 - Project setup Flashcards

1
Q

create new django project

A

django-admin startproject monthly_challenges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

create a django app

A

python3 manage.py startapp challenges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

run django server

A

python3 manage.py runserver

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what are views

A

the logic that is executed for different URLs, code handles requests and return responses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what are URLs

A

Route mappings to specific action that ensure specific result when specific URL is accessed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

sample index action

A

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
return HttpResponse(“This works!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

sample url to index action

A

from django.urls import path
from . import views

urlpatterns = [
path(“january”, views.index)
]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

template folder structure

A

/challenges/templates/challenges/challenge.html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

render a template from challenges app

A

def monthly_challenge(request, month):
try:
challenge_text = monthly_challenges[month]
return render(request, “challenges/challenge.html”)
except:
return HttpResponseNotFound(“<h1>This month is not supported</h1>”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

passing values from view action to template

A

from django.shortcuts import render

def monthly_challenge(request, month):
	try:
		 challenge_text = monthly_challenges[month]
		 return render(request, "challenges/challenge.html", {
				 "text": challenge_text
		 })
	except:
		return HttpResponseNotFound("<h1>This month is not supported</h1>")
		
		
//challenges/templates/challenges/challenge.html

<body>
	<h1>This month's Challenge</h1>
	<h2>{{text}}</h2>
</body>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

adding filters to view files

A

//challenges/templates/challenges/challenge.html

<body>
	<h1>{{month_name | title}</h1>
	<h2>{{text}}</h2>
</body>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

using the for tag

A

<ul>
{% for month in months %}
<li><a></a></li>
{% endfor %}
</ul>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

using the URL tag

A

//index.html

<ul>
{% for month in months %}
<li><a>{{ month| title}}</a></li>
{% endfor %}
</ul>

//index.html

<ul>
{% for month in months %}
<li><a>{{ month| title}}</a></li>
{% endfor %}
</ul>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

using if for conditional rendering

A

<body>
<h1>{{ month_name|title }} Challenge</h1>
{% if text is not None %}
<h2>{{ text }}</h2>
{% else %}
<p>There is no challenge for this month</p>
{% endif %}
</body>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

using template inheritance

A

/templates/layout.html | base.html

<head>
<title>{% block page_title %} my challenges {% endblock %}</title>
</head>

<body>
{% block content %} {% endblock%}

</body>

//index.html

{% extends “base.html”%}

//monthly_challenges/settings.py
TEMPLATES = [
BASE-DIR / “templates”
]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

including partials

A

/templates/challenges/includes/header.html

//challenge.html
{% include “challenges/includes/header.html” %}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

sample 404 template

A

monthly_challenges/templates/404.html

{% extends “base.html” %}

{% block page_title %}
Something went wrong - we could not find that page!
{% endblock %}

//challenges/views.py

from django.template.loader import render_to_string

render_to_string("404.html")

//challenges/views.py

from django.http import Http404

raise Http404() //looks for 404 template

//settings.py
DEBUG = False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

adding static files

A

/challenges/static
/challenge/challenges.css

/settings.py

INSTALLED_APPS = [

]

STATIC_URL = ‘/static/’

//inex.html

{% load static %}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

adding global static files

A

./static/
styles.css
import google font link
add css for font-family and more css

load static in base.html file

//settings.py
STATICFILES_DIRS = [
	
]
20
Q

adding static css

A

./static/app.css

./blog/static/blog/index.css

21
Q

adding images as static files

A

./blog/static/blog/images

<img src=”{% static “blog/images/max.png” %}” alt=”Max - The Author of This Blog” />

22
Q

create simple Book model

A

from django.db import models

class Book(models.Model):
title = models.CharField(max_length=50)
rating = models.IntegerField()

23
Q

creating and running migrations

A

$python3 manage.py makemigrations

$python3 manage.py migrate

24
Q

insert data from the shell

A

python3 manage.py shell
from book_outlet.models import book
harry_potter = Book(title=”Harry Potter 1”, rating=5)
harry_potter.save()

25
Q

get all book entries

A

Book.objects.all();

26
Q

updating models and migrations

A

from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator

class Book(models.Model):
title = models.CharField(max_length=50)
rating = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(5)])
author = models.CharField(null=True, max_length=100)

//overrides any Book query in string format
def __str__(self):
return f”{self.title} ({self.rating})”

27
Q

blank vs null

A
28
Q

update book data

A

harry_potter = Book.objects.all()[0]
harry_potter.author = “J.K. Rowling”
harry_potter.save()

29
Q

delete a book

A

harry_potter = Book.objects.all()[0]
harry_potter.delete()

30
Q

using the create method

A

Book.objects.create(title=”title”, rating=5, …)

31
Q

sample querying and filtering

A

Book.objects.get(id=3) // returns single record, error if multiple returned
Book.objects.filter(is_bestselling=True) // returns multiple
Book.objects.filter(rating__lte=4, title__contains=”story”)

32
Q

using OR condition

A

import django.db.models import Q

Book.objects.filter(Q(rating__lt=4) | Q(is_bestselling=True))

33
Q

query performance optimization

A
34
Q

get_object_or_404

A
35
Q

adding slugfield and overriding save

A
36
Q

aggregating and ordering

A

Book.objects.all().order_by(“-title”)// descending

37
Q

creating a superuser

A

python3 manage.py createsuperuser

38
Q

adding models to the admin area

A

admin.site.register(Book)

39
Q

add options for a model in admin

A

class BookAdmin(admin.ModelAdmin):
prepopulated_fields = {“slug”: {“title”, }}
list_filter = {“author”, “rating”}
list_display: {“title”, “author”}

40
Q

adding a one to many Book - Author relationship

A
41
Q

filter books by author first_name

A
42
Q

adding a one-to-one relationship

A
43
Q

adding a many-to-many relationship

A
44
Q
A
45
Q
A