Assignment for Implementing Controller
Last modified: 01-06-2015
In this assignment, we’ll need you to implement several controllers according to our specifications.
Note:
Before you start, you should know that from now on, since we wish you to think independently and figure out the solution, we won’t directly tell you exactly what to do step by step; but we’ll give me some general guidances. You can try to search on google, or look at the official document of Django, etc.
When you implement a controller that accepts POST requests, you may need to add the
csrf_exempt
decorator to that controller (if you’re using multiple decorators, make sure this is the last one being applied). By default, for POST request, Django would first check if the request contains a valid csrf token; if not will return 403. For making the problem simple, we use this decorator to work around that check. (More information on Cross Site Request Forgery)
Now let’s begin!
- Let’s start with something simple: heart beat response. The controller you need to implement is already defined in
app.controllers.practise.heart_beat
. Specifications are written in the doc string. When finished, usemake 2_controller_heart_beat
to under the test directory to test -
Now we come to the second question. You need to implement:
app.controllers.practise.create_file
, which processes requests with relative urls exactly the same as ‘/practise/file/create’app.controllers.practise.get_file
, which processes requests will relative urls in format ‘/practise/file/{the file id, alphanumeric(0-9, a-z, A-z)}/get’
Specifications are in the doc string. You will also need to add the pattern in
urls.py
.Hints:
- You may use
uuid
to generate a unique alphanumeric id
When finished, use
make 2_controller_files
to test - You may also use
make 2_controller
to test all assignments in this chapter
How to debug your server
When the request goes wrong, the first thing you need to do is to check the response code:
- 200: Server return successfully, but it’s probably returning unexpected results
- 404: Server cannot find the request you want. You probably sent the request to the wrong url or you didn’t configure the url dispatcher correctly
- 403: This is a tricky one. One possible reason is, your requested with POST, but you didn’t use @csrf_exempt decorator to the controller; in this case, Django refuse to process your request
- 500: This is probably the most common one, which means some code in the controller crashed, or threw an exception
Normally you can see the error message in the terminal you’re running the server, like this:
Internal Server Error: /practise/heartbeat
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 130, in get_response
% (callback.__module__, view_name))
ValueError: The view app.controllers.practise.heart_beat didn't return an HttpResponse object. It returned None instead.
[09/Feb/2015 17:44:15] "GET /practise/heartbeat HTTP/1.1" 500 55293
The error message would be very helpful for debugging. If you’re using PyCharm, you’ll see similar error messages in the console window.
Summary
After the study of this chapter, you should
- Roughly know the content inside a http request and a http response
- Understand how to set up Url Dispatcher in Django.
- Be comfortable with implementing controllers in Django