0

Here is my model:

  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)

I am trying to display data in a view:

<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>

Here is my function where I send data in the db. My next step is to retrieve the data from db and display it in 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})

When I try to retrieve data from db, the following error occurs:

UnboundLocalError at /pages/contact/
local variable 'dbdata' referenced before assignment

How can I retrieve and display my data?

2
  • 1
    it should be {{ row.name }} not {{ rows.name }} Commented Feb 3, 2020 at 9:22
  • Does this answer your question? display data from db in django Commented Feb 4, 2020 at 14:54

2 Answers 2

3

There is a mistake in passing context.. you are using key row in contex.But you are calling rows in for loop.change key row to rows . and keep for loop as it is.

here change in passing context dictinary key

return render(request, 'contact.html',  {'title':'Contact Page','rows':dbdata})

please let me know if it is workin or not..

Sign up to request clarification or add additional context in comments.

Comments

1

You should move dbdata declaration out of if(request.method == 'POST'): block:

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})

Otherwise in case of GET request block of code inside if statement never reached and you'll see this error.

Also in template you should replace rows with row inside for loop:

{% for row in rows%}
    <tr>
        <th>{{row.name}}</th> 
        <th>{{row.emai}}</th>
        <th>{{row.address}}</th>
        <th>{{row.city}}</th>
        <th>{{row.zipcode}}</th>
    </tr>
{%endfor%}

1 Comment

And instead of <th>{{rows.###}}</th> use <th>{{row.###}}</th>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.