04.10 Blog Reply

ajax method:

$.ajax 、$.get 、$.post  、 $.getJson  、.load()

The data returned is json, or xml type


Since it is an asynchronous update, the reply function uses ajax


Backstage:

def replyblog1(request):
        blogid= int (request.POST.get( 'id' )) #Remember to convert id to integer type
         val=request.POST.get( 'val' )
        username=request.session.get('username')
        myreply=replyblog()
        myreply.content=val
        myreply.bloguser=BlogUser.objects.filter( userName =username) .first() #This is an object, so to get the object, this is to get the login username, and the reply has a username, matching
         myreply.blog111=Blog. objects.get( pk =blogid) #Get the blog object, there is an author, pk is the primary key
         myreply.save()
        list=[]
        blogreplylist=replyblog.objects.filter( blog111_id =blogid) #Lists , strings, and dictionaries are also serializable, and a single object cannot be serialized
 print (blogreplylist)
         for rep in blogreplylist: #Because the value of json is passed in the past , without username, all ids, so use your own json format to implement asynchronous requests, [{id:'id',content:'2345'}]
 blogrep={}                    
            blogrep [ 'id' ] = rep.id
            blogrep['content']=rep.content
            blogrep[ 'createtime' ]=datetime.strftime(rep.createtime, '%Y-%m-%d %H:%M:%S' ) #To change the time to string format, serializable,
             blogrep[ 'username' ]=rep.bloguser.userName
            list.append(blogrep)
 
 
    return HttpResponse(json.dumps(list))

front desk:



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325522142&siteId=291194637
Recommended