|
creating a project
|
django-admin startproject mysite . |
|
changing settings
|
edit mysite/settings.py
|
|
database setup
|
python3 manage.py migrate |
|
run the server
|
python3 manage.py runserver |
|
and in browser:
|
|
|
test shit in an interactive console
|
python3 manage.py shell |
|
creating an application
|
python3 manage.py startapp blog |
|
add the app name to
INSTALLED_APPS |
'blog.apps.BlogConfig' |
|
models are kept in models.py
|
|
|
add, edit and delete model in admin.py
|
|
|
getting the model classes
|
from app_name.models import model_name |
|
getting the user model
|
from django.contrib.auth.models import User |
|
see all objects of a certain model
|
Class_name.objects.all() (is iterable) |
|
create an object
|
Class_name.objects.create(attributes) you don't have to save for it to be reflected |
|
accessing the object via attribute
|
Class_name.objects.get(attr="value") |
|
filtering (can return multiple, iterable)
|
Class_name.objects.filter(attr="value") |
|
filtering (contains)
|
Class_name.objects.filter(title__contains="phrase") |
published_date__lte=timezone.now() |
|
|
ordering
|
Class_name.objects.order_by("attribute") "-attr" does the reverse |
|
you can combine ordering and filtering
|
Class_name.objects.does this first.does this second |
|
delete
|
obj.delete() |
|
import
|
from django import forms |
from .models import model_name |
|
login decorator
|
from django.contrib.auth.decorators import login_required |
@login_required |
|
model attribute
|
description
|
args
|
models.CharField |
text with a limited number of characters
|
max_length |
models.TextField |
text field with unlimited content
|
|
models.DateTimeField |
datetime field
|
default, blank, null, auto_now_add |
|
creating a database
|
python3 manage.py migrate |
|
update changes to database
|
python3 manage.py makemigrations app_name |
|
migrate
|
python3 manage.py migrate app_name |
|
register database in app_name/admin.py
|
admin.site.register(Model_name) |
|
register a model
|
admin.site.register(Post) |
|
create superuser
|
python3 manage.py createsuperuser |
|
1. create the api token
|
|
2. new bash console
|
|
3. in bash:
pip3 install --user pythonanywhere |
|
4.
pa_autoconfigure_django.py --python=3.8 repo_link.git |
|
5. to update website, go to your directory and pull code
|
|
6. reload at web
|
|
7.
workon your_directory |
|
8.
python3 manage.py collectstatic |
|
get bootstrap
|
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> |
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css"> |
|
|
add static files
|
make a static folder with css files inside app_name folder
|
{% load static %} |
|
<link rel="stylesheet" href="{% static 'css/blog.css' %}"> |
|
|
create a block
|
{% block content %} |
{% endblock %} |
|
|
block content
|
{% block content %} |
{% endblock %} |
|
|
connect a block to the base
|
{% extends 'blog/base.html' %} |
|
link a url within a site
|
a href="{% url 'url_name' pk=post.pk %}" |
|
where pk stands for primary key
|
|
|
to use the link
|
path('whatever/<int:pk>/', views.view_name, name='*url_name') |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.