Why aren't model records displayed?



  • So while I was fixing one thing, I had a different breakdown, and the fact that I'd worked before and what I had the highest hopes suddenly broke, and the thing is that the code changed without touching the model, that's what led me to the stud, and first I was trying to create a separate class in the box that would be pure for this model and for her record, but it didn't lead me to anything, then I tried to change. python manage.py sqlmigrate Homepage 0001 The modeling table creates, and that's what led to the stud, but it doesn't turn out, maybe I didn't find out that I didn't deny it, and the problem would be, well, it's gonna be easy.

    Here's the view code.py

    class IndexView(generic.ListView):
    template_name = 'Homepage/index.html'
    model = Goods
    context_object_name = 'goods'
    

    def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    numbers = Number.objects.all()
    context['numbers'] = numbers
    return context

    Here's the model code.py.

    class Number(models.Model):
    number1_text = models.CharField(max_length=200)
    number2_text = models.CharField(max_length=200)

    def str(self):
    return self.number1_text + self.number2_text

    Here's the code urls.py.

    from django.urls import path
    from django.conf.urls import include, url
    from . import views
    from django.views.generic import RedirectView
    app_name = 'Homepage'
    urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
    path('Home', views.HomeView.as_view(), name='home'),
    path('sale', views.sale, name='sale'),
    url(r'^favicon.ico$', RedirectView.as_view(url='media/images/logo1.png', permanent=True)),
    ]

    Here's the code xm

    <div class="modal-body">
    {% for number in numbers %}
    <h3 class="numbers">{{number.number1_text}}</h3>
    {% endfor %}

           {% for number in numbers %}
              &lt;h3 class="numbers"&gt;{{numbers.number2_text}}&lt;/h3&gt;
           {% endfor %}
    
          &lt;/div&gt;</code></pre></div></div></p><p>I don't even know what else to, so if you need anything to write, I'll add it.</p>


  • For numbers:

    numbers.number2_text
    

    Must be number:

    number.number2_text
    

    Try the template:

    {% for number in numbers %}
      <h3 class="numbers">{{ number.number1_text }}</h3>
      <h3 class="numbers">{{ number.number2_text }}</h3>
    {% endfor %}
    

    There's no point in redefining. str:

    class Number(models.Model):
        number1_text = models.CharField(max_length=200)
        number2_text = models.CharField(max_length=200)
    

    def str(self):
    return self.number1_text + self.number2_text


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2