2017-10-17 1 views
0

js 파일 (정적 또는 동적 일 수 있음)로 라우팅하고 싶습니다.Django urlpattern에서 js 파일로 경로 지정하는 방법

(function() { 
    var img = new Image, 
     url = encodeURIComponent(document.location.href), 
     title = encodeURIComponent(document.title), 
     ref = encodeURIComponent(document.referrer); 
    img.src = '%s/a.gif?url=' + url + '&t=' + title + '&ref=' + ref; 
})(); 

그리고 클라이언트에서 I에 의해 헤더에서이 스크립트를 실행합니다 : 사용 staticfile없이

<script async="" src="https://example.com/a.js"> </script> 

, 나는 우리가 URLPATTERN하여 사용할 수 있다고 생각

JS 파일 형식 같은 일을 가지고 . 하지만 자바 스크립트 파일에 대한 사용을 모른다.

+0

? 이 js 파일을 템플릿에 제공하고 싶습니까? 이 파일의 출처는 어디입니까? – MohitC

+0

과 같은 요청을 받았을 때이 자바 스크립트를 생성하고 응답으로 되돌리려합니다. –

답변

0

해결책을 찾았습니다. views.py에서

:

def generatejs(request): 
    js_str=""" 
    (function() { 
     var img = new Image, 
      url = encodeURIComponent(document.location.href), 
      title = encodeURIComponent(document.title), 
      ref = encodeURIComponent(document.referrer); 
      img.src = 'http://127.0.0.1:8000/a.gif?url=' + url + '&t=' + title + '&ref=' + ref; 
     })(); 
    """ 
    return HttpResponse(js_str) 

그리고 urlpatterns에서

, 삽입 :

정확히 여기에 원하는 무엇
url(r'^gtag/js', collector.generatejs), 
관련 문제