V
V
Vladimir Timoshko2019-07-13 20:54:12
Django
Vladimir Timoshko, 2019-07-13 20:54:12

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'] # Сортировка по дате(от новых до старых)

Further, after the migration, I got into views.py and directly created the post display function there:
from django.shortcuts import render
from .models import news

def main(request):
    posts = news.objects.all()
    return render(request, 'school/main.html', {'posts': posts})

After that, I opened the template and entered directly the following:
{% 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 %}

When starting the server, an error appears: unindent does not match any outer indentation level ( undefined does not match any outer indentation level, as Google Translate said) ': posts}) in views.py Can you help with this. I may have used methods from legacy Django versions somewhere. Python version: 3.7.4 (latest for now) Django version: 2.2.3 (latest release for now)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2019-07-13
@IIIaTaTeJIb

"unindent does not match any outer indentation level" means an indentation error.

A
Alexey Guest007, 2019-07-13
@Guest007

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 question

Ask a Question

731 491 924 answers to any question