I've done the following
>>from Djano.test.client import Client
>>c=Client()
>>c=c.get('/promotions/',)
>>c.status_code
301
I keep getting 301 or 500 status codes. I've tried the full URL but
have not got any further.
The URL works fine in a normal browser and in the manage.py runserver
window it shows a status code of 200.
Anybody have any ideas of where I'm going wrong with the Test Client.
MerMer
301 is a permanent redirection, which indicates that the view attached
to /promotions is returning a HttpResponsePermanentRedirect. Another
common (and similar) response is 302 - a HttpReponseRedirect, which
happens after a successful post to a generic view.
If you check the contents of c.response, you should see the URL that
you are being redirected to (which may be the same URL).
Yours,
Russ Magee %-)
Many thanks for the response. However, c.response isn't working for
me. I'm getting "No attribute found". Running dir(c) does not show up
this method.
MerMer
Random, possibly silly question: do you have CommonMiddleware enabled
with APPEND_SLASH = True? If so, and if you're sending a request to a
URL which doesn't end in a trailing slash, Django will issue a
redirect...
--
"May the forces of evil become confused on the way to your house."
-- George Carlin
That's wierd. I can't think of any obvious reason that that would happen.
Ok - help me replicate the problem. Can you provide a test case - that is:
- the simplest subset of the view behind /promotions/ that exhibits the problem
- instructions on how you are invoking the test.
- The test code that exhibits the problem.
- Any details you can furnish on your platform, OS, etc.
Yours
Russ Magee %-)
I think I had failed to import everything. I have now used "from
django import * with partial success. Then I tried.
>>c=Client()
>>d=c.get('/promotions/')
>>d.content
This worked and returned me a large page of in my shell view.
However,
>>d.context
>>d.template
returns nothing.
>>d(dir)
returns a list of available methods, but "response" is not included.
I then created a simple view:-
def testivew(request):
return HttpResponse("This is a test")
set this up via the URL conf and then tried again. Same results as
before.
I'm running Python 2.4 on Windows XP with the latest version of Django.
MerMer
Ok... First off - Russell wakes up and realizes the mistake he made.
Note to self - don't answer email with an unhappy 7 month old in your
lap. :-)
I'm not sure which dark corner of my brain 'c.response' came from, but
it was a bad suggestion - that attribute doesn't exist. I think I was
aiming for 'c.content', but that wouldn't be correct, either
(although, it would be interesting to see the value of 'c.content'.
Try:
c = Client()
response=c.get('/promotions/',)
print response['Location']
This should give you the location you are being redirected to.
> set this up via the URL conf and then tried again. Same results as
> before.
Ok - you've given me little bits, and a vague set of instructions of
what you might have done, but not enough to replicate your problem.
Keep in mind that I don't have the problem you have, so you need to
help me help you. Giving me little bits of snippets of code, and
helpful hints like "I think I had failed to import everything" doesn't
help me replicate your problem.
What I need is a _complete project_ that exhibits the problem - that
means a model, view, URLpattern, settings, and complete execution
instructions.
Don't just send me your entire multi-view, multi-model application,
either. Send me a test case - the smallest project you can build that
exhibits the problem you are observing. I don't want to spelunk
through thousands of lines of code to try and work out which two lines
from your project are causing the problem. Again - help me help you.
Also, speaking from experience - the process of building this minimal
subset project quite often reveals that the actual problem is some
silly typo that you didn't spot, so it can be a valuable debugging
tool.
Yours,
Russ Magee %-)