7

I need to set custom response headers in Django project.

Here is code from facts/urls.py:

d = {
    'app_name': 'facts',
    'model_name': 'Fact'
}

urlpatterns = patterns('',
    (r'^$', 'facts.main', d),
)

This approach shows data from model, but I'am not sure if there is a way to set custom headers here?

Also I tried another approach - I created facts/views.py with following function:

def fact(request):

    response = render_to_response('facts.html', 
                                  {'app_name': 'facts',
                                   'model_name': 'Fact'}, 
                                  context_instance=RequestContext(request))

    response['TestCustomHeader'] = 'test'

    return response

and changed code in urls.py:

(r'^$', facts.views.fact),

This approach sets custom headers but doesn't show data from model.

Any help?

danodonovan
  • 17,150
  • 6
  • 64
  • 73
Phantom
  • 1,686
  • 4
  • 17
  • 31
  • You are not clear in your question, as I don't see any header manipulation regarding your response... Django covers Headers in the Documentation: https://docs.djangoproject.com/en/1.6/ref/request-response/#django.http.HttpRequest.META – petkostas Jan 16 '14 at 15:13
  • I add custom header(TestCustomHeader) in second approach and can see it in firebug. But model is not loaded in that case. – Phantom Jan 16 '14 at 15:17
  • Why should you see model data? you are not actually fetching any model data anywhere... You should check Django documentation for further understanding: https://docs.djangoproject.com/en/1.6/topics/http/views/ – petkostas Jan 16 '14 at 15:37
  • I just dont want to put big amount of code. People will not read it ... I have Fact model in facts app and it works ok in first approach(in urls.py), but it is not loaded in views.py. I cannot realize why – Phantom Jan 16 '14 at 15:39
  • Because views.py does not work the same way with urls.py, and actually you should be dealing with the code, not readers, overloading urls.py with tasks is not readable as well, developers expect view data to be found in the corresponding places. In your view function you have to actually do the manipulations (call the model, fetch the data and assign them to the response) this is no Django way, this is how most frameworks work when dealing with views / controllers. – petkostas Jan 16 '14 at 15:43

1 Answers1

6

When you pass the dict to views.main in urls.py, the function def main() deals with {"model_name": "Fact"}. Probably there's some code like:

model = get_model(kwargs["model_name"])
return model.objects.all()

When you pass "model_name" to render_to_response, the dict is passed to the template as context. If you include in your template {{model_name}}, the page should render Fact.


Setting custom headings in Class Based views, inside the Class define a function like:

def get(self, request):
    response = HttpResponse()
    response["TestCustomHeader"] = "test"

    return response

Or in a function view:

def main(request):
    response = HttpResponse()
    reponse["TestCustomHeader"] = "test"

    [ Some code to fetch model data ]

    return response
xbello
  • 6,291
  • 3
  • 24
  • 38