Skip to content

Instantly share code, notes, and snippets.

@newz2000
Created March 1, 2013 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newz2000/5065081 to your computer and use it in GitHub Desktop.
Save newz2000/5065081 to your computer and use it in GitHub Desktop.
This gist demonstrates the use case for fhurl pull request #2 https://github.com/amitu/fhurl/pull/2
from importd import d
@d("/")
def index(request):
return "home.html", {'title': 'Hello world!'}
@d("/api/")
def api(request):
context = {'title': 'api', 'me': 1}
if request.is_ajax():
return context
return "home.html", context
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<script src="/static/js/jquery.min.js"></script>
<script>
$(document).ready(function () {
"use strict";
$.getJSON('/api/', function (data) {
$('#content').append("The title is: " + data.title);
});
});
</script>
</head>
<body>
<h1>Hello World!</h1>
<p id="content"></p>
</body>
</html>
import unittest
from django.test.client import Client
from fhurl import JSONResponse
from django.http.response import HttpResponse
class TestViews(unittest.TestCase):
def setUp(self):
self.client = Client()
def test_home(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
def test_api_responds_with_json_if_ajax_request(self):
response = self.client.get('/api/', {}, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertIsInstance(response, JSONResponse)
def test_api_responds_with_html_if_normal_get(self):
response = self.client.get('/api/')
self.assertIsInstance(response, HttpResponse)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment