@BickDem
Школьник, yчy python

Can only concatenate str (not "NoneType") to str в Джанго, в чем ошибка?

Код Viеws
from django.shortcuts import render
from django.http import HttpResponse

from .models import Board
# Create your views here.
def index(request):
    s = 'СМР Блог\r\n\r\n\r\n'
    for b in Board.objects.order_by('-published'):
        s += b.title + '\r\n' + b.content + '\r\n'
        return HttpResponse(s, content_type = 'text/plain; charset = utf-8')

Код Urls
from django.urls import path

from .views import index

urlpatterns = [
    path('board/', index)#, name = 'index')
]

Код Modеls
from django.db import models

# Create your models here.
class Board(models.Model):
    title = models.CharField(max_length = 120)
    content = models.TextField(max_length = 6000)
    published = models.DateTimeField(auto_now_add = True, db_index = True)

Ошибка
TypeError: can only concatenate str (not "NoneType") to str

В чем проблема? Бyдy благодарен ответам
  • Вопрос задан
  • 1698 просмотров
Пригласить эксперта
Ответы на вопрос 2
@galaxy
В БД есть board, у которых title или content - null?
Ответ написан
@Realmixer
Full stack Python (Django) web-developer
Трейсбэк наверняка всё рассказал. Скорее всего здесь проблема:
s += b.title + '\r\n' + b.content + '\r\n'
В b.title или в b.content вместо строки None. Используй f-строки:
s += f'{b.title}\r\n{b.content}\r\n'
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы