Blog with Django (Codemy.com) - 1~3 Setting up/ Class based Views/ Bootstrap

이다연·2021년 3월 6일
0

Django

목록 보기
3/33

Add name for cascading

admin
personal info: add name

  • CASCADE:
    if we delete the user, blog post under that name will be deleted altogher. cascade used in the model to prevent having a post under removed author.

models.py

from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    title = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
  
    body = models.TextField()

    def __str__(self):
        return self.title + ' | ' + str(self.author)
    

__str__

  • __str__: return self.title + ' | ' + str(self.author)
    on our admin page, object will be shown with recognizeable text.

Class-based Views

Docs: class-based views

(inside project folder )

Home: ListView

1. views

  • ListView: will allow us to list querysets into the DB, do a query set for us. Look up all the records in the DB and bring them back
    so we can list on our webpage. e.g. All blog posts
from django.views.generic import ListView, DetailView
from .models import Post

class HomeView(ListView):
    model = Post
    template_name = 'home.html'
    
    
# old views
# def home(request):
#     return render(request, 'home.html', {})
    

2. urls

  • import the view, class based views needs
    .as_view()
from django.urls import path
from .views import HomeView

urlpatterns = [
    path('', HomeView.as_view(), name="home"),

]

3. templates

  • {% for post in object_list %}
    we didn't send context from view, but we can get hold of data with 'object_list' at template

  • li tag lives inside ul
    notice {{ post.author.first_name }} brings registered name

<h1> Post </h1>
<ul>
{% for post in object_list %}
    <li>{{ post.title }} - 
    {{ post.author.first_name }}{{ post.author.last_name }} <br/>
        {{ post.body }}</li>

{% endfor %}
</ul>

Article: DetailView

DetailView: brings back one record. e.g. One blog post.

1. views

class ArticleDetailView(DetailView):
    model = Post
    template_name = 'article_details.html'

2. urls

path('article/<int:pk>', 
	ArticleDetailView.as_view(), name="article-detail"),

3. templates

  • {% for post in object_list %}
    we didn't send context from view, but we can get hold of data with 'object_list' at template

  • {% url 'article-detail' post.pk %} created a link to each 'article_details' pages

home.html

<h1> Post </h1>
<ul>
{% for post in object_list %}
    <li><a href="{% url 'article-detail' post.pk %}">
    {{ post.title }}</a> -
    {{ post.author.first_name }}{{ post.author.last_name }} <br/>
        {{ post.body }}</li>

{% endfor %}
</ul>

passing post or object???

article_details.html

<h1>{{ post.title }}</h1>
<small>By: {{ post.author.first_name }}
    {{ post.author.last_name }}</small><br>
    <hr>
    </br>
    {{ post.body }}
<br/><br/>
<a href="{% url 'home' %}"> Back </a>

Bootstrap

  • Bootstrap starter template
  • base.html
  • Button: add btn class into a href

-Bootstrap example

<button type="button" class="btn btn-secondary">Back</button>

-Applied to project

<a href="{% url 'home' %}" class="btn btn-secondary> Back </a>
  • container pushes everything over.

    	div class="container"
  • navbar
    inside body tag

    {% url 'home' %} -> same as usr_for'function_name'
a class="navbar-brand" href="{% url 'home' %}">Blog</a>

  • title_tag
    for title on each article page
    models.py
class Post(models.Model):
    title_tag = models.CharField(
    max_length=255, default='Hello Blog') 

article_details.html

{% block title %} {{ post.title_tag }} {% endblock %}

SEO meta tags -> same method!

profile
Dayeon Lee | Django & Python Web Developer

0개의 댓글