Reputation: 3719
I have a django view in which I wish to read a cookie
urls.py:
path('', views.home, name='home'),
views.py:
def home(request):
context = {}
url = 'home/home_page.html'
cookies_allowed = request.COOKIES.get('cookies_allowed', '0')
return render(request, url, context)
models.py:
class HomePage(Page):
template = "home/home_page.html"
banner_title = models.CharField(max_length=100, blank=False, null=True)
But, of course, the render function does not contain any context data and I get a blank page.
How do I get the data defined in the model into context?
Upvotes: 0
Views: 156
Reputation: 1629
views.py
def home(request):
context = {'data': HomePage.objects.all()}
url = 'home/home_page.html'
cookies_allowed = request.COOKIES.get('cookies_allowed', '0')
return render(request, url, context)
home_page.html
{% for obj in data %}
{{obj.banner_title}} <br>
{% endfor %}
Upvotes: 1
Reputation: 1105
views.py:
def home(request):
context = {}
url = 'home/home_page.html'
cookies_allowed = request.COOKIES.get('cookies_allowed', '0')
data = ModelName.objects.all()
context = {
'data':data
}
return render(request, url, context)
models.py:
class ModelName(models.Model):
banner_title = models.CharField(max_length=100, blank=False, null=True)
Look, model is using to define the table field as a model attribute. So there is no template or anything in here.
Upvotes: 0