pass data from one view to another django - html

I have two views in my app
Views.py
def selectwarehouse(request):
z = Warehouse.objects.all()
return render(request, 'packsapp/employee/selectwarehouse.html', {'z': z})
def warehouse_details(request):
queryset = AllotmentDocket.objects.filter(send_from_warehouse = #dynamic(from the selectwarehouse.html))
return render(request, 'packsapp/employee/allotwarehousedetails.html', {'query': queryset})
selectwarehouse.html
{% block content %}
<label>Select Warehouse<label>
<select id="the-id">
{% for i in z %}
<option value="{{ i }}">{{ i }}</option>
{% endfor %}
<form method="post" novalidate>
{% csrf_token %}
Proceed
Nevermind
</form>
</select>
{% endblock %}
URLS.py
path('select-warehouse/', selectwarehouse, name='select_warehouse'),
path('warehouse-details/', warehouse_details, name='warehouse_details'),
I want that when a person selects the "Warehouse" from the dropdown and clicks on Proceed, it should pass that value to def warehouse_details and pass the value to Allotment queryset. How Can I do that ?

MAKE Proceed as a hidden input
Template
{% block content %}
<label>Select Warehouse<label>
<select id="the-id">
{% for i in z %}
<option value="{{ i }}">{{ i }}</option>
<form method="post" novalidate>
{% csrf_token %}
<input type="hidden" name="object_id" value="{{ i.id }}">
<input class="btn btn-outline-secondary" name="Proceed" type="submit"
value="Proceed">
<a href="{% url 'employee:products_table' %}" class="btn btn-outline-
secondary" role="button">Nevermind</a>
</form>
{% endfor %}
</select>
{% endblock %}
views.py
def selectwarehouse(request):
z = Warehouse.objects.all()
return render(request, 'packsapp/employee/selectwarehouse.html', {'z': z})
def warehouse_details(request):
queryset = AllotmentDocket.objects.get(id=request.POST.get('object_id'))
//Now to access the element in queryset write (queryset.'attribute name')
return render(request, 'packsapp/employee/allotwarehousedetails.html', {'query':
queryset}
Check if this works
Try printing the queryset and see what is the output.

Related

Formset not showing label in django

I am developing a domestic worker booking app in django
When I try to pass the formset, I am not geting the label of that field. I am only getting the field in html.
{% for formset in formsets %}
<form method="post">
{% for form in formset %}
{% for field in form %}
<div>
<label for="{{ field.auto_id }}">{{ field.label }}</label>
{{ field }}
{% for error in field.errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endfor %}
{% endfor %}
<input type="submit" value="Submit">
</form>
{% endfor %}
This the html code
def staffApply(request,pk):
if request.method == 'POST':
selected_domestic_works = request.POST.getlist('domestic_works')
formsets = []
if 'cook' in selected_domestic_works:
formsets.append(CookingForm(request.POST,prefix='cook'))
if 'driver' in selected_domestic_works:
formsets.append(DriverForm(request.POST,prefix='driver'))
print(formsets)
return render(request, 'staffApply2.html', {'formsets': formsets})
return render(request,'staffapply.html',{'uid':pk})
enter code here
This is my views.py
class CookingForm(ModelForm):
food_cooked=(('veg','veg'),
('non-veg','non-veg'),
('both','both')
)
class Meta:
model = Cook
fields = '__all__'
exclude=['user']
widgets={
'food_cooked':forms.widgets.RadioSelect(),
'type_of_cuisine':forms.widgets.CheckboxSelectMultiple()
}
This is my forms.py
I am getting the fields to type. But I am not getting hte label for those fields. Please help me fix this.
class Cook(models.Model):
food_cooked=(('veg','veg'),
('non-veg','non-veg'),
('both','both')
)
type_of_cuisine=(('NorthIndian','NorthIndian'),
('SouthIndian','SouthIndian'),
('Chettinadu','Chettinadu'),
('Chinese','Chinese'),
)
user=models.ForeignKey(User,on_delete=models.CASCADE)
food_cooked=models.CharField(choices=food_cooked,max_length=30)
type_of_cuisine=models.CharField(choices=type_of_cuisine,max_length=30)
experience=models.IntegerField()
wages_expected=models.IntegerField()
dishwashing_flag=models.BooleanField()
wages_for_dishwashing=models.IntegerField(null=True)
desc=models.TextField(max_length=200)
This is my models.py
You have one extra loop, with the wrong naming so you cannot access {{ field.label }} on your loops its like you are trying something like {{ form.field.attribute.label }}, the correct way would be the following:
{% for form in formsets %}
<form method="post">
{% for field in form %}
<div>
<label for="{{ field.auto_id }}">{{ field.label }}</label>
{{ field }}
{% for error in field.errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endfor %}
<input type="submit" value="Submit">
</form>
{% endfor %}
That being said, you can also use Django form rendering options, instead of doing it manually.
{% for form in formsets %}
<form method="post">
{{form.as_p}}
<input type="submit" value="Submit">
</form>
{% endfor %}

Django custom HTML template not returning any values to the admin.py

I have created a custom html template with basic checkboxes to select a value and return the value to the Django admin page.
The value of the selected superprofile does not get captured by the variable "selected_value" in the admin.py
The if statement "if request.method == 'POST':" is getting triggered but i keep getting the value of "selected_value" as none
The Html template
{% extends "admin/base_site.html" %}
{% load i18n admin_urls static admin_modify %}
{% block extrahead %}
{{ media }}
{% endblock %}
{% block content %}
<form class="change_superprofile_parent_form" met`your text`hod="POST" class="change_superprofile_parent_form">{% csrf_token %}
{% for superprofile in superprofiles %}
<input type="checkbox" name="superprofile_selected" {{ superprofile.checked }} value="{{ superprofile }}"> {{ superprofile }}<br>
{% endfor %}
<input type="submit" value="Submit">
</form>
{% endblock %}
Django admin.py
def change_superprofile_parent(self, request, queryset):
"""
Action to change the superprofile of the selected
"""
queryset = queryset.values_list("id", flat=True)
if request.method == 'POST':
selected_value = request.POST.getlist('superprofile_selected')
eligible_superprofiles = SuperProfile.objects.filter(status='Active')
return render(
request, 'admin/auth/user/change_superprofile_parent.html', context={
'superprofiles': eligible_superprofiles,
}
)

Django custom selection HTML page for django admin form

I have created a custom html template with basic checkboxes to select a value and return the value to the Django admin page.
I have done a 100 times before, but now the value of the selected superprofile does not get captured by the variable "selected_value" in the admin.py
The if statement "if request.method == 'POST':" is getting triggered but i keep getting the value of "selected_value" as none
Driving me crazy, I cannot find anything wrong in the code
The Html template
{% extends "admin/base_site.html" %}
{% load i18n admin_urls static admin_modify %}
{% block extrahead %}
{{ media }}
{% endblock %}
{% block content %}
<form class="change_superprofile_parent_form" method="POST" class="change_superprofile_parent_form">{% csrf_token %}
{% for superprofile in superprofiles %}
<input type="checkbox" name="superprofile_selected" {{ superprofile.checked }} value="{{ superprofile }}"> {{ superprofile }}<br>
{% endfor %}
<input type="submit" value="Submit">
</form>
{% endblock %}
Django admin.py
def change_superprofile_parent(self, request, queryset):
"""
Action to change the superprofile of the selected
"""
queryset = queryset.values_list("id", flat=True)
if request.method == 'POST':
selected_value = request.POST.getlist('superprofile_selected')
eligible_superprofiles = SuperProfile.objects.filter(status='Active')
return render(
request, 'admin/auth/user/change_superprofile_parent.html', context={
'superprofiles': eligible_superprofiles,
}
)

Action attribute in html form tag is not sending the data in django

I want to send song.id from each song in an album model but every time I send it, I get A KeyError
This is what I wrote in details.html
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="{% url 'music:favorite' album.id %}">
{% csrf_token %}
{% for song in album.song_set.all %}
<input type="radio" id="song{{ song.id }}" name="song" value="{{ song.id }}">
<label for="song{{ song.id }}">
{{ song.song_title }}
{% if song.is_favorite %}
<img src="https://png.pngtree.com/png-vector/20190726/ourmid/pngtree-cute-light-star-with-black-frame-png-image_1633374.jpg">
{% endif %}
</label>
<br>
{% endfor %}
<input type="submit" value="Favorite">
</form>
This is my views.py
def favorite(request, album_id):
album = get_object_or_404(Album, pk=album_id)
try:
selected_song = album.song_set.get(pk=request.POST['song'])
except (KeyError, Song.DoesNotExist):
return render(request, 'music/detail.html', {
'album':album,
'error_message':"You did not select a valid song",
})
else:
selected_song.is_favorite = True
selected_song.save()
return render(request, 'music/detail.html', {'album':album})
I am answering my own question. So in the form tag, I had to include the method attribute and set it to POST.
Change
<form action="{% url 'music:favorite' album.id %}">
To
<form action="{% url 'music:favorite' album.id %}" method="POST">

How to insert a button in a dropdown selected option in Django?

I would like to insert a dropdown in Django that will return me to a page, and I am inserting a button that will lead to that page, but when I do this I return to the page I am currently in.
index.html
{% if lista_de_provas %}
<form method='post' action=''>
{% csrf_token %}
<select class="form-control" name="prova_selecionada" id="prova_selecionada">
{% for prova in lista_de_provas %}
<option id="{{prova.idProva}}" name="listaProvas" value='{{prova.idProva}}' disabled>{{prova.tipoProva}} {{prova.anoProva}}</option>
{% endfor %}
</select>
<input id="selProva" type="button" value="Confirma" onclick = "location.href ='{{prova.idProva}}';" />
</form>
{% endif %}
views.py
def index(request):
lista_de_provas = Prova.objects.all()
cprova = request.POST.get('idProva')
if request.method == 'POST':
sprova = Prova.objects.get(cprova = cprova)
sprova.select()
return redirect('polls/detalhes.html')
else:
form = ProvaForm()
return render(request, 'polls/index.html',{'form':form,'lista_de_provas': lista_de_provas})
There are two ways you can do this.
First, you can specify explicitly what object you want. If you are looking for the second element you can do something like this:
{% if lista_de_provas %}
<form method='post' action=''>
{% csrf_token %}
<select class="form-control" name="prova_selecionada" id="prova_selecionada">
<option id="{{prova.1.idProva}}" name="listaProvas" value='{{prova.1.idProva}}' desabled>{{prova.1.tipoProva}} {{prova.1.anoProva}}</option>Mdjss.199
</select>
<input id="selProva" type="button" value="Confirma" onclick = "location.href ='{{prova.1.idProva}}';" />
</form>
{% endif %}
Or to make it a bit more "Dynamic" and fail-safe, you can go in your view and use a .filter() on your object. So instead of lista_de_provas = Prova.objects.all() you can do something like:
lista_de_provas = Prova.objects.filter(id=2)
Sorry if I missed something. English is my first language.