Thanks for telling me about TemplateResponse.

After experimenting for a while I have found that unless you call the
required URL through self.client.get() the templates variable is not
available whatever type of HttpResponse I use. In this case I am
calling the view directly because of the need to add something extra
to the request as mentioned before. The code is question looks like
this:

self.factory = RequestFactory()
        request = self.factory.get('/')
        request.subdomain = 'Something'
        response = send_to_view(request, page=None)

        response.render()

        self.assertContains(response, 'SwanLotus | Something')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'something/something.html')

The two assertions above pass but self.assertTemplateUsed() still
gives an error:

template_names = [t.name for t in response.templates]
AttributeError: 'TemplateResponse' object has no attribute
'templates'.

Have you seen this happen in your experience? I am thinking of taking
a look at
the django source to see what client.get() does different.

Thanks,

Yours sincerely,
nav

On Nov 9, 4:14 pm, Flavia Missi <flaviami...@gmail.com> wrote:
> TemplateResponse<https://docs.djangoproject.com/en/1.3/ref/template-response/#template...>has
> the attributes you need, plus, the template rendering process is lazy.
> I always use this class instead of using HttpResponse directly.
>
> []'s
>
>
>
>
>
>
>
>
>
> On Wed, Nov 9, 2011 at 5:47 AM, nav <navanitach...@gmail.com> wrote:
> > Thanks RequestFactory does exactly what I needed it is a pity that I
> > had ignored it while going through the docs.
>
> > There is however one question I have is it possible to check what
> > template was used to render the content. The assertTemplateUsed()
> > method looks for a response.templates attribute that does not exist as
> > part of the response returned from the view. response.template (please
> > note the lack of the 's' at the end) is also a deprecated feature and
> > the django source says that response.templates should be used instead.
>
> > I cannot think of any other way to check what template was rendered
> > and wondered if you have any pointers in this respect.
>
> > Thanks once again.
>
> > Yours sincerely,
> > nav
>
> > On Nov 8, 8:39 pm, Flavia Missi <flaviami...@gmail.com> wrote:
> > > When I need something like you want, I usually use RequestFactory to
> > create
> > > the request and call the view directly.
>
> > > You can see an example herehttps://gist.github.com/1348085
>
> > > Hope that helps ;)
>
> > > []'s
>
> > > On Tue, Nov 8, 2011 at 12:21 PM, nav <navanitach...@gmail.com> wrote:
> > > > Dear Folks,
>
> > > > While testing my application I found out that the middleware that
> > > > implements process_request is not run. This is fine as long as I can
> > > > add the required field to the request object before passing the
> > > > required URL to self.client.get().
>
> > > > what I want to do is something like this
>
> > > > request.field = 'some value'
> > > > self.client.get(url)
> > > > assertsomething()
>
> > > > The variable field is checked for in the views and does some
> > > > processing based on that. What would be the best way to add this field
> > > > to the request sent by self.client.get().
>
> > > > Thanks,
>
> > > > Yours sincerely,
> > > > nav
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "Django users" group.> To post to this group, send 
> > > > email>todjango-us...@googlegroups.com.
> > > > To unsubscribe from this group, send email 
> > > > to>>django-users+unsubscr...@googlegroups.com.
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/django-users?hl=en.
>
> > > --
> > > Flávia Missi
> > > @flaviamissi <http://twitter.com/flaviamissi>
> > > flaviamissi.com.brhttps://github.com/flaviamissi
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.> To post to this group, send email 
> > todjango-us...@googlegroups.com.
> > To unsubscribe from this group, send email 
> > to>django-users+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.
>
> --
> Flávia Missi
> @flaviamissi <http://twitter.com/flaviamissi>
> flaviamissi.com.brhttps://github.com/flaviamissi

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to