I cannot display the model I created on a different page. It appears on the Model Admin page.
Here models.py
class Bonusrequest(models.Model):
username = models.CharField(max_length=200,verbose_name="Kullanici Adi")
name = models.CharField(max_length=200, verbose_name="Ad Soyad")
created = models.DateTimeField(verbose_name="Tarih")
And views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse,HttpResponseRedirect
from django.template import loader
from django.utils import timezone
import requests
from datetime import datetime
from datetime import timedelta
import json
import time
import pytz
from pytz import timezone
from .models import Bonusrequest
from .apps import Discount
from django.contrib import messages
from django.contrib import auth
from django.contrib.auth import logout
from django.contrib.auth.models import User
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from .models import Bonusrequest
@login_required
def finance(request):
myModel = Bonusrequest.objects.all()
return render (request,'pages/finance.html',{"Bonusrequest":myModel})
And here html page
{% extends 'layoutdash.html'%}
{% load static %}
{% block content %}
<section class="home-section">
<h1 class="c-grey-900 mT-10 mB-30">Test</h1>
<hr>
<table>
<tr>
<th>Name</th>
<th>Username</th>
<th>Country</th>
</tr>
{% for result in Bonusrequest %}
<tr>
<td>{{result.name}}</td>
<td>{{result.username}}</td>
<td>{{result.created}}</td>
</tr>
{% endfor %}
</table>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</section>
{% endblock %}
What I want to do here is to show the values saved in the model on this page. What do you think am I missing? Here is the project details.The problem here is that it cannot be displayed this way on the desired page. In the sources I researched, it was possible to display the model on the desired page by writing a code in this way.
This model table can be displayed in the normal admin template.
CodePudding user response:
I think you may wrong inheritance or wrong block content so remove everything from templates except the following to make sure are the results displayed or not
<section class="home-section">
<h1 class="c-grey-900 mT-10 mB-30">Test</h1>
<hr>
<table>
<tr>
<th>Name</th>
<th>Username</th>
<th>Country</th>
</tr>
{% for result in Bonusrequest %}
<tr>
<td>{{result.name}}</td>
<td>{{result.username}}</td>
<td>{{result.created}}</td>
</tr>
{% endfor %}
</table>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</section>