M
M
ma3xak2018-08-30 07:17:54
Django
ma3xak, 2018-08-30 07:17:54

Can't display Queryset?

Trying to gradually learn python with djnago. Faced difficulties when outputting to a template from a connected model.
URLs

from django.conf.urls import url

from . import views 

urlpatterns = [
  url(r'^$', views.RealtyListView.as_view(), name ='realty'),
  url(r'^(?P<pk>\d+)/$', views.RealtyDeailView.as_view(), name= 'realty-detail'),


]

models
class realty(models.Model):

  ROOMS_CHOICES=(
    ('1', '1 ком'),
    ('2', '2 ком'),
    ('3', '3 ком'),
    ('4', '4 ком'),
    ('st', 'Студия'),
    ('ob', 'Общежитие')
    )

  RA_CHOICES =(
    ('jd', 'Железнодорожный'),
    ('kr', 'Кировский'),
    ('ln', 'Ленинский'),
    ('ok', 'Октябрьский'),
    ('sv', 'Свердловский'),
    ('sov', 'Советский'),
    ('cent', 'Центральный')
    )

  SAN_CHOICES =(
    ('sov', 'Совместный'),
    ('raz', 'Разделённый'),
    )

  adress = models.CharField(verbose_name = "Адресс объекта", blank = False, null = True, default = None, max_length = 150)
  etaj = models.DecimalField(verbose_name = "Этаж", max_digits= 2, decimal_places = 0, blank = False, null = True, default = None)
  ploshad = models.DecimalField(verbose_name = "Площадь", max_digits= 5, decimal_places = 2, blank = False, null = True, default = None)
  ploshad_k = models.DecimalField(verbose_name = "Площадь кухни", max_digits= 5, decimal_places = 2, blank = False, null = True, default = None)
  ploshad_j = models.DecimalField(verbose_name = "Жилая площадь", max_digits= 5, decimal_places = 2, blank = False, null = True, default = None)
  ploshad_b = models.DecimalField(verbose_name = "Полоадь балкона", max_digits= 5, decimal_places = 2, blank = False, null = True, default = None)
  san_usel = models.CharField(verbose_name = "Сан узел", blank = False, null = True, choices = SAN_CHOICES, max_length = 100)
  price = models.DecimalField(verbose_name = "Цена", max_digits= 10, decimal_places = 2, blank = False, null = True, default = None)
  price2 = models.DecimalField(verbose_name = "Цена за квадрат", max_digits= 8, decimal_places = 2, blank = True, null = True, default = None)
  potolok = models.DecimalField(verbose_name = "Высота потолков",  max_digits= 4, decimal_places = 2, blank = False, null = True, default = None)
  rooms = models.CharField(verbose_name = "Количество комнат", blank = False, null = True, default = "1", choices= ROOMS_CHOICES, max_length = 15)
  rayon = models.CharField(verbose_name = "Район", blank = False, null = True, choices = RA_CHOICES, max_length = 100)
  planirovka = models.ForeignKey('planirovka', verbose_name = "Планировка", on_delete = models.CASCADE, default= None)
  realty_type = models.ForeignKey('realty_type', verbose_name = "Тип недживижимости", on_delete = models.CASCADE, default= None)
  realty_status = models.ForeignKey('realty_status', verbose_name = "Статус дома", on_delete = models.CASCADE, default= None)
  otdelka = models.ForeignKey('otdelka', verbose_name = "Отделка", on_delete = models.CASCADE, default= None)
  description = models.TextField(verbose_name = "Описание")

  def __str__(self):
    return "%s" % self.adress
  #Тестим save и пост save
  def save(self, *args, **kwargs):
    self.price2 = self.price /self.ploshad
    self.price2 = round(self.price2, 2)
    super(realty, self).save(*args, **kwargs)

  class Meta:
    verbose_name = 'Квартира'
    verbose_name_plural = 'Квартиры'
  
  def get_absolute_url(self):
    return "/realty/%i/" % self.id



class photos(models.Model):
  image_admin = models.ForeignKey('realty', verbose_name = "Фотографии", on_delete = models.CASCADE, default= None)
  image = models.ImageField(upload_to='imgage_objects')

  class Meta:
    verbose_name = "Фотография"
    verbose_name_plural = "Фотографии"

class planirovka(models.Model):
  title = models.CharField(verbose_name = "Название", blank = False, null = True, default = None, max_length = 150)
  description = models.TextField(verbose_name = "Описание", blank = False, null = True, default = None)

  def __str__(self):
    return "%s" % self.title

  class Meta:
    verbose_name = "Планировка"
    verbose_name_plural = "Планировки"

class otdelka(models.Model):
  title = models.CharField(verbose_name = "Название", blank = False, null = True, default = None, max_length = 150)
  description = models.TextField(verbose_name = "Описание", blank = False, null = True, default = None)

  def __str__(self):
    return "%s" % self.title

  class Meta:
    verbose_name = "Отделка"
    verbose_name_plural = "Отделки" 

class realty_type(models.Model): 
  title = models.CharField(verbose_name = "Название", blank = False, null = True, default = None, max_length = 150)
  description = models.TextField(verbose_name = "Описание", blank = False, null = True, default = None)

  def __str__(self):
    return "%s" % self.title

  class Meta:
    verbose_name = "Тип недвижимости"
    verbose_name_plural = "Типы недживижимости" 



class realty_status(models.Model): 
  title = models.CharField(verbose_name = "Название", blank = False, null = True, default = None, max_length = 150)
  description = models.TextField(verbose_name = "Описание", blank = False, null = True, default = None)

  def __str__(self):
    return "%s" % self.title

  class Meta:
    verbose_name = "Статус дома"
    verbose_name_plural = "Статусы домов"

views
from django.shortcuts import render
from django.views import generic
from realty.models import *
# Create your views here.

##return render(request, 'realty/realty.html', locals())


class RealtyListView(generic.ListView):
  model = realty 
  print('Скрипт отработал')
  def photo(self):
    photo = photos.objects.filter(id = 1)


class RealtyDeailView(generic.DetailView):
  model = realty

Templates
{{realty.id}}
{{type_realty.realty.title}}

{% extends 'base.html' %}
  {% block content %}
    <h1>Выберите свой новый дом!</h1>
      {% if realty_list %}
        <ul>
          {% for realty in realty_list %}
          <a href="{{realty.get_absolute_url}}">{{realty.adress}}</a>
          <img src="" alt="">
          {{p}}
        </ul>
        {% endfor %}
      {% endif %}
  {% endblock %}

I can’t figure out how to pick up data from related models, to display data from the photos model, I made such a request,
image = photos.objects.all()but I can’t figure out how to display this request in a template

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question