私信  •  关注

Salman Ahmed

Salman Ahmed 最近创建的主题
Salman Ahmed 最近回复了
5 年前
回复了 Salman Ahmed 创建的主题 » 如何使用ajax在django模板中设置默认值

初始化时将初始对象传递给窗体以设置初始值。注意表单和模型中声明的字段的名称。

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)
5 年前
回复了 Salman Ahmed 创建的主题 » Django Ajax:在多个python脚本的目录中调用特定的python脚本

为ajax_测试文件中的每个函数创建一个单独的URL,并在点击该URL时调用该函数。

from djangotest.ajax.ajax_test import ajaxTest, greetingTest
from django.contrib import admin
from django.urls import path

urlpatterns = [
      path('admin/', admin.site.urls),
      path('', handlerView),
      path('ajax/hello_word', ajaxTest)
      path('ajax/greet', greetingTest)
    ]

ajax_测试中的函数:

from django.shortcuts import HttpResponse
def greetingTest(request):
    return HttpResponse("Good day!")

打JS电话:

$.ajax({
    url: 'ajax/hello_world',
    type: 'get', 
    success: function(data) {
        alert(data);
    },
    failure: function(data) { 
        alert('Encountered an error');
    }
});

$.ajax({
    url: 'ajax/greet',
    type: 'get', 
    success: function(data) {
        alert(data);
    },
    failure: function(data) { 
        alert('Encountered an error');
    }
});