1) Create model classes
2) Create forms for each model class and specify the attributes that can be edited
3) Create the view
4) create the template
1) Using get_queryset method in Class based views
class PostListView(ListView):
model = models.Post
#method to grab the list and order them
def get_queryset(self):
# __lte implies less than or equal to
#grab all posts less than current time and order by date in decending order (-published_date)
#published_date is an attribute in Post
return models.Post.objects.filter(published_date__lte = timezone.now()).order_by('-published_date')
#translates to SQL Command
# SELECT * FROM Post WHERE pub_date <= '2006-01-01';
Django Db query documentation (Falls under Field lookups)
How python does this evaluation of argument names at run time? See keyword arguments
2) Using bootstrap glyphicons (not available in bootstrap 4) / fontawsome
<!-- bootstrap icon (glyphicon) -->
<!-- ref : https://www.w3schools.com/icons/fontawesome_icons_webapp.asp -->
<span class="fa fa-user" aria-hidden="true"></span></a>
3) Cool CSS classes from codepen
4) Ordering list of objects in html template tagging
<!-- sorting post.comments.all ref : https://stackoverflow.com/questions/6540032/sorting-related-items-in-a-django-template --> {% for comment in post.comments.all|dictsort:"create_date" %} (ascending) {% for comment in post.comments.all|dictsortreversed:"create_date" %} (descending)
5) No Reverse match error always has to do with the URL
6) Using auth login and logout use auth.LoginView and LogoutView
from django.contrib.auth import views
urlpatterns = [
path('accounts/login',views.LoginView.as_view(),name='login'),
path('accounts/logout',views.LogoutView.as_view(),name='logout'),
]
the redirect path after logout can be specified as
<a class="nav-item nav-link" href="{% url 'logout' %}?next={% url 'post_list' %}">Log out</a>For login we can specify in settings.py
LOGIN_REDIRECT_URL = '/path_to_the_page' LOGIN_URL = '/path_to_the_page'