Need help formatting and incrementing a form in HTML

33 views
Skip to first unread message

Zev

unread,
Sep 25, 2017, 4:16:04 PM9/25/17
to Django users

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?


Daniel Roseman

unread,
Sep 26, 2017, 6:58:13 AM9/26/17
to Django users
On Monday, 25 September 2017 21:16:04 UTC+1, Zev wrote:

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?



I'm not sure what you're asking. If you want to enumerate objects within a for loop in the template, just use `{{ forloop.counter }}`. 
--
DR. 
Reply all
Reply to author
Forward
0 new messages