OK so I have a running form in Django that updates the model and displays this on the page, but I was hoping to better format it. What happens is that the page displays all data imputed in the form. What I want to do is to numerically list it. This is what I have in my home.html right now:
{% extends 'base.html' %}
{% block body %}
<div class="container">
<h1>Home</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% for post in posts %}
<h2>Object:{{ post.post }}</h2>
{% endfor %}
</div>
{% endblock %}Also here's my view.py file:
from django.shortcuts import render, redirect
from firstapp.forms import IndexForm
from django.views.generic import TemplateView
from firstapp.models import Post
class HomePage(TemplateView):
template_name = 'home/home.html'
def get(self, request):
form = IndexForm()
posts = Post.objects.all()
args = {'form': form, 'posts': posts}
return render(request, self.template_name, args)
def post(self, request):
form = IndexForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user
post.save()
text = form.cleaned_data['post']
form = IndexForm()
return redirect('home:home')
args = {'form': form, 'text': text}
return render(request, self.template_name, args)So say I have data "a", "b", and "c". It would display itself as
Object: a
Object: b
Object: c
If I added d to the form, it would add
Object: d
What I'm hoping to do is add an increment to this so it displays itself as
Object 1: a
Object 2: b
Object 3: c
And add d as
Object 4:
How would I go about implementing this? Another thing I wanted to know is whether I can change the "Post:" comment next to the form. Right now my form displays itself with "Post:" at the left side. Is there a way to edit this?
So say I have data "a", "b", and "c". It would display itself as
Object: a
Object: b
Object: c
If I added d to the form, it would add
Object: d
What I'm hoping to do is add an increment to this so it displays itself as
Object 1: a
Object 2: b
Object 3: c
And add d as
Object 4:
How would I go about implementing this? Another thing I wanted to know is whether I can change the "Post:" comment next to the form. Right now my form displays itself with "Post:" at the left side. Is there a way to edit this?