Django Cards Flashcards
Learn Django syntax
Docstring for class Topic
”"”A topic the user is learning about”””
def __str__(self):
Return a string representation of the model
Text field data type 50 characters
models.CharField( max_length=50, blank=False, null=False )
Text field for data type long
models.TextField()
Date field with default sysdate
models.DateTimeField(auto_now_add=True)
Foreigh Key Constraint
models.ForeighnKey(Topic, on_delete=models.CASCADE)
Models Wagtail class Meta:
verbose_name & verbose_name_plural
Define url for application
app_name = ‘learning_logs’
Add learning_logs urls.py to main urls.py
path(‘’, include(‘learning_logs.urls’))
Add link to topics page (pass topic_id)
path(‘topics//’, views.topic, name=’topic’)
function to return index.html
def index(request): """The home page for Learning Log.""" title = "Home Page" context = {'title': title} return render(request, 'learning_logs/index.html', context)
In views.py
import syntax for models and forms
from .models import Topic
from .forms import TopicForm, EntryForm
Get single row from table Topic
Topic.objects.get(id=2)
Get topics orderd by name
Topic.objects.order_by(‘name’)
Syntax in admin.py to register Topic table
admin.site.register(Topic)