Sunday, January 19, 2020

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

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
    

]
 
               
 


On Mon, Jan 20, 2020 at 12:00 PM nmaxberry@gmail.com <nmaxberry@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.

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!

--
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+unsubscribe@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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABOHK3TaKgxGuR_pAJ05mTsYjKiaNnnZfxXvzU__v%3DAE%2B3o5QQ%40mail.gmail.com.

No comments:

Post a Comment