[英]submit form without refreshing the page
I created form contains title, content and image. 我创建的表单包含标题,内容和图像。
I try to submit that form without refreshing the whole page using with django with ajax. 我尝试提交该表单,而不用将ajax与django一起使用来刷新整个页面。
When I submit the form POST method is okay but I get another GET method showing this error. 当我提交表单时,POST方法是可以的,但是我得到了另一个显示此错误的GET方法。
GET http://127.0.0.1:8000/media/default.jpg [HTTP/1.1 404 Not Found 8ms] GET http://127.0.0.1:8000/media/default.jpg [HTTP / 1.1 404找不到8ms]
And I didn't declare default image in my model.py. 而且我没有在model.py中声明默认图像。
Here is my views.py 这是我的views.py
def posts(request):
posts = Post.objects.filter(posted_date__lte=timezone.now()).order_by('-posted_date')
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.save()
else:
form = PostForm()
args = {
'form': form,
'posts': posts,
}
if request.is_ajax():
html = render_to_string('posts/post-section.html', context=args, request=request)
return JsonResponse({'pos':html})
return render(request, 'posts/posts.html', args)
also here is my ajax 这也是我的阿贾克斯
$(document).on('submit', '.post-form', function(event){
event.preventDefault();
console.log($(this).serialize());
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: $(this).serialize(),
dataType: 'json',
success: function(response){
$('.post-section').html(response['pos']);
},
error: function(rs, e){
console.log(rs.responseText);
},
});
});
What should I do to submit that post without refreshing the page? 我应该怎么做才能在不刷新页面的情况下提交该帖子?
您的模板'posts / post-section.html'试图在渲染图像内容时错误地加载图像$('.post-section').html(response['pos']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.