我正在尝试通过AJAX使用Django视图上传图像。这是我的HTML表单。
<form class="BackgroundImageUplaoder" action="/uplaod" id="form2">{% csrf_token %}
<input type="file" accept="/image*" name="image" multiple="false" />
<button type="submit">Uplaod</button>
而相应的ajax是:-
$(document).ready(function(){
$('#form2').submit(function(){
var csrftoken = $("[name=csrfmiddlewaretoken]").val();
var formdata={
'image':$('input[name=image]').val(),
};
console.log("Formvalue is taken");
console.log(formdata.image);
$.ajax({
type:'POST',
url:'/Upload/Background/',
data:formdata,
dataType:'json',
encode:true,
headers:{
"X-CSRFToken": csrftoken
},
processData:false,
contentType:false,
})
.done(function(data){
console.log(data);
if(!data.success){//we will handle error
if (data.password){
console.log(data.password);
$('#password_error').text(data.password);
}
return false;
}
else{
window.location='/';
}
});
event.preventDefault();
});
});
和django视图是:-
def uploadbackground(request):
if request.method=="POST":
form=BackgroundImageUplaod(request.POST,request.FILES)
if form.is_valid():
instance=form.save(commit=False)
myobject=HomeScreen.objects.get(profile__user=request.user)
if myobject:
myobject.image=instance
myobject.save()
return JsonResponse({'success':True})
else:
sample=HomeScreen(profile__user=request.user,image=instance)
sample.save()
return JsonResponse({'success':True})
else:
form=BackgroundImageUplaod()
return JsonResponse({'image':'An error is encountered while uplaoding','errors': [(k, v[0]) for k, v in form.errors.items()]})
根据我的观点,表格未通过验证。我正在检查是否存在与当前登录用户对应的实例。如果是,我正在更新该实例,否则创建新对象
答案 0 :(得分:1)
您的问题是您无法通过仅接受字符串和整数的AJAX JSON import random
import pygame
import sys
import time
from pygame import mixer
screenx = 800
screeny = 600
clock = pygame.time.Clock()
name = pygame.display.set_caption('Brutal Survival')
win = pygame.display.set_mode((screenx,screeny))
player_right = pygame.image.load('maincharright.png')
player_left = pygame.image.load('maincharleft.png')
player_up = pygame.image.load('maincharup.png')
player_down = pygame.image.load('mainchardown.png')
background = pygame.image.load('background.jpg')
mummychar = pygame.image.load('mummychar.png')
randomspawnabove = (random.randint(0, screenx), -100)
randomspawnbelow = (random.randint(0, screenx), (screeny + 100))
randomspawnleft = (-100, random.randint(0, screeny))
randomspawnright = ((screenx + 100), random.randint(0, screeny))
multiplier = 2
level = 1
nextlevel = True
spawn = True
up = True
down = False
left = False
right = False
run = True
class player():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.alive = True
self.vel = 5
class enemy():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.alive = False
self.vel = 2
def redrawScreen():
pygame.display.update()
if run == True:
win.blit(background, (0,0))
if up == True and left == False and right == False and down == False:
win.blit(player_up, (mainChar.x, mainChar.y))
if up == False and left == False and right == False and down == True:
win.blit(player_down, (mainChar.x, mainChar.y))
if up == False and left == False and right == True and down == False:
win.blit(player_right, (mainChar.x, mainChar.y))
if up == False and left == True and right == False and down == False:
win.blit(player_left, (mainChar.x, mainChar.y))
if mummy.alive == True:
win.blit(mummychar, (mummy.x, mummy.y))
maxenemies = (level * multiplier)
mummy_Spawn = [randomspawnleft, randomspawnright, randomspawnabove, randomspawnbelow]
mummy = enemy(*random.choice(mummy_Spawn), 50, 50)
mainChar = player(screenx/2 - 30, screeny/2, 60, 60)
Bullets = []
Enemies = []
while run:
clock.tick(60)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
for x in range(maxenemies):
Enemies.append(mummy())
for Enemy in Enemies:
enemy.update()
for Enemy in Enemies:
mummmy.draw(win)
if nextlevel == True:
if level == 1:
mummy.alive = True
if mummy.alive == True and mummy.x > mainChar.x:
mummy.x = mummy.x - mummy.vel
if mummy.alive == True and mummy.x < mainChar.x:
mummy.x = mummy.x + mummy.vel
if mummy.alive == True and mummy.y > mainChar.y:
mummy.y = mummy.y - mummy.vel
if mummy.alive == True and mummy.y < mainChar.y:
mummy.y = mummy.y + mummy.vel
if keys[pygame.K_ESCAPE]:
run = False
pygame.quit()
if keys[pygame.K_UP]:
mainChar.y = mainChar.y - mainChar.vel
up = True
down = False
left = False
right = False
if keys[pygame.K_DOWN]:
mainChar.y = mainChar.y + mainChar.vel
down = True
left = False
up = False
right = False
if keys[pygame.K_RIGHT]:
mainChar.x = mainChar.x + mainChar.vel
right = True
left = False
up = False
down = False
if keys[pygame.K_LEFT]:
mainChar.x = mainChar.x - mainChar.vel
left = True
right = False
down = False
up = False
if (mainChar.y + mainChar.height) > screeny:
mainChar.y = mainChar.y - 5
if mainChar.y < 0:
mainChar.y = mainChar.y + 5
if mainChar.x < 0:
mainChar.x = mainChar.x + 5
if (mainChar.x + mainChar.width) > screenx:
mainChar.x = mainChar.x - 5
redrawScreen()
发送图像。您需要使用POST
。