Re: I want to display my restaurant list for which i used a function based view and it is working fine but when i import generic list view then it's not displaying that list

2017-12-21 Thread Matemática A3K
On Thu, Dec 21, 2017 at 3:05 PM, Ruhail Ahmad 
wrote:

> def resturant_listview(request):
> template_name = 'resturants/resturants_list.html'
> queryset = ResturantLocation.objects.all()
> context= {
>"obj_list": queryset
>}
> return render(request,template_name,context)
>
> class ResturantListView(ListView):
> template_name   = 'resturants/resturants_list.html'
> queryset=  ResturantLocation.objects.all()
>
>

> {% block content %}
>
> Resturants List
> 
> {% for obj in obj_list %}
> {{obj.name}} {{obj.location}} {{obj.category}}
> {{obj.timestamp}}
> {% endfor %}
> 
> {% endblock content %}
>
>
It's because ListView makes available the queryset to the template in the
"object_list" variable, and your template is expecting "obj_list" which you
provide in your FBV
https://docs.djangoproject.com/en/2.0/ref/class-based-views/generic-display/#listview

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BFDnh%2BiqLO%3DBXt4wg1Q9v5FoMAGoxnzjZLtihVE8x4JHzAePQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


I want to display my restaurant list for which i used a function based view and it is working fine but when i import generic list view then it's not displaying that list

2017-12-21 Thread Ruhail Ahmad
#VIEWS.PY
from django.db.models import Q
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import TemplateView
from django.views.generic import ListView
from .models import ResturantLocation

# Create your views here.

def resturant_listview(request):
template_name = 'resturants/resturants_list.html'
queryset = ResturantLocation.objects.all()
context= {
   "obj_list": queryset
   }
return render(request,template_name,context)

class ResturantListView(ListView):
template_name   = 'resturants/resturants_list.html'
queryset=  ResturantLocation.objects.all()

#URLS.PY
from django.conf.urls import url
from django.contrib import admin
from django.views.generic import TemplateView, ListView
from resturants.views import (
resturant_listview,
ResturantListView,
)

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', TemplateView.as_view(template_name = 'home.html')),
#url(r'^resturants/$',resturant_listview),
url(r'^resturants/$', ResturantListView.as_view()),
url(r'^resturants/(?P\w+)/$', ResturantListView.as_view()),
url(r'^about/$', TemplateView.as_view(template_name= 'about.html')),
url(r'^contact/$', TemplateView.as_view(template_name = 
'contact.html')),
]

#HTML(resturants_list.html):

{% extends 'base.html' %}

{% block title %}Resturants List{% endblock %}

{% block content %}

Resturants List

{% for obj in obj_list %}
{{obj.name}} {{obj.location}} {{obj.category}} 
{{obj.timestamp}}
{% endfor %}

{% endblock content %}

#MODELS.PY
from django.db import models

# Create your models here.
class ResturantLocation(models.Model):
name= models.CharField(max_length=120)
location= models.CharField(max_length=120,null=True,blank=True)
category= models.CharField(max_length=120,null=True,blank=False)
timestamp   = models.DateTimeField(auto_now_add=True)
update  = models.DateTimeField(auto_now=True)


def __str__(self):
return self.name





-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/d68a7766-0773-49d9-920e-e980cae7c05e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.