anthony pizarro
anthony pizarro

Reputation: 145

Django template and views functions

How can I pass a list of items into a template?

In my views:

from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from django.template import loader
from django.views.decorators.csrf import csrf_exempt
from .models import Question
from .forms import usersData1, usersData2, usersData3

def start_pr(response):
    name = ["Name","Phone Number","Email"]
    form = usersData1()
    return render_to_response(response, "start_pr/start.html", {"form":form}, {"name":name})

In my HTML:

{% block content %}
<form  action="/start/" method="post">
    <legend>
        {%for items in form %}
            {% for obj in name %}
                <p> {{ obj }} </p>
            {% endfor %}
            <input type="text" name="name" maxlength="20" required="" id="id_name" placeholder="{{items.name}}">
            <br>
        {% endfor %}
        <a href="#">next</a>
    </legend>
</form>
{% endblock %}

In my forms:

from Django import forms

class usersData1(forms.Form):
    name= forms.CharField(label="name", max_length=20)
    phoneNum= forms.IntegerField(label="phoneNum")
    email= forms.EmailField(label="email", max_length=50)

The list is in my views as name. I have used {% for obj in name %}<p>{{obj}}</p>.

When I go to localhost:8000 it shows an HTML form as the out product. which is weird (like opening up google view page sources when right-clicked). I am also new to Django but know how to make views and link them along with most of the basics.

What I don't know is this {%%}; what is this symbol called, and where can I find documentation on this syntax?

What I am trying to achieve in this Django view is to have 3 boxes with names from the list name as the name for that box (pretty much an iteration).

Upvotes: 1

Views: 52

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

If you want to pass two (or more) elements to the template, you do that in the same dictionary:

def start_pr(response):
    name = ['Name', 'Phone Number', 'Email']
    form = usersData1()
    return render_to_response(
        response,
        'start_pr/start.html',
        {'form': form, 'name':name}
    )

However here it looks like you simply want to add placeholders. You can simply do that in the form:

from django import forms

class UsersData1(forms.Form):
    name= forms.CharField(
        label='Name',
        max_length=20,
        widget=forms.TextInput(attrs={'placeholder': 'Name'})
    )
    phoneNum = forms.IntegerField(
        label='Phone Number',
        widget=forms.NumberInput(attrs={'placeholder': 'Phone Number'})
    )
    email= forms.EmailField(
        label='Email',
        max_length=20,
        widget=forms.TextInput(attrs={'placeholder': 'Email'})
    )

This will avoid complicated logic in the template which is not the purpose of a template anyway. A template is used for rendering logic.

Upvotes: 1

Related Questions