How to render data from populated database (no user input) to html

26 views
Skip to first unread message

nmax...@gmail.com

unread,
Jan 20, 2020, 1:29:01 AM1/20/20
to Django users
I would like to render data from a populated database into a HTML (homepage_view).  I am not sure if I create a <form> GET method from the database (and if so how) or use (fix) the code listed below. 

The ultimate outcome is to have a table with three columns (models.py) with six rows.  Note:  In the home.html (below), I did hardcode the headers but tried a "for loop" to populate the data.

I am using Python 3.8, Django 2.2, Windows 10, sqlite3 and bootstrap.

Please any help would be truly appreciated.

Steps I have taken:

Relevant settings.py

DATABASES = {
    'default': {
        'ENGINE''django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3')
    },
        
    'currRep' : {
        'NAME''rep2.db',
        'ENGINE''django.db.backends.sqlite3',
    },

    'mainData' : {
        'ENGINE' : 'django.db.backends.sqlite3',
        'NAME' : 'KentuckyProject.db',
    },

    
}


Models.py
from django.db import models
class currRep(models.Model):
    Congressional_District = models.IntegerField()
    f_name = models.TextField()
    l_name = models.TextField()

    def __str__(self):
        return f'{self.Congressional_District} {self.f_name} {self.l_name}'

INSTALLED_APPS = [
    'accounts',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

urls.py (app)
from django.urls import path, include
from django.contrib import admin
from django.contrib.auth import login
from django.conf.urls.static import static


from . import views


urlpatterns = [
    path('', views.homepage_view),
    

]


Views.py
from django.shortcuts import render
from django.http import HttpResponse
def currentReps_view(request):
    currResults = currRep.objects.all()
        
    return render(request, 'home.html', currResults)

Home.html
{% extends "base.html" %}
from . import forms
from . import views
{% block currentReps %}
            
       <table class = "table curr_Table">
            <thead>
                <tr>
                    <th scope="col">Congressional_District</th>
                    <th scope="col">f_name</th>
                    <th scope="col">l_name</th>
                    
                </tr>
            </thead>
            <tbody>
                <tr>
            {% for item in currReps %}
                    <td>{{ item.Congressional_District }}</td>
                    <td>{{ item.f_name }}</td>
                    <td>{{ item.l_name }}</td>
                    
                </tr>
            {% endfor %}
             
            </tbody>   
        </table>
        {% endblock currentReps %}

Again, any assistance would be appreciated.  Thanks!

maninder singh Kumar

unread,
Jan 20, 2020, 1:56:16 AM1/20/20
to django...@googlegroups.com
Your path is as follows :
urlpatterns = [
    path('', views.homepage_view),

but your view file is def currentReps_view(request)
Make the change in the urls.py
    

]
 
               
 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d1f9303c-0c6e-4a7c-b3d5-77de8260f0d5%40googlegroups.com.

Suraj

unread,
Jan 28, 2020, 4:51:33 AM1/28/20
to Django users
On Monday, January 20, 2020 at 11:59:01 AM UTC+5:30, nmax...@gmail.com wrote:
I would like to render data from a populated database into a HTML (homepage_view).  I am not sure if I create a <form> GET method from the database (and if so how) or use (fix) the code listed below. 

The ultimate outcome is to have a table with three columns (models.py) with six rows.  Note:  In the home.html (below), I did hardcode the headers but tried a "for loop" to populate the data. 

I am using Python 3.8, Django 2.2, Windows 10, sqlite3 and bootstrap.

Please any help would be truly appreciated.

You can use django-tables2 to create tables from model data.
It is very simple by using django-tables2.

-- 
Suraj

maninder singh Kumar

unread,
Jan 28, 2020, 10:57:32 AM1/28/20
to django...@googlegroups.com
<form method = "get">
{% csrf_token %}
<input type = "submit", name = "q">

views
------

formView (request):
     if request.method == "GET" and 'q' in request.method:
          ---------
         your view code
        -----------
        return render(request, template_name, context = {'text' : database_text, ... . ., ., , ,}

HTML (use bootstrap if possible)
-------
<div class = "container'>
     <div class = "row">
          <div class = "col-sm4">
               Your columns for row 1
         <
/div>
          <div class = "col-sm4">
          </div>
          <div class = "col-sm4">
          </div>
      </div>
and another row here

if you work it out on say : text = Content.objects.filter("your query")

then you could loop based on this in your HTML:

{% for t in text %}
{{t.content_Content}}
{% endfor %}


Hope it helps !

regards


 
               
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages