Вот моя модель:
from django.db import models
# Create your models here.
class Contact(models.Model):
name = models.CharField(max_length=125, null=True)
email = models.EmailField()
address = models.CharField(max_length=255)
city = models.CharField(max_length=150)
zipcode = models.CharField(max_length=15)
Я пытаюсь отобразить данные в виде:
<div class="row d-block">
<table class="table table-responsive">
<thead>
<tr>
<th>Name:</th>
<th>Email:</th>
<th>Address:</th>
<th>City:</th>
<th>Zipcode:</th>
</tr>
</thead>
<tbody>
{% for row in rows%}
<tr>
<th>{{rows.name}}</th>
<th>{{rows.emai}}</th>
<th>{{rows.address}}</th>
<th>{{rows.city}}</th>
<th>{{rows.zipcode}}</th>
</tr>
{%endfor%}
</tbody>
</table>
Вот моя функция, куда я отправляю данные в БД. Мой следующий шаг - извлечь данные из БД и отобразить их в html:
from django.shortcuts import render
from django.http import HttpResponse
from pages.models import Contact
# from django.views import View
# Create your views here.
def home(request):
return render(request, 'index.html', {'title':'Home Page'})
def contact(request):
if(request.method == 'POST'):
data = Contact(
name = request.POST['name'],
email = request.POST['email'],
address = request.POST['address'],
city = request.POST['city'],
zipcode = request.POST['zipcode'],
)
data.save()
dbdata = Contact.objects.all()
print(dbdata)
return render(request, 'contact.html', {'title':'Contact Page','row':dbdata})
Когда я пытаюсь получить данные из БД, возникает следующая ошибка:
UnboundLocalError at /pages/contact/
local variable 'dbdata' referenced before assignment
Как я могу получить и отобразить мои данные?