Inheriting template with a form into an existing template

6 views
Skip to first unread message

DF

unread,
Jul 3, 2012, 12:46:33 PM7/3/12
to django...@googlegroups.com
I have a simple note taking part of my application I'm trying to converge into one template. Currently, I have one template and view that renders and posts the form and another that displays the results. I'd like to amalgamate these into one where the form exists and the results are displayed on the same page.

This seems to be something that be achieved with template inheritance, but I'm not sure how to alter the views so that both of these results can appear in the same template. I've looked around for answers but can't seem to find what I need to achieve this. Any help that could also inform me on how to properly achieve this moving forward would be appreciated. This is one of those areas I've been anticipating learning but it's been more difficult that I assumed. The essential code is posted below:

Views for submitting and displaying notes:

@login_required
def submit_note(request):
    if request.method == 'POST':
        notes_form = NotesForm(request.POST, request.FILES)
        if notes_form.is_valid():
            new_note = notes_form.save(commit=False)
            new_note.author = request.user
            new_note.save()
            return HttpResponseRedirect( "" )
    else:
        notes_form = NotesForm()
    return render_to_response("write_note/notes.html", {'form': notes_form}, context_instance=RequestContext(request))

@login_required
def all_notes(request):
    all_notes = Notes.objects.all().order_by('-date')
    paginate = paginated_stories(request, all_notes)
    return render_to_response("report/notes.html",
                               {'notes': paginate},
                                context_instance=RequestContext(request))

Current basic templates to submit and display:

{% extends 'base.html' %}
{% block page_title %}Notebook{% endblock %}
{% block headline %}Notebook{% endblock %}
{% block content %}
<h2>Write and save notes, quotes and other story info</h2>
<div class="row">
<div class="span8">

<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Submit" />
</form>
</div>

</div>
{% endblock %}

{% extends 'base.html' %}
{% block page_title %}Notebook{% endblock %}
{% block headline %}Notebook{% endblock %}

{% block content %}
<div class="row">
<div class="span4">
{% if notes %}
{% for note in user.notes.all reversed %}
<h3>{{ note.title }}</h3>
<p>{{ note.date }}</p>
<p>{{ note.copy }}</p>
{% endfor %}
{% else %}
<p>You have no notes stored.</p>
{% endif %}
</div>
</div>
{% endblock content %}

And the two urls:

    url(r'^write_note/$', 'stentorian.report.views.submit_note', name='write_note'),
    url(r'^notes/', 'stentorian.report.views.all_notes', name='all_notes')

Melvyn Sopacua

unread,
Jul 4, 2012, 10:57:19 AM7/4/12
to django...@googlegroups.com
On 3-7-2012 18:46, DF wrote:
> I have a simple note taking part of my application I'm trying to converge
> into one template. Currently, I have one template and view that renders and
> posts the form and another that displays the results. I'd like to
> amalgamate these into one where the form exists and the results are
> displayed on the same page.
>
> This seems to be something that be achieved with template inheritance,

Why? Sisters are family, but don't inherit from each other and this is a
sister relationship. The only thing they have in common is the stuff
surrounding the data. One has a form.as_p the other is a loop based on
different variable. You're going to end up with more if statements than
code saved.
What you can do, is create a shared parent that does the page_title,
headline and so on for you, so that you only have to provide the content
block. So this would be a base_notes.html which extends base.html and
has an empty content block.
--
Melvyn Sopacua


Reply all
Reply to author
Forward
0 new messages