我想在我的应用程序中一个创建另一个,以便仅管理ajax请求。</ p>

我的第一个 </ p>

  def my_playlists(request):
        返回HttpResponseRedirect('/ login /')
     播放列表= Playlist.objects.filter(user_id =
     返回render(request,'playlists / my_playlists.html',{'playlists':播放列表})
</ code> </ pre>

还有第二个,我可以在其中调用jquery的ajax请求。</ p>

def getTemplateForm(request):
       id_song = request.POST ['id_song'];
       音乐= get_object_or_404(音乐,pk = id_song)
       标题= music.title
</ code> </ pre>

因此可以创建另一个视图吗? 以及我如何处理</ p>
     </ div>



I am trying to understand how manage greatly the ajax system request with the framework Django. I would like create another in one on my app in order to manage only ajax requests.

My first

def my_playlists(request) :
   if not request.user.is_authenticated() :
       return HttpResponseRedirect('/login/')
    playlists = Playlist.objects.filter(
    return render(request, 'playlists/my_playlists.html', {'playlists' : playlists})

And a second where I can call ajax request from jquery.

def getTemplateForm(request) :
   if request.is_ajax() :
      id_song = request.POST['id_song'];
      music = get_object_or_404(Music, pk=id_song)
      author =
      title = music.title
      return render(request, 'playlists/edit-form-song.html', {'author' : author, 'title' : title})

I think it's better to seperate ajax and action methods. So it is possible to create another view ? And how I do with ?


The views handling AJAX requests have not special magic. They have only specific headers in response.

For example, I use my custom decorators for AJAX views.

# to set cookies in IE
import json as json2
from django.http import HttpResponse

def _setP3P( response ):
    response["P3P"] = 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"' # to set up cookies in AJAX views (for Internet Explorer)

def json( view ):
    def response( *args, **kwargs ):
        request, data = view( *args, **kwargs )

        response = HttpResponse(    json2.dumps( data ),
                                content_type = 'application/json' )

        _setP3P( response )

        return response

    return response

The example of using:

def set_photo_mark( request ):
    req = request.REQUEST

    # actions here

    return request, { 'result': "OK" } # json response

The view above generates json answer.

You can include it into your by a standart way:

urlpatterns = patterns('',          
    ( r'^set-photo-mark', 'phapp.views.set_photo_mark' ),

Then you can do usual AJAX calls to /set-photo-mark/.

For non-ajax views I have another decorator: template.

def template( template = None ):
    def template_inner( view ):
        def response( *args, **kwargs ):        

            args_list = list( args )

            request = args[ 0 ]
            del args_list[ 0 ]  

            request, result = view( request, *args_list, **kwargs )     

            req = request.REQUEST

            if type( result ) == dict:
                " Result is out. "
                t = loader.get_template( template )
                c = RequestContext( request, result )

                response = HttpResponse(    t.render(c),
                                        content_type = 'text/html' )            

            elif type( result ) == type( None ):
                response = HttpResponseNotFound( request )                                      
                response = redirect( result )

                _setP3P( response )

                return response

            return response

    return template_inner

This is also easy to use:

@template( "index.html" )
def index( request ):
    req = request.REQUEST

    user_id = req[ 'oid' ]

    # actions...

    if True:
        # an example of using the current template index.html
        return request, {   'sid': user_id,
                    'user_id': req[ 'oid' ],
                    'PHOTOS_ROOT_URL': PHOTOS_ROOT_URL
        return request, '/help/' # an example of redirection to /help/

And you can make wrapped templates to emulate view inheritance.

Csdn user default icon