Written on
·
3.3K
0
MTV 구조 맛보기: model, views, templates 사용하기 - 수강 중입니다.
블로그 페이지 만들고 html이랑 urls.py 연동하는 과정에서 저런 에러가 생겼는데 어떻게 해야 할까요?
대충 읽어보니까 models.py에서 object = ??? 를 추가해야 하는 것 같은데 정확히 뭘 추가해야 할까요?
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog</title>
</head>
<body>
<h1>Blog</h1>
{% for p in posts %}
<h3>{{ p }}</h3>
{% endfor %}
</body>
</html>
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
created = models.DateTimeField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return '{} :: {}'.format(self.title, self.author)
Answer 4
0
0
0
views.py 입니다
from django.shortcuts import render
from .models import Post
# Create your views here.
def index(request):
posts = Post.object.all()
return render(
request,
'blog/index.html',
{
'posts': posts,
}
)
0