Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include template displaying a form

What I want to do is include a form from a separate template at the bottom of a given page, lets say; "example.com/listdataandform/".

The form-template "form.html" displays the form as it should when the view is included in the URLConf. So I can view with "example.com/form/"

What I have so far goes something like this:

{% extends "base/base.html" %}

{% block title %} page title {% endblock %}
{% block content %}
<h2>some "scene" data</h2>
<ul>
{% for scene in scenes %}
    <li>{{ scene.scene }} - {{ scene.date }}</li>
{% endfor %}
</ul>

{% include "tasks/form.html"%}

{% endblock %}

The code inside "block content" works as it should, since it is defined with it's corresponding view for the url "example.com/listdataandform/".

{% include "tasks/form.html"%}: This only displays the submit button from form.html, as expected. I realize by only doing this: {% include "tasks/form.html"%}, the corresponding view method is never executed to provide the "form"-template with data.

Is there any way to this without having to define the view to a specific pattern in urls.py, so that the form can be used without going to the that specified URL..?

So I guess the more general question is; how to include templates and provide them with data generated from a view?

Thanks.

like image 462
sjiffa Avatar asked Nov 13 '13 10:11

sjiffa


People also ask

How can you build a template to display a form?

Click on Content → Dynamic Data Lists. Open the configuration menu (click the vertical ellipsis in the portlet's title bar), then click Manage Data Definitions. Find your data definition in the list, then click the Actions button and choose Manage Templates. Now click the Add button and choose Add Display Template.

What is a form or template?

A Template is the master version of your digital document and every copy you fill out is called a Form. Perhaps the most important thing to understand in GoFormz is the distinction between a Form and a Template. Think of a Template as a master version of your form.

How to design a form in django?

Creating a form in Django is completely similar to creating a model, one needs to specify what fields would exist in the form and of what type. For example, to input, a registration form one might need First Name (CharField), Roll Number (IntegerField), and so on.

What is the form in django?

The Django Form class (A ModelForm maps a model class's fields to HTML form <input> elements via a Form ; this is what the Django admin is based upon.) A form's fields are themselves classes; they manage form data and perform validation when a form is submitted.


1 Answers

For occasions like this, where I have something that needs to be included on every (or almost every) page, I use a custom context processor, which I then add to the TEMPLATE_CONTEXT_PROCESSORS in settings.py. You can add your form to the context by using this method.

Example: common.py (this goes in the same folder as settings.py)

from myapp.forms import MyForm

def context(request):
    c = {}
    c['myform'] = MyForm()

    return c

You can also do any processing required for the form here.

Then add it in your settings.py file: settings.py

.
.
TEMPLATE_CONTEXT_PROCESSORS = (
    '''
    All the processors that are already there
    '''
    "myproject.common.context",
)
.
.
like image 96
hellsgate Avatar answered Sep 29 '22 02:09

hellsgate