submit button in django refreshes the page without saving - html

I am trying to save data in ORM via a form but when I click on the submit button all it does is refresh the page with the same data.
HTML
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% crispy form %}
{% load widget_tweaks %}
{% block content %}
<h2 class="mb-3">Add a Warehouse</h2>
<div class="row">
<form method="post" class="col-md-12 proct-form" novalidate>
{% csrf_token %}
<div class="form-group col-md-4 mb-0">
{{ form.owner|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_name|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_email|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_contact|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_city|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_pincode|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_state|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_pan|as_crispy_field }}
</div>
<div class="form-group col-md-4 mb-0">
{{ form.warehouse_gst|as_crispy_field }}
</div>
<div class="form-group col-md-12 mb-0">
<button type="submit" class="btn btn-success">Save</button>
Nevermind
</div>
</form>
</div>
{% endblock %}
Views.py
#method_decorator([login_required, employee_required], name='dispatch')
class WarehouseFormView(CreateView):
model = Warehouse
fields = "__all__"
template_name = 'packsapp/employee/warehouseForm.html'
def form_valid (self, form):
product = form.save(commit=False)
product.save()
messages.success(self.request, 'The Warehouse was created with success!')
return redirect('employee:warehouse_table')
Is there a problem with my views.py or do I need to change my templates?
All the other forms in the app are created the same way and they work fine

Related

Dropdown are not working in Django , HTML

