0

I am trying retrive data from db and display in view but output is empty. Here is my function

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()
  return render(request, 'contact.html',  {'title':'Contact Page','row':dbdata})

Here is my template and display data in this table

  <tbody>

            {% 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%} 
        </tbody>

I am beginner in django.and also please tell me how to debug code in django.

1
  • In view it should be rows: return render(request, 'contact.html', {'title':'Contact Page','rows':dbdata}). Commented Feb 3, 2020 at 10:50

2 Answers 2

4

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

if this is working then let me know....

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

Comments

1

replace follwoing code in view.py [you just made a mistake at row's name ]

make sure your code in proper alignment

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()
    return render(request, 'contact.html',  {'title':'Contact Page','rows':dbdata})

and add following code in HTML

 <tbody>
        {% 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%}
</tbody>

Comments

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.