i’m trying to set default value to an input field in django template , i have two models tables
JavaScript
x
10
10
1
class Ticketing(models.Model):
2
Title = models.CharField(max_length=40)
3
b = models.DecimalField(max_digits=5,decimal_places=2)
4
#others
5
6
class Cancel(models.Model):
7
ticket = models.ForeignKey(Ticketing,on_delete=models.CASCADE,related_name="ticket")
8
title = models.CharField(max_length=40)
9
#others
10
forms.py
JavaScript
1
5
1
class CancelForm(forms.ModelForm):
2
class Meta:
3
model = Cancel
4
fields = ['title',]
5
my views.py
JavaScript
1
17
17
1
def create_cancel(request,id):
2
booking_obj = get_object_or_404(Ticketing,id=id)
3
form = CancelForm()
4
if request.method == 'POST':
5
form = CancelForm(request.POST)
6
if form.is_valid():
7
obj = form.save(commit=False)
8
obj.admin = request.user
9
obj.ticket= booking_obj
10
obj.save()
11
return redirect('cancel:create' ,booking_obj.id)
12
13
context = {
14
' booking_obj':booking_obj,'form':form
15
}
16
return render(request,'cancel/create.html',context)
17
urls
JavaScript
1
3
1
path('create/<int:id>',create_cancel,name='create'),
2
path('ajax/check-title/<int:id>',create_cancel,name='check_title'),
3
i tried to call back title
using ajax but it doesnt work
JavaScript
1
8
1
@login_required
2
def check_invoice_paid(request,id):
3
obj = get_object_or_404(Ticketing,id=id)
4
data = {
5
'title':obj.title
6
}
7
return JsonResponse(data)
8
my template
JavaScript
1
18
18
1
$('select').change(function() {
2
let elm = $(this);
3
data = {};
4
data[elm.attr("name")] = elm.val();
5
$.ajax({
6
url:'{% url 'cancel:check_title' booking_obj.id %}',
7
data:data,
8
success:function(data){
9
if (data.title){
10
elm.closest("div.ticket ").find("input.title").val(data.title);
11
}
12
13
14
}
15
16
}
17
)
18
})
JavaScript
1
17
17
1
<form action="" method="POST">{% csrf_token %}
2
{{form.errors}}
3
<div id="printDv" class="text-lg">
4
<p class="p-2 header rounded-lg text-center text-white">Cancel</p>
5
<div class="border border-purple-900 mt-2 rounded-lg p-2 text-center">
6
<p>no. : {{booking_obj.id}}
7
</p>
8
</div>
9
10
<div class="ticket border border-purple-900 mt-2 text-center rounded-lg p-2 grid grid-cols-1 md:grid-cols-1 gap-3" dir="ltr" >
11
<p>{{form.title | add_class:'title bg-transparent focus:outline-none w-8/12' }} :title</p>
12
13
</div>
14
15
</div>
16
<button class="header pt-2 text-white px-4 p-1 rounded-lg mt-4">save</button>
17
</form>
im trying to set booking_obj.title
as default value for title
input in the Cancel
model
thank you for helping
Advertisement
Answer
Pass an initial object to the form on initialization to set initial values. Be careful with the names of the fields you have declared in the form and model.
JavaScript
1
22
22
1
def create_cancel(request,id):
2
booking_obj = get_object_or_404(Ticketing,id=id)
3
4
# set initial data here
5
initial_data = {'title': 'Title' }
6
form = CancelForm(initial=data)
7
8
9
if request.method == 'POST':
10
form = CancelForm(request.POST)
11
if form.is_valid():
12
obj = form.save(commit=False)
13
obj.admin = request.user
14
obj.ticket= booking_obj
15
obj.save()
16
return redirect('cancel:create' ,booking_obj.id)
17
18
context = {
19
' booking_obj':booking_obj,'form':form
20
}
21
return render(request,'cancel/create.html',context)
22