Monday, September 25, 2017

Need help formatting and incrementing a form in HTML

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?


--
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+unsubscribe@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/a51ddef9-d540-4d8a-8c10-822f3f16921b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment