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

已查看 26 次
跳至第一个未读帖子

nmax...@gmail.com

未读,
2020年1月20日 01:29:012020/1/20
收件人 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

未读,
2020年1月20日 01:56:162020/1/20
收件人 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

未读,
2020年1月28日 04:51:332020/1/28
收件人 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

未读,
2020年1月28日 10:57:322020/1/28
收件人 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.
回复全部
回复作者
转发
0 个新帖子