Saturday, May 2, 2020

How to show a list of users present in the database in the webpage in django....

Project name-djangoTut
app name-Home

My urls.py file of Home app:

#functions to import
from django.urls import path
from home.views import HomeView
from django.conf.urls import url

app_name='home'

urlpatterns = [
path('',HomeView.as_view(),name='home'),

]

My views.py file of Home app:

#functions to import
from django.views.generic import TemplateView
from django.shortcuts import render
from django.contrib.auth.models import User

#class associated with url
class HomeView(TemplateView):
    template_name='home/home.html'
def get(self,request):
  users=User.objects.exclude(id=request.user.id)

args={'users':users}
return render(request,self.template_name,args)



My home/home.html file:


{% comment %} extending the base.html file in ginger templating language 
{% endcomment %}{% extends 'base.html' %}



{% block body %}

<div class="container">


    <h2>Other Peoples</h2>
    {% for user in  users %}
    <h3>{{user.username}}</h3>
    {% endfor %}
    
</div>
{% endblock body %}

For more such stuffs comment below ...

No comments:

Post a Comment

Please do not enter any spam link in the comment box

Should I tell her?

 How do you know when to confess your feelings for someone? No rulebook says this is the right time or the wrong time. However, there are a ...