Trying to create an app that saves sales in different sateges for my team(similar to an ecommerce) however the form to add the account info is not saving the data...
I review different options and use formset as well no changes.
model.py
TYPE=(
('delegated','Delegated'),
('not_delegated','Not Delegated')
)
class Account(models.Model):
agent_profile=models.ForeignKey(AgentProfile, on_delete= CASCADE)
account_name=models.CharField(max_length=120)
opp_type=models.CharField(max_length=20, choices=TYPE, default='not_delegated')
bp_id=models.IntegerField()
mdm_id=models.CharField(max_length=50, null=True,blank=True)
AM=models.CharField(max_length=50,null=True,blank=True)
def __str__(self):
return str(self.agent_profile)
forms.py
from django.forms import ModelForm
from .models import Account
class AccountForm(ModelForm):
class Meta:
model=Account
fields=[
#'agent_profile',
'account_name',
'opp_type',
'bp_id',
'mdm_id',
'AM'
]
views.py
def confirm_account_create_view(request):
form=AccountForm(request.POST or None)
context = {
"form": form
}
next_ = request.GET.get('next')
next_post = request.POST.get('next')
redirect_path = next_ or next_post or None
if form.is_valid():
instance=form.save(commit=False)
agent_profile, agent_profile_created = AgentProfile.objects.new_or_get(request)
if agent_profile is not None:
opp_type = request.POST.get('delegate_opp', 'not_delegated_opp')
instance.agent_profile = agent_profile
instance.opp_type = opp_type
instance.save()
request.session[opp_type + "_opp_id"] = instance.id
print(opp_type + "_opp_id")
else:
print("Please verify Form")
return redirect("opps:confirm")
if is_safe_url(redirect_path, request.get_host()):
return redirect(redirect_path)
return redirect("opps:confirm")
urls.py (main app)
path('orders/confirm/account', confirm_account_create_view, name='create_account'),
form page(HTML) {% csrf_token %} {% if next_url %} {% endif %}
{% if opp_type %}
<input type='hidden' name='opp_type' value='{{ opp_type }}' />
{% endif %}
{{ form.as_p }}
<button type='submit' class='btn btn-default'>Submit</button>
</form>
confirm page(HTML) {% extends "base.html" %} {% load static %}
{% block content %}
{{ object.order_id }} --{{object.opp}}
<div class="pb-9 mb-7 text-center border-b border-black border-opacity-5">
<h2 class="mb-7 lg:mt-6 text-3xl font-heading font-medium">Confirm Info</h2>
</div>
<h2 class="mb-7 lg:mt-6 text-3xl font-heading font-medium">Account</h2>
<div class='row'>
<div class="flex items-center justify-between py-4 px-10 mb-3 leading-8 bg-white bg-opacity-50 font-heading font-medium rounded-3xl">
{% if not object.delegated_opp %}
{% url "create_account" as confirm_account_create_view%}
{% include 'accounts/form.html' with form=delegated_form ext_url=request.build_absolute_uri action_url=confirm_account_create_view opp_type='not_delegated_opp' %}
</div>
<div class="flex items-center justify-between py-4 px-10 mb-3 leading-8 bg-white bg-opacity-50 font-heading font-medium rounded-3xl">
{% elif not object.not_delegated_opp %}
{% url "create_account" as confirm_account_create_view%}
{% include 'accounts/form.html' with form=delegated_form ext_url=request.build_absolute_uri action_url=confirm_account_create_view opp_type='not_delegated_opp' %}
</div>
</div>
{%else%}
<div>
<h2 class="mb-7 lg:mt-6 text-3xl font-heading font-medium">Order summary</h2>
<div class="flex items-center justify-between py-4 px-10 mb-3 leading-8 bg-white bg-opacity-50 font-heading font-medium rounded-3xl">
<span>Subtotal</span>
<span class="flex items-center text-xl">
<span class="mr-2 text-base">$</span>
<span>{{object.opp.subtotal}}</span>
</span>
</div>
<div class="flex items-center justify-between py-4 px-10 mb-3 leading-8 bg-white bg-opacity-50 font-heading font-medium rounded-3xl">
<span>Discount</span>
<span class="flex items-center text-xl">
<span class="mr-2 text-base">$</span>
<span>{{object.discount}}</span>
</span>
</div>
<div class="flex items-center justify-between py-4 px-10 mb-14 leading-8 bg-white font-heading font-medium rounded-3xl">
<span>Total</span>
<span class="flex items-center text-xl text-blue-500">
<span class="mr-2 text-base">$</span>
<span>{{object.total}}</span>
</span>
</div>
<a class="inline-block w-full py-5 lg:py-3 px-10 text-lg leading-6 lg:leading-7 text-white font-medium tracking-tighter font-heading text-center bg-blue-500 hover:bg-blue-600 focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 rounded-xl" href="#">Save Opp</a>
</div>
</div>
</div>
<div class="md:w-96"><a class="block py-5 px-10 w-full text-xl leading-6 font-medium tracking-tighter font-heading text-center bg-white hover:bg-gray-50 focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50 rounded-xl" href="#">Back to top</a></div>
</div>
</section>
{% endif %}
{% endblock content %}