Reputation: 555
I have a problem where I can't use my Django variables inside Html This is my code : models.py
from django.db import models
from django.urls import reverse
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
created = models.DateTimeField(auto_now_add=True)
content = models.TextField(default="---")
H_price = models.IntegerField(default=0)
L_price = models.IntegerField(default=0)
remaining = models.IntegerField(default=0)
original_price = models.IntegerField(default=0)
Ended = models.BooleanField(default=False)
Published = models.BooleanField(default=True)
class Meta:
ordering = ['-created']
def __unicode__(self):
return u'%s'% self.title
def get_absolute_url(self):
return reverse('Products.views.post', args=[self.slug])
Views.py
from django.shortcuts import render
from .models import Post
# Create your views here.
def index(request):
posts=Post.objects.all()
return render(request, 'Index.html', {"Posts": posts})
def post(request):
return
Index.html
<h1>This is just a title </h1>
{% for post in posts %}
<div>
<h3> {{ post.title }}</h3>
<h3> {{ post.content }}</h3>
</div>
{% endfor %}
I know this isn't the best way to do Html but the goal is just to get it to work then I will style it with css and make everything Look Clean When i run the server i only get "this is just a title" Any suggestions to help me fix it will be apreciated Note that I am a begginer in django
Upvotes: 1
Views: 28
Reputation: 308899
Variables in the Django template language are case sensitive. You use {% for post in posts %}
in your template, therefore you need to use posts
not Posts
in your view.
return render(request, 'Index.html', {"posts": posts})
Upvotes: 1