I have written a function of django ajax. If you don't know what's wrong, you can't execute successfully?

the function to be implemented is that when viewing a resource, you need to deduct the points of the resource, display the real resource address, and call ajax, to implement no refresh

.

models.py
class Zyuser_MeiMei (models.Model):

author = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
mmei = models.ForeignKey("MeiMei", verbose_name="", null=True)
mark = models.BooleanField(default =False, verbose_name=_(u""))

def __unicode__(self):
    return self.author.username
class Meta:
    verbose_name_plural = u"vip"


views.py

< H1 > users view deduction function < / H1 >

@ login_required
@ csrf_exempt
def look_mm (request, mm_id):

author = request.user
mm = MeiMei.objects.get(id = mm_id)
print "adsfasdf"
if author.levels < mm.level:
    -sharpraise ","
    -sharp!
    return render(request,"meimei/jifen.html")
else:
    User.objects.filter(username = author.username).update(levels=F("levels") - mm.level)
    u_m = Zyuser_MeiMei.objects.update_or_create(author=author, mmei=mm)
    u_m.mark = True
print "aaaaaaaaaa"
return u_m

.html page

Click to view resources

< script src= " http://libs.baidu.com/jquery/.;></script>
< script type= "text/javascript" >
function vip_chakan (o, meimei.id) {

    $.ajax({
        type:"POST",
        url:"/look_mm/"+{{meimei.id}}+"/",
        dataType:"json",
        processData:false,  // jquery
        contentType:false,  // jquery
        success:function(u_m){
            alert(u_m.mark);
        }
        error:function(){
            alert("!")
        }
    });

}
< / script >

url.py
url (r"^ look _ mm/ (? P < mm_id > [0-9] +) / $", login_required (look_mm), name="lookmm"),

I don"t know what went wrong. Which views.py function is always not executed?


where the veiw.py is called, you can make a breakpoint under debug, or add a log to see where the problem occurs.


got it out. Make a note:

views.py

@login_required
@csrf_exempt
def look_mm(request, mm_id):
    author = request.user
    ret = {'status': 0, 'u_m': ''}
    mm = MeiMei.objects.get(id = mm_id)
    try:
        User.objects.filter(username = author.username).update(levels=F('levels') - mm.level)
        Zyuser_MeiMei.objects.update_or_create(author=author, mmei=mm)
        ret['status'] = 1
        ret['u_m'] = True
    except:
    return JsonResponse(ret)

html

<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> 
<script type="text/javascript">
function vip_chakan(){
        $.ajax({
            type:"POST",
            url:"/look_mm/"+{{meimei.id}}+"/",
            dataType:"json",
            processData:false,  // jquery
            contentType:false,  // jquery
            success:function(data, status){
                if(data.status == 1){
                $("-sharpchakan").css('display','none'); 
                $("-sharpokchakan").css('display','inline');                
                                    }
            }
        });
}
</script>

the judgment that the integral in the original number is not enough, I put it directly in the template.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e58f48-441aa.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e58f48-441aa.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?