Show the selected data of a select in a textarea


I am new to programming at Django, please if anyone can help me, I appreciate it.

I have a select where the options are the objects stored in the database of a model called Tema , what I want to do is show the attributes of the model selected below the select in a textarea .

This is the code of the view:

from django.shortcuts import render
from django.contrib import messages
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse, reverse_lazy
from django.http.response import HttpResponseRedirect
from django.views.generic.detail import DetailView
from django.views.generic.edit import UpdateView, DeleteView, CreateView
from django.views.generic.list import ListView
from forms import CreateRemedial, UpdateRemedial
from remedial.models import Remedial

class CreateRemedial(CreateView):

    model = Remedial
    form_class = CreateRemedial
    template_name = 'remedial/list-rem/create_r.html'

    def form_valid(self, form):
        new_rem =
        messages.success(self.request, 'Remedial creado satisfactoriamente', extra_tags='msg')
        url = reverse('update_remedial', kwargs=dict(
        return HttpResponseRedirect(url)

    def form_invalid(self, form):
        print self.request.POST
        messages.error(self.request, 'Verifique los datos marcados en rojo', extra_tags='msg')
        return super(CreateRemedial, self).form_invalid(form)

Model code:

from django.db import models
from tema.models import Tema

class Remedial(models.Model):

    def __unicode__(self):
        return self.description

Template code:

{% extends 'base.html' %}
{% block menu %}
    <li><a href="{% url 'index' %}"><i class="fa fa-home"></i> Inicio</a></li>
    <li><i class="fa fa-plus-circle"></i> Insertar remedial</li>
{% endblock %}
{% block title %}Insertar Remedial{% endblock %}
{% block new_r %}active{% endblock %}
{% block content %}
    <div class="col-lg-10">
        <div class="box box-primary flat">
            <div class="box-header">
                <h3 class="box-title"><i class="fa fa-plus-circle"></i> DATOS <span
                        class="small">del nuevo remedial</span></h3>
            <div class="box-body">
                <dl class="margin">
                    <form role="form" action="{% url 'create_remedial' %}" method="POST" autocomplete="off">
                        {% csrf_token %}
                        <div class="col-lg-12" style="margin: -15px 0 0 0; padding-bottom:10px; ">
                           <label for="descrip">{{ form.description.label|escape }} <sup><sup><i
                                   class="fa fa-asterisk"></i></sup></sup></label>

                            <div class="form-group {% if form.description.errors %}has-error{% endif %}">
                                {{ form.description }}
                                <small class="help-block">
                                    {% if form.description.errors %}{% for error in form.description.errors %}
                                        {{ error|escape }}
                                    {% endfor %}{% else %}{{ form.description.help_text|escape }}{% endif %}</small>
                             <div class="form-group {% if form.tema.errors %}has-error{% endif %}">
                                {{ form.tema }}
                                <small class="help-block">
                                    {% if form.tema.errors %}{% for error in form.tema.errors %}
                                        {{ error|escape }}
                                    {% endfor %}{% else %}{{ form.tema.help_text|escape }}{% endif %}</small>

                            <button type="submit" class="btn btn-success btn-flat" name="only_save"><i
                                    class="fa fa-check"></i> Insertar
                            <a href="{% url 'list_remedial' %}" class="btn btn-danger btn-flat pull-right"><i
                                    class="fa fa-arrow-circle-left"></i> Cancelar</a>
{% endblock %}

And the code of the form:

class CreateRemedial(forms.ModelForm):

    class Meta:

        model= Remedial




        help_text='Escriba los ejercicios que conforman el remedial',

        widget=forms.Textarea(attrs={'id': 'descrip',
                                      'class': 'form-control',
                                      'name': 'descrip1',
                                      'placeholder': 'Ejemplo: Ejercicio 3 del LT'
    tema=forms.ModelChoiceField(queryset=Tema.objects.all(),empty_label='Seleccione un tema',help_text='Seleccione el tema al cual está vinculado el remedial')

Basically what I want is to put inside the form that I have a textarea in such a way that when I display it and select a theme, I will be shown the description of it in textarea .

asked by Raudel 29.03.2016 в 20:56

1 answer


You must create an AJAX that calls a php where you look for the attributes of the Model. I leave the code of what the AJAX would be like.

$('#idSelect').change( function() {
var selectVal = $('#idSelect').val();
        type: 'POST',
        data: "select=" + selectVal,
        dataType: "json",
        url: "/url_donde_tendras_el_php_que_consiga_los_datos_del_model",
        success: function (response) {
            for(var i=0;i<response.length;i++){


Note In the php you receive the value of the select by postpor what you would do

$selectVal = $_POST['select'];
answered by 09.05.2016 в 18:51