1 | from django.conf.urls.defaults import patterns
|
---|
2 | from django.http import HttpResponse
|
---|
3 | from django.template import RequestContext, Template
|
---|
4 |
|
---|
5 | template = """
|
---|
6 | <script src="//yandex.st/jquery/1.6.2/jquery.min.js"></script>
|
---|
7 | <script>
|
---|
8 | $(function() {
|
---|
9 | $('#ajax_get, #ajax_post').click(function(e) {
|
---|
10 | e.preventDefault();
|
---|
11 | var id = this.id;
|
---|
12 | $.ajax({
|
---|
13 | //type: (id == 'ajax_post') ? 'POST' : 'GET',
|
---|
14 | type: 'POST',
|
---|
15 | url: '/ajax/',
|
---|
16 | cache: false,
|
---|
17 | data: {csrfmiddlewaretoken: '{{ csrf_token }}'},
|
---|
18 | success: function(data) {alert(data);},
|
---|
19 | error: function(e) {alert(e.status);}
|
---|
20 | });
|
---|
21 | });
|
---|
22 | });
|
---|
23 | </script>
|
---|
24 | HTTP_REFERER: {{ request.META.HTTP_REFERER }}<br>
|
---|
25 | HTTP_HOST: {{ request.META.HTTP_HOST }}<br>
|
---|
26 | <a href="">Get referer</a><br>
|
---|
27 | <a href="" id="ajax_get">Ajax GET request</a>
|
---|
28 | <a href="" id="ajax_post">Ajax POST request</a>
|
---|
29 | """
|
---|
30 |
|
---|
31 | def index(request):
|
---|
32 | t = Template(template)
|
---|
33 | return HttpResponse(t.render(RequestContext(request, {'request': request})))
|
---|
34 |
|
---|
35 | def ajax(request):
|
---|
36 | return HttpResponse('HTTP_REFERER: %s\nHTTP_HOST: %s' % (request.META.get('HTTP_REFERER', ''), request.META['HTTP_HOST']))
|
---|
37 |
|
---|
38 | urlpatterns = patterns('',
|
---|
39 | (r'^$', index),
|
---|
40 | (r'^ajax/$', ajax),
|
---|
41 | )
|
---|