Created
March 1, 2013 14:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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