I am trying to display data from database throw drop-down list and Use filter query to filter data and display
if i tried to select value of html and select but slot is not create. Debugging form and i see error. If i select anything but drop-down do't selected here is error
Here is my code: slot/views.py
def slot_create(request):
form = SlotForm(request.POST)
print(form)
station = Station.objects.filter(user_id=request.user)
print(station)
if form.is_valid():
slot = form.save(commit=False)
slot.user = request.user
slot.save()
messages.success(request, 'Your Slot is add successfully')
return redirect('view_slot')
return render(request, 'add_slot.html', {
'form': form,
'station': station
})
here is my add_slot.html
<form method="post" novalidate>
{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-12 mb-0">
{{ form.slot_name|as_crispy_field }}
</div>
<div class="form-group col-md-12 mb-0">
{{ form.per_unit_price|as_crispy_field }}
</div>
<div class="form-group col-md-6 mb-0">
{{ form.current_status|as_crispy_field }}
</div>
{% comment %} <div class="form-group col-md-6 mb-0">
{{ form.station|as_crispy_field }}
</div> {% endcomment %}
<div class="form-group col-md-6 mb-0">
<label for="station" class="block font-semibold text-sm mb-1"> Choose Station </label>
<select name="station_name" id = "station">
{% for station in station %}
<option value="{{station}}"> {{station}} </option>
{% endfor %}
</select>
</div>
<button type="submit" class="w-full rounded-full bg-red-gradient p-3 text-white font-bold hover:ring">
Add Slot
</button>
</div>
</form>

Django ask for fill a form not required

I've a strange bug with my forms.
In a page I must hande 2 separate forms,
the 1st one have no problem at all and I can add the record in DB ,
the 2nd one raise me an error asking to fill a required data of 1st form.
The POST dictionary looks ok
Here the log:
<QueryDict: {'csrfmiddlewaretoken': ['lvt5Ph2SA1xxFK4LMotdHOWk2JuZzYDo0OKWc77rKICYKYmemy3gl0dBphnRNcFb'], 'pk_atto': ['1.1'], 'pk_persona': ['1'], 'capacita': ['11'], 'Aggiungi_persona': ['persona'], 'tipo': ['fondo']}>
<ul class="errorlist"><li>pk_particella<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
the views:
if request.method == 'POST':
if("Aggiungi_particella" in request.POST):
save_atto = AttiPartenzeParticelleForm(request.POST)
else:
save_atto = AttiPartenzePersoneForm(request.POST)
print(request.POST)
print(save_atto.errors)
if save_atto.is_valid():
save_atto.save()
return redirect('/aggiungi_atto_partenza' + '/' + str(save_atto['pk_atto'].value()))
the forms:
class AttiPartenzeParticelleForm(ModelForm):
pk_atto = forms.ModelChoiceField(queryset=Atti.objects.all(),
widget=forms.Select
(attrs={'class': 'form-control'}))
pk_particella = forms.ModelChoiceField(queryset=Particelle.objects.all(),
widget=forms.Select
(attrs={'class': 'form-control'}))
capacita = forms.CharField(max_length=30,
widget=forms.NumberInput
(attrs={'class': 'form-control'}))
tipo = forms.CharField(max_length=30, initial="fondo", widget=forms.TextInput (attrs={'class': 'form-control'}))
class Meta:
model = Acquisizioni_Cessioni_particella
fields = '__all__'
class AttiPartenzePersoneForm(ModelForm):
pk_atto = forms.ModelChoiceField(queryset=Atti.objects.all(),
widget=forms.Select
(attrs={'class': 'form-control'}))
pk_persona = forms.ModelChoiceField(queryset=Persone.objects.all(),
widget=forms.Select
(attrs={'class': 'form-control'}))
capacita = forms.CharField(max_length=30,
widget=forms.NumberInput
(attrs={'class': 'form-control'}))
tipo = forms.CharField(max_length=30, initial="fondo", widget=forms.TextInput (attrs={'class': 'form-control'}))
class Meta:
model = Acquisizioni_Cessioni_particella
fields = '__all__'
and the HTML
<div id="particella" class="content-section d-flex justify-content-center mt-5">
<form action="" method="POST" id="particella_f">
{% csrf_token %}
<fieldset class="form-group">
<div style="visibility:hidden">
{{ form.pk_atto|as_crispy_field }}
</div>
<div class="row">
<div class="col-8">
{{ form.pk_particella|as_crispy_field }}
</div>
<div class="col-2">
{{ form.capacita|as_crispy_field }}
</div>
<div class="col-4 d-flex justify-content-center">
<button form="particella_f" class="btn btn-outline-primary btn-lg mt-5" type="submit" name="Aggiungi_particella" value="particella">
AGGIUNGI PARTICELLA
</button>
</div>
<div style="visibility:hidden">
{{ form.tipo|as_crispy_field }}
</div>
</div>
</fieldset>
</form>
</div>
<div id="persona" class="content-section d-flex justify-content-center mt-5">
<form action="" method="POST" id="persona_f">
{% csrf_token %}
<fieldset class="form-group">
<div style="visibility:hidden">
{{ persona.pk_atto|as_crispy_field }}
</div>
<div class="row">
<div class="col-8">
{{ persona.pk_persona|as_crispy_field }}
</div>
<div class="col-2">
{{ persona.capacita|as_crispy_field }}
</div>
<div class="col-4 d-flex justify-content-center">
<button class="btn btn-outline-primary btn-lg mt-5" form="persona_f" type="submit" name="Aggiungi_persona" value="persona">
AGGIUNGI PERSONA
</button>
</div>
<div style="visibility:hidden">
{{ form.tipo|as_crispy_field }}
</div>
</div>
</fieldset>
</form>
</div>
soneone have any idea?
thx

Django Formset one form in each row

How to place each form of a formset on a horizontal line, one below the other without using a table?
My html is putting everything on one line.
Any help much appreciated.
Html
[<div class="form-group row">
<div class="row">
{{ formset.management_form }}
{% for form in formset.forms %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="col-sm-2">
{{ form.logical_operator|as_crispy_field }}
</div>
<div class="col-sm-3">
{{ form.event|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.operator|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.event_value|as_crispy_field }}
</div>
<div class="col-md-1 align-self-center delete-form">
<button onclick="exclude(this)" type="button"
title="Excluir"
class="btn btn-danger btn-sm">
<i class="feather icon-trash-2"></i>
</button>
</div>
{% endfor %}
</div>
</div>
image from screen
This reorganization solved the problem.
Thanks for the help!
<div class="form-group row">
<div class="col-sm-12">
{{ formset.management_form }}
{% for form in formset %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="row">
<div class="col-sm-2">
{{ form.logical_operator|as_crispy_field }}
</div>
<div class="col-sm-3">
{{ form.event|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.operator|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.event_value|as_crispy_field }}
</div>
<div class="col-md-1 align-self-center delete-form">
<button onclick="exclude(this)" type="button"
title="Excluir"
class="btn btn-danger btn-sm">
<i class="feather icon-trash-2"></i>
</button>
</div>
</div>
{% endfor %}
</div>
</div>
Please try using </br> (break line) command:
<h1> HEllo</h1> </br> <h2>How are you</h2>

How to edit a BoleanField in Django

please i need your help been stuck like this for days,here is my problem, i have a profile page of a user that contain a BooleanField and a Button if the user would like to subscribe or not, i am wanting to design it this way, in which their will only be a one click button that will show Subscribe and once clicked you you get redirected to the payment page then you choose your plan and make payment,then in the user info it will show a dead btn of Subscribed and underneath would be the code generated for a specific task,just help me share even if its a link to a tutorial to follow as a starting point,here is my code and the picture
model.py
class Patient(models.Model):
STATE_CHOICES=(
(True, u'Yes'),
(False, u'No'),
)
user = models.OneToOneField(CustomUser, on_delete=models.CASCADE, null=True, related_name="patient")
subscribe = models.BooleanField(default=True, choices=STATE_CHOICES)
html
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4 h4 text-center center">Your Email</legend>
<legend class=" mb-4 h3 center text-center">{{ user.email }}</legend>
</fieldset>
<div class="form-group text-center center">
<button class="btn btn-outline-info" type="submit">{{ user.patient.subscribe }} </button>
</div>
</form>
Im not sure what exactly you want. But it looks like you want to use if statement in your template. Something like this.
{% if not user.patient.subscribed %}
<button class="btn btn-outline-info" type="submit">{{ user.patient.subscribe }}</button>
{% else %}
Subscribed!
{% endif %}
https://docs.djangoproject.com/en/3.0/ref/templates/language/
how do i generate a random numbers or text as plan id
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4 h4 text-center center">Your Email</legend>
<legend class=" mb-4 h3 center text-center">{{ user.email }}</legend>
</fieldset>
<div class="form-group text-center center">
{% if not user.patient.subscribe %}
<button class="btn btn-outline-info" type="submit">Subscribe</button>
{% else %}
{% if user.patient.subscribe %}
<button class="btn btn-danger" type="submit">Subscribed</button>
<p>Thanks For Subscribing here is your Plan ID </p>
{% endif %}
{% endif %}
</div>
</form>

Bootstrap misaligned label under textarea

I have a bootstrap form which consists of basic input fields and a few textarea's. Im using Django for my form validation and am having a persistent problem aligning validation labels under the textarea's. This form is only using bootstrap css modules so there is no custom css interfering with it as far as i can tell.
Example: The date and time field align perfectly as the notes validation label does not.
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="well well-lg">
<form class="form-horizontal" action="" autocomplete="off" method="post">
{% csrf_token %}
{% for field in UserForm %}
<div class="form-group">
<label class="col-md-4 control-label" for="{{ field.label }}"> {{ field.label }}</label>
<div class="col-md-6">
{{ field }}
</div>
{% for error in field.errors %}
<div class="col-md-6">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
</div>
{% endfor %}
</form>
</div>
</div>
</div>
The number of columns inside the class="row" does not add up to 12. Try rewriting the form-group part like this:
<div class="form-group">
<label class="col-md-4 control-label" for="{{ field.label }}"> {{ field.label }}</label>
<div class="col-md-6">
{{ field }}
{% for error in field.errors %}
<p>
<strong>{{ error|escape }}</strong>
</p>
{% endfor %}
</div>
</div>