A Wedge of Django: Covers Python 3.8 and Django 3.x
We also get some points from this book…
- Terminology “Wire in the URL”, meaning to associate the Django view with a web location on the site.
- Python List: a list consists of comma-separated values wrapped in square brackets
['Abc', 2, 'Fine', 3, 4]
- Python Dictionary: A set of key: value pairs
{
'a1': 'Model A',
'B3': 'Color 1',
'coffee': 'cafe',
}
<p>{{ my_statement }}</p>
The curly braces around my_statement mean that it won’t be displayed on the page as-is.
It’ll be evaluated on the Python side and then the result will be displayed. It can be any of the following:- A Python expression, e.g., string, number, or instantiated object
- A function or method call
- Create a PostgreSQL database and add the database configuration in config\settings\base.py
Setting the environment variable: DATABASE_URL environment variable
DATABASES = {
# Raises ImproperlyConfigured Exception
# if DATABASE_URL Not in os.environ
"default": env.db(
"DATABASE_URL", default="postgres://db_username:[email protected]:5432/db_name",
)
} -
<p>{{ object.bio|linebreaksbr }}<p>
The “|” or “pipe” symbol in a template is a “filter”. Filters are used to modify variables in Django Templates.
The “linebreakbr” filter modifies text to replace every carriage return with the HTML <br> tag in teh page being rendered. - assert key, is used when debugging code. It lets you test if a condition in your code
returns True, if not, the program will raise an AssertionError.
x = 1
#if condition returns True, then nothing happens:
assert x == 1
#if condition returns False, AssertionError is raised:
assert x == 2
- It is a common practice in the Django world to keep all our Django apps inside one directory.
- A good rule of thumb is to use TextField rather than CharField whenever there might be a need for more than 255 characters.
- Don’t Use list_editable.
As records are tracked not by primary keys but by their position in a displayed list. - CBV (Class-based View)
from django.http import HttpResponse
from django.views.generic import View
class MyView(View):
def get(self, request, *args, **kwargs):
return HttpResponse('Response to GET request')
def post(self, request, *args, **kwargs):
return HttpResponse('Response to POST request')
def delete(self, request, *args, **kwargs):
return HttpResponse('Response to DELETE request')
FBV (Function-based View)
def my_view(request, *args, **kwargs):
if request.method == 'POST':
return HttpResponse('Response POST request')
elif request.method == 'DELETE':
return HttpResponse('Response DELETE request')
return HttpResponse('Response GET request')
or this FBV approach:
def my_view(request, *args, **kwargs):
METHOD_DISPATCH = {
'POST': HttpResponse('Response POST request'),
'DELETE': HttpResponse('Response DELETE request'),
}
DEFAULT = HttpResponse('Response GET request')
return METHOD_DISPATCH.get(request.method, DEFAULT)
- HTTP methods used by clients (web browsers) to access application servers (Django).
GET: Used to read web pages
POST: Used to submit forms
DELETE: Used with an API to delete a resource such as a web page - In a DetailView’s template, the object is accessible as the lowercased model name.
- get_FOO_display() is a utility method created automatically for any Django model field.
- We set blank=True for the optional field. But we don’t set null=True because Django’s convention is to store empty values as the empty string, and to retrieve NULL/empty values as the empty string.
To be continued…
Readings:
บล็อกของ phyblas
Qiita (@phyblas)
django-braces’s documentation
Class-based View
What is a REPL?
How to configure your Django project for multiple environments?
Deploy A Django Project
Deploying a Django application in Windows with Apache and mod_wsgi
How to Remove Services in Windows 10
Anaconda + Django + Apache Webserver
How to run Django on Apache using Windows 10, Virtualenv Python and mod_wsgi
Manage your Python Virtual Environment with Conda
Deploying Django on Windows Server 2019 and Apache with MOD_WSGI