在django的javascript通知 [英] javascript notification in django

查看:254
本文介绍了在django的javascript通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络应用程序中比较新,我在我的一个项目中使用django。我有一个上传页面。我想在我的上传页面中添加javacript / jquery通知,也就是上传内容后,消息将显示在我的页面顶部,其中的消息将是这样的。您的文件上传已经完成,此消息将显示几秒钟,然后将自动消失。现在这是我的上传视图。 ..来自django.shortcuts的




从django导入render_to_response,RequestContext
.contrib.auth.decorators import log_required
from myprofile.forms import DocumentForm
from django.contrib.auth import authenticate,login,logout,REDIRECT_FIELD_NAME $ d
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import logout as Signout
from django.contrib.auth.models import用户




def UserImageUpload(request):

如果request.method =='POST':
form = DocumentForm(request.POST,request.FILES)
如果form.is_valid():

newdoc =照片(photo = request.FILES ['photo'],user = request.user )
newdoc.save()


else:
form = DocumentForm()

uploaded_image = Photo.objects.all()

返回render_to_response('myprofile / user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request))

,这是forms.py ..

  from django import forms 

class DocumentForm(forms.Form):
photo = forms.ImageField(
label ='选择一个文件'


这是upload.html ..

  {%extends'base.html'%} 
{%b锁定标题%}用户图像上传{%endblock%}
{%block content%}
< div class =containerstyle =margin-top:5%>
< div class =col-md-4 col-md-offset-4>
< div class =well>
< form action =method =postenctype =multipart / form-data>
{%csrf_token%}
< p> {{form.non_field_errors}}< / p>
< p> {{form.photo.label_tag}} {{form.photo.help_text}}< / p>
< p>
{{form.photo.errors}}
{{form.photo}}
< / p>

< p>< input type =submitvalue =Uploadclass =btn btn-success/>< / p>


< / form>
< / div>
< / div>
< / div>

{%endblock%}

现在我该怎么办? / p>

解决方案

您可以设置cookie作为回应,保存照片时,并编写一些jquery代码来获取并显示,代码如下所示:

  ... 
newdoc.save()
response = render_to_response('myprofile / user_image_upload.html ''{'upload_image':uploaded_image,'form':form},context_instance = RequestContext(request)

response.set_cookie('message',你的文件上传已经完成)
返回响应

从插件的Cookie获取此消息的Jquery代码 https://github.com/carhartl/jquery-cookie ,并显示在页面上:

  msg = $ .cookie(message)
if(msg)
alert(msg);

您可以选择一个jquery通知插件(例如 http://ned.im/noty/ )替换alert(msg)。


i am relatively new in web application,i am using django in one of my project.Here i have a upload page.i want to add javacript/jquery notification in my upload page,that is ,after uploading a content,a message will be shown in top of my page where the message will be something like that.. "your file upload has been done" and this message will appear for some seconds and then it will be dissappear automatically.now this is my upload view...

from django.shortcuts import render_to_response,RequestContext
from photo.models import Photo
from django.contrib.auth.decorators import login_required
from myprofile.forms import DocumentForm
from django.contrib.auth import authenticate, login, logout, REDIRECT_FIELD_NAME
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import logout as Signout
from django.contrib.auth.models import User




def UserImageUpload(request):

    if request.method == 'POST':
        form = DocumentForm(request.POST,request.FILES)
        if form.is_valid():

            newdoc = Photo(photo = request.FILES['photo'],user = request.user)
            newdoc.save()


    else:
        form = DocumentForm()

    uploaded_image = Photo.objects.all()

    return render_to_response('myprofile/user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request))

and this is the forms.py..

from django import forms

class DocumentForm(forms.Form):
    photo = forms.ImageField(
         label='Select a file'

    )

and this is the upload.html..

{% extends 'base.html'%}
{% block title%}User Image Upload {% endblock %}
{%block content%}
<div class="container" style="margin-top:5%">
    <div class="col-md-4 col-md-offset-4">
        <div class="well">
  <form action="" method="post" enctype="multipart/form-data">
            {% csrf_token %}
            <p>{{ form.non_field_errors }}</p>
            <p>{{ form.photo.label_tag }} {{ form.photo.help_text }}</p>
            <p>
                {{ form.photo.errors }}
                {{ form.photo }}
            </p>

            <p><input type="submit" value="Upload" class="btn btn-success" /></p>


        </form>
        </div>
</div>
</div>

{%endblock%}

now how can i do this?

解决方案

You can set cookies in response, when photo saved, and write some jquery code to get and show it, code like this:

...
newdoc.save()
response = render_to_response('myprofile/user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request))

response.set_cookie('message', "your file upload has been done") 
return response

Jquery code to get this message from cookies with plugin https://github.com/carhartl/jquery-cookie, and show on page:

msg = $.cookie("message")
if (msg)
    alert(msg);

And you can choice a jquery notification plugin(such as http://ned.im/noty/) to replace "alert(msg)".

这篇关于在django的javascript通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