Skip to content
Advertisement

how to set default value in django template

i’m trying to set default value to an input field in django template , i have two models tables

class Ticketing(models.Model):
    Title = models.CharField(max_length=40)
    b = models.DecimalField(max_digits=5,decimal_places=2)
    #others

class Cancel(models.Model):
    ticket = models.ForeignKey(Ticketing,on_delete=models.CASCADE,related_name="ticket")
    title = models.CharField(max_length=40)
    #others

forms.py

class CancelForm(forms.ModelForm): 
    class Meta:
        model = Cancel
        fields = ['title',]

my views.py

def create_cancel(request,id):
    booking_obj = get_object_or_404(Ticketing,id=id)
    form = CancelForm()
    if request.method == 'POST':
        form = CancelForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.admin = request.user
            obj.ticket= booking_obj
            obj.save()
            return redirect('cancel:create' ,booking_obj.id)

     context = {
    '    booking_obj':booking_obj,'form':form
     }
     return render(request,'cancel/create.html',context)

urls

path('create/<int:id>',create_cancel,name='create'),
path('ajax/check-title/<int:id>',create_cancel,name='check_title'),

i tried to call back title using ajax but it doesnt work

@login_required
def check_invoice_paid(request,id):
    obj = get_object_or_404(Ticketing,id=id)
    data = {
       'title':obj.title
    }
    return JsonResponse(data)

my template

        $('select').change(function() {
            let elm = $(this);
            data = {};
            data[elm.attr("name")] = elm.val();
            $.ajax({
                url:'{% url 'cancel:check_title' booking_obj.id %}',
                data:data,
                success:function(data){
                    if (data.title){
                        elm.closest("div.ticket ").find("input.title").val(data.title);
                    }


                    }

                }
            )
        })  
            <form action="" method="POST">{% csrf_token %}
                {{form.errors}}
                <div id="printDv" class="text-lg">
                    <p class="p-2 header rounded-lg text-center text-white">Cancel</p>
                    <div class="border border-purple-900 mt-2 rounded-lg p-2 text-center">
                        <p>no. : {{booking_obj.id}}
                        </p>
                    </div>
                    
                    <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" >
                        <p>{{form.title | add_class:'title bg-transparent focus:outline-none w-8/12' }} :title</p>
         
                    </div>
                    
                </div>
                <button class="header pt-2  text-white px-4 p-1 rounded-lg mt-4">save</button>
            </form>
but still doesnt work ! is there something i did wrong ?

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.

def create_cancel(request,id):
    booking_obj = get_object_or_404(Ticketing,id=id)
     
    # set initial data here
    initial_data = {'title': 'Title' }
    form = CancelForm(initial=data)
    

    if request.method == 'POST':
        form = CancelForm(request.POST)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.admin = request.user
            obj.ticket= booking_obj
            obj.save()
            return redirect('cancel:create' ,booking_obj.id)

     context = {
    '    booking_obj':booking_obj,'form':form
     }
     return render(request,'cancel/create.html',context)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement