Wednesday, March 21, 2018

Re: Building a calculator

Basically what you need is to store current selections/values in the session https://docs.djangoproject.com/en/2.0/topics/http/sessions/
Then access the session in the view and redirect accordingly.

def my_

On Tuesday, March 20, 2018 at 4:29:00 PM UTC-4, Abbad yeslem wrote:
Hello, 

I am new to Django and trying to learn using the framework as best as i can.

I am trying to build an application that can calculate thickness for a vessel under two cases: internal or external pressure. 

I am really stuck trying to answer the following (Please Help):

1) How can i redirect the user to one of the two calculation pages (where they will need to enter values in a field and see the result on the same page) based on their choice from ModelChoiceField?

2) How to do the calculations based on the equations i have and the iterations they require ( i know i should write them in the views but not sure how exactly)?

Please, if there is any other problem or you require further details on what i am trying to achieve, do tell me.

Thanks a lot!

- The calculations i am trying to do through the web app ( for the case of the external pressure)
p = float(input("Enter pressure in Kpa "))
if (p < 20684): # p < 3000 psi
    
  e = float(input("Enter joint efficiency "))
  c = float(input("Enter corrosion allowance in mm "))
  ltt = float(input("Enter tan-tan length in mm "))
  do = float(input("Enter outside diameter in mm "))
  t= float(input("Initialize thickness value in mm "))
  step_size= float(input("Enter step side in mm "))
  
else: 
      print("pressure too high!")
      quit()
  
print("Select Material.")
print("1.ASME SA285 C")
print("2.ASME SA516")
print("3.ASME SA537")
print("4.Enter Material")

choice = input("Enter choice(1/2/3):")

#s=allowable stress

if choice == '1':
    s=235000 
    el=206842718.79
   
    
if choice == '2':
    s=260000 
    el=2
    
    
if choice == '3':
    s=310000 
    el=3
   
    
if choice == '4':
    s=int(input("Enter material allowable stress in Kpa "))
    el=int(input("Enter modulus of elasticity in Kpa "))
    

allowable_pressure=0


while allowable_pressure < p:
    t=t+step_size
    
    d=do-2*t
    
    print("Select Head type.")
    print("1.hemi-heads")
    print("2.2:1 S.E. heads")
    print("3.100% - 6% heads")

    choice_1 = input("Enter choice(1/2/3):")

    if choice_1 == '1':
      l=ltt + 0.333*d
    
    if choice_1 == '2':
      l=ltt+0.16666*d
    
    if choice_1 == '3':
      l=ltt+0.16666*d
      
    #will work on digitizing the graphs to avoid having the user input values at every iteration
    print("your L/Do is ", l/do, "your Do/t is ", do/t)
    print("determine Factor A from ASME Code, Section II, Part D, Subpart 3, Fig G: Geometric Chart for Components Under External or Compressive Loadings")
    a = float(input("Enter A"))
    print("Using Factor "A", enter the applicable material chart from ASME Code, Section II, Part D, Subpart 3 at the appropriate temperature and determine Factor "B."")
    b = float(input("Enter B"))
    print("if A is to the left of material graph enter (1), on(2)")
    choice_2 = input("Enter choice(1/2):")  
      
    if choice_2 == '1':
       allowable_pressure=(3*do/t)
    
    if choice_2 == '2':
       allowable_pressure=(4*a*el)/(3*do/t)
    
print (t, "mm")
print (allowable_pressure, "Kpa")  
    


- Views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from .import forms

@login_required(login_url="/login/")
def AddMaterial(request):
    form=forms.AddMaterial()
    return render(request, 'webapp/Add_Material.html',{'form':form})

def thickness_l(p,ri,s,e,c):
   return (p*ri)/((2*s*e)+(0.4*p))+c

def thickness_c(p,ri,s,e,c):
   return (p*ri)/((2*s*e)-(0.6*p))+c

def thickness(p,ri,s,e,c):
    if thickness_l(p,ri,s,e,c) < thickness_c(p,ri,s,e,c):
         return thickness_c(p,ri,s,e,c)
    else:
         return thickness_l(p,ri,s,e,c)


# add equations for external pressure


@login_required(login_url="/login/")
def selection(request, ):
    if request.method == 'POST':
        form=forms.Pressure_choice(request.POST)
        #if choice = internal
        if form = #?
            return redirect('/Internal/')
        #if choice = external
        elif form = #?
            return redirect('/External/')
    else
        return render(request, 'webapp/selection.html',{'form':form})
        

@login_required(login_url="/login/")
def Internal_Pressure(request):
    form=forms.Internal_Pressure()
    return render(request, 'webapp/Internal_Pressure.html',{'form':form})

@login_required(login_url="/login/")
def External_Pressure(request):
    form=forms.External_Pressure()
    return render(request, 'webapp/External_Pressure.html',{'form':form})



- forms.py
from django import forms
from .import models

class AddMaterial(forms.ModelForm):
    class Meta:
        model=models.Material
        fields='__all__'


class Pressure_choice(forms.Form):
        CHOICES = (('1', 'Internal Pressure',), ('2', 'External Pressure',))
        choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)

class Internal_Pressure(forms.Form):
    class Meta:
        Material=models.Material
        Material_choice = forms.ModelChoiceField(queryset=Material.objects.all(), widget=forms.Select)
        Pressure = forms.FloatField()
        Internal_radius = forms.FloatField()
        Joint_efficiency= forms.FloatField()

class External_Pressure(forms.Form):
    class Meta:
        Material=models.Material
        Head=models.Head
        Material_choice = forms.ModelChoiceField(queryset=Material.objects.all(), widget=forms.Select)
        Head_choice=forms.ModelChoiceField(queryset=Head.objects.all(), widget=forms.Select)
        Pressure = forms.FloatField()
        Internal_radius = forms.FloatField()
        Tan_to_Tan_Length= forms.FloatField()
        #etc..

- External_Pressure.html
{% extends 'webapp/base_layout.html' %}

{% block content %}
  <h1>External Pressure</h1>
  <form class="site-form" action="index.html" method="post">
    {%csrf_token%}
    {{ form }}
    <input type="Submit" value="Submit">
{% endblock %}


- urls.py
from django.conf.urls import url
from . import views
from django.contrib.auth.views import login, logout
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings
from django.views.generic.base import RedirectView


urlpatterns = [
    url(r'^$', login, {'template_name': 'registration/login.html'}, name='login'),
    url(r'^logout/$', logout, {'template_name': 'registration/logout.html'}, name='logout'),
    url(r'^Internal/$', views.Internal_Pressure, name='Internal'),
    url(r'^External/$', views.External_Pressure, name='External'),
    url(r'^selection/$', views.selection, name='selection'),
    url(r'^AddMaterial/$', views.AddMaterial, name='AddMaterial'),
    #url(r'^.*$', RedirectView.as_view(pattern_name='login', permanent=False)),
]

urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


- models.py
from django.db import models
from django.forms import ModelForm
from django import forms

# Create your models here.
class Material(models.Model):
    Name = models.CharField(max_length=20)
    allow_stress = models.FloatField()
    elasticity = models.FloatField()

    def __str__(self):
        return self.Name

class Head(models.Model):
    Name = models.CharField(max_length=20)

    def __str__(self):
        return self.Name




--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/16d931f6-fcaa-4b8c-8e33-b5fa9bc4c1a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment