Cp Verma Cp Verma - 7 months ago 9
Python Question

How to show all user list on template in django

I want to show registered users/company name list to template in django. i am new in working with django forms.
I had registered successfully company by using template now i want to show all the company name which has been registered.
my models.py

class Company(models.Model):
company_id=models.IntegerField(default=0,primary_key=True)
company_name=models.CharField(max_length=255,blank=True,null=True)
company_email=models.EmailField(max_length=255,blank=True,null=True)
is_active=models.BooleanField(default=False)
last_login=models.DateTimeField(default=datetime.now,blank=True,null=True)


i am stuck that how can i show all information on template using forms. how can i make form for this and how can i get all companies name to show

I wrote this to get all companies name

def allcompanies(request):
context = RequestContext(request)
if request.method =='GET':
all_company=comoanyForm(request.GET)

data=Company.objects.all()
print "print all data",data

else:
pass

return render_to_response(
'allcompany.html',
{ },
context_instance=RequestContext(request))


i get values in data but did not showing on template.

Answer

You should not be using render_to_response - it is likely to be depreciated in the future. You should be using render.

   from django.shortcuts import render

   return render(request, 'allcompany.html', {'data': data,})

then within your template you should have something like:

{% for company in data %}
    {{company.company_name}}
{% endfor %}

You should do the django tutorial (or refer to it) if you haven't already, these are simple concepts.

Comments