Answer the question
In order to leave comments, you need to log in
Django server won't start (Database related)?
I want to add to my server the display of posts from the database through views, but for some reason it doesn’t work.
In models.py, I created a class that will be where I will add posts.
It looks like this:
class news(models.Model):
title = models.CharField(max_length=120, db_index=True)
post = models.TextField(db_index=True)
date = models.DateTimeField(auto_now_add=True)
image = models.ImageField(upload_to='main/static/images/load', blank=True)
def __str__(self):
return self.title
class Meta:
ordering = ['-date'] # Сортировка по дате(от новых до старых)
from django.shortcuts import render
from .models import news
def main(request):
posts = news.objects.all()
return render(request, 'school/main.html', {'posts': posts})
{% for post in posts %}
<div class="novosti">
<h3 align="center">{{post.title|safe}}</h3>
<p style="margin-left: 0.75%;">{{post.post|safe}}</p>
{% if post.image %}
<img src="{{post.image.url}}" class="img">
{% endif %}
<p align="right" class="date">{{post.date|date:"d-m-Y"}}<br></p>
</div>
{% endfor %}
Answer the question
In order to leave comments, you need to log in
"unindent does not match any outer indentation level" means an indentation error.
extra space before return
1) Use a normal editor/IDE for Python - indentation is formed correctly there
2) Give the full error traceback
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question