Wednesday, May 6, 2015

Firing an html file from within a view/template

Production Context, Ubuntu 14.04, Django 1.7.7 & Apache2

Dear experts,

I'm stranded!
What I would like to do is:
1) to provide an initial and final date to make some statistical computation in that period,
2) pass those dates to an R procedure which by means of the package R2HTML produces an html file ("statcosti.html"),
3) watch ecstatically and with emotion at the results of computation in my browser.

I 've been able to make step 1) and 2) but only partially step 3). That's why I'm asking your help.

Here it is the relevant part of my code

#statistical_view.py
...........
class IntervalloDateForm(forms.Form):
# the default format is %Y-%m-%d
data_iniziale = forms.DateField(label='Data Inizio (AAAA-MM-GG)',widget=forms.widgets.DateInput(format="%d/%m/%Y"), required=False)
data_finale = forms.DateField(label='Data Fine (AAAA-MM-GG)',widget=forms.widgets.DateInput(format="%d/%m/%Y"), required=False)
def cleaned_data_finale(self):
if (self.cleaned_data['data_finale'] < self.cleaned_data['data_iniziale']):
raise forms.ValidationError("La data iniziale deve essere precedente alla data finale")
return self.cleaned_data['data_finale']

@login_required
def costi_per_paziente(request):
import os
ris = None
if request.method == 'POST':
form = IntervalloDateForm(request.POST)
if form.is_valid():
data_in = form.cleaned_data['data_iniziale']
data_fi = form.cleaned_data['data_finale']
if data_fi < data_in:
raise forms.ValidationError("La data iniziale deve essere precedente alla data finale")
my_command = '/usr/bin/Rscript /var/www/magazantea/r-base/costi_per_paziente.R "' + str(form.cleaned_data['data_iniziale']) + '" "' + str(form.cleaned_data['data_f
ris = os.system(my_command)
else:
form = IntervalloDateForm()
return render_to_response('stat_costoxpaziente.html',
{'form':form,'ris':ris}, RequestContext(request))



#urls.py
....................
urlpatterns = patterns('',
url(r'^magazzino/', include(admin.site.urls)),
............,
url(r'^costiperpaziente/$', 'magazzino.statistical_views.costi_per_paziente', name='costiperpaziente'),
)


#templates/stat_costoxpaziente.html

{% extends "base.html" %}
{% block title %}Date di inizio e fine analisi{% endblock %}
{% block content %}
<h1>Analisi costi di magazzino per paziente e categoria di prodotto</h1>
<form action="." method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="OK" />
</form>
{% if ris == 0 %}
{% include "/var/www/magazantea/templates/R2HTML/statcosti.html" %}
{% endif %}
{% endblock %}

While if I input file:////var/www/magazantea/templates/R2HTML/statcosti.html directly in my browser I see all the tables and graphs wonderfully formatted, (that is, text and graphs are centered), the include command in templates/stat_costoxpaziente.html loses all the centering alignment of text and tables and above all doesn't show at all the graphs even though their path is hardcoded in statcosti.html as
.............
<p align=center><img src='/var/www/magazantea/templates/R2HTML/grafico2.png' border=1 width=600></p>
.............

How is that possible?

Besides, I find somewhat confusing to have in the same web page both the widgets for inputting the dates and to follow the results.

Therefore I'm asking you how to modify my statistical_views.py, templates/stat_costoxpaziente.html, and urls.py to obtain the following in this order:
1) The page with the date input widgets and clicking on OK,
2) Start the R procedure
3) In case of success (ris ==0) open /var/www/magazantea/templates/R2HTML/statcosti.html in the browser.

Ciao
Vittorio





--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3434EC2D-0F6F-4845-AAC7-CBF66DE6C5CE%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment