The project I'm working on has some data that needs to get passed to every view, so we have a wrapper around render_to_response
called master_rtr
. Ok.
Now, I need our 404 pages to run through this as well. Per the instructions, I created a custom 404 handler (cleverly called custom_404
) that calls master_rtr. Everything looks good, but our tests are failing, because we're receiving back a 200 OK.
So, I'm trying to figure out how to return a 404 status code, instead. There seems to be an HttpResponseNotFound class that's kinda what I want, but I'm not quite sure how to construct all of that nonsense instead of using render_to_response
. Or rather, I could probably figure it out, but it seems like their must be an easier way; is there?
EDIT:
The appropriate parts of the code:
def master_rtr(request, template, data = {}): if request.user.is_authenticated(): # Since we're only grabbing the enrollments to get at the courses, # doing select_related() will save us from having to hit database for # every course the user is enrolled in data['courses'] = \ [e.course for e in \ Enrollment.objects.select_related().filter(user=request.user) \ if e.view] else: if "anonCourses" in request.session: data['courses'] = request.session['anonCourses'] else: data['courses'] = []
data['THEME'] = settings.THEME
return render_to_response(template, data, context_instance=RequestContext(request))
def custom_404(request): response = master_rtr(request, '404.html') response.status_code = 404 return response