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:
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 psie = 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 stressif choice == '1':s=235000el=206842718.79if choice == '2':s=260000el=2if choice == '3':s=310000el=3if choice == '4':s=int(input("Enter material allowable stress in Kpa "))el=int(input("Enter modulus of elasticity in Kpa "))allowable_pressure=0while allowable_pressure < p:t=t+step_sized=do-2*tprint("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*dif choice_1 == '2':l=ltt+0.16666*dif choice_1 == '3':l=ltt+0.16666*d#will work on digitizing the graphs to avoid having the user input values at every iterationprint("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, redirectfrom django.contrib.auth.decorators import login_requiredfrom .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))+cdef thickness_c(p,ri,s,e,c):return (p*ri)/((2*s*e)-(0.6*p))+cdef 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 = internalif form = #?return redirect('/Internal/')#if choice = externalelif form = #?return redirect('/External/')elsereturn 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 formsfrom .import modelsclass AddMaterial(forms.ModelForm):class Meta:model=models.Materialfields='__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.MaterialMaterial_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.MaterialHead=models.HeadMaterial_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 urlfrom . import viewsfrom django.contrib.auth.views import login, logoutfrom django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.conf.urls.static import staticfrom django.conf import settingsfrom django.views.generic.base import RedirectViewurlpatterns = [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 modelsfrom django.forms import ModelFormfrom 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.Nameclass 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