python - How receive/posta data using URLs in django? -
i new in django. need write application read, write data using service links.
for example, have urls like:
http://site.com/something/loadproducts/
http://site.com/something/deleteproductbyid/ , on.
can give me example work get, post urls crud operations. thanks
from django.shortcuts import render, get_object_or_404 # form imports here # model imports here # objects def get_view(request): objects = yourmodel.objects.all() return render(request, 'all-objects.html', {'objects': objects} # make new object def make_new_obj(request): form = yourform(request.post or none) if request.method == 'post': if form.is_valid(): form.save() return render(request, 'make-new-obj.html', {'form': form} # object id def get_single_object_view(request, id): obj = get_object_or_404(yourmodel, pk=id) return render(request, 'obj-detail.html', {'obj': obj} # update object def update_obj(request, id): obj = get_object_or_404(yourmodel, pk=id) form = yourform(request.post or none, instance=obj) if request.method == 'post': if form.is_valid(): form.save() return render(request, 'update-obj.html', {'obj': obj, 'form': form} # delete object def delete_obj(request, id): obj = get_object_or_404(yourmodel, pk=id) obj.delete() # else redirect object index, etc # you'll want make two-step process
obviously, there lot of code here can combined, mix , match needed. of code covered in django documentation...
model instance reference: https://docs.djangoproject.com/en/1.5/ref/models/instances/
working forms: https://docs.djangoproject.com/en/1.5/topics/forms/
Comments
Post a Comment