Cheatography
https://cheatography.com
django basics from django girls blog
setup
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 |
|
apps
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 |
django querysets
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 |
|
forms
import |
|
|
from .models import model_name
|
security
login decorator |
from django.contrib.auth.decorators import login_required
|
|
|
|
|
model attributes
model attribute |
description |
args |
|
text with a limited number of characters |
|
|
text field with unlimited content |
|
datetime field |
default, blank, null, auto_now_add
|
databases
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)
|
admin
register a model |
admin.site.register(Post)
|
create superuser |
python3 manage.py createsuperuser
|
pythonanywhere deployment
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
|
basic styling
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 |
|
|
|
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
|
create a block |
|
|
|
block content |
|
|
|
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')
|
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets