0

The url im trying to match is:

http://domain/games/getdata/genres/

This request is made via ajax to get some JSON data from a external api. I cant get it to make a match; keeps going to a middleware handler i have set up. Im sure this is fix is pretty obvious, but ive been staring at this way too long.

urls.py:

from django.conf import settings
from django.conf.urls.defaults import patterns, include
from django.contrib import admin


admin.autodiscover()

urlpatterns = patterns('',

    (r'^$', 'blog.views.index'),
    (r'^games/$', 'giantbomb_games.views.index'),
    (r'^games/getdata/(?P<resource>\w+)/$', 'giantbomb_games.views.getdata'),

    (r'^grappelli/', include('grappelli.urls')),
    (r'^admin/filebrowser/', include('filebrowser.urls')),
    (r'^admin/', include(admin.site.urls)),
)

views.py:

def getdata(request, resource):

    url = '%s/%s/?api_key=%s&format=%s' % (api_url, resource , api_key, request_format)
    print url
    r = requests.get(url)

    return r.content

page/middleware.py:

from django.http import Http404
from django.conf import settings

from page.views import site_page

class SitePageFallbackMiddleware(object):
    def process_response(self, request, response):
        if response.status_code != 404:
            return response # No need to check for a flatpage for non-404 responses.
        try:
            return site_page(request, request.path_info)
        # Return the original response if any errors happened. Because this
        # is a middleware, we can't assume the errors will be caught elsewhere.
        except Http404:
            return response
        except:
            if settings.DEBUG:
                raise
            return response

django error:

Traceback (most recent call last):

  File "/var/www/html/top10/top10/../ext/django/core/servers/basehttp.py", line 280, in run
    self.result = application(self.environ, self.start_response)

  File "/var/www/html/top10/top10/../ext/django/core/servers/basehttp.py", line 674, in __call__
    return self.application(environ, start_response)

  File "/var/www/html/top10/top10/../ext/django/core/handlers/wsgi.py", line 245, in __call__
    response = middleware_method(request, response)

  File "/var/www/html/top10/top10/page/middleware.py", line 8, in process_response
    if response.status_code != 404:

AttributeError: 'str' object has no attribute 'status_code'

thank you in advance.

1
  • 1
    URLs don't "go to middleware handlers". If you've got some middleware intercepting URL resolution, you should post it. Commented Oct 4, 2011 at 22:45

3 Answers 3

1

I'm not sure I understood you correctly... are you saying your view doesn't get called?

If you've set up some middleware classes, it's only normal that the flow that handles a request passes through the middleware before (and after), it gets to your view.

Here's some documentation:

https://docs.djangoproject.com/en/dev/topics/http/middleware/?from=olddocs

Inside your middleware class you should be able to see what view function is going to get called by the url resolver, when it gets to the method: process_view(self, request, view_func, view_args, view_kwargs)

Please let me know if I didn't quite understand your question.

Sign up to request clarification or add additional context in comments.

1 Comment

To be honest, this middleware was not written by me, and my experience with it is minimal. I just know that when i added a regex match for the getdata view, i could not get it to work. The one for the index view abomve works just fine.
1

getdata view returns string as response instead of HttpResponse object so process_response in middleware gets string and string doesn't have status_code method. Change your view to:

def getdata(request, resource):
    #...
    return HttpResponse(r.content, mimetype="text/plain")

1 Comment

now i get this: <html> <head></head> <body> 500 Internal Server Error </body> </html>
0

Figured it out. Django was actually hitting the view correctly. There was an error being thrown by the api because of a malformed url request made from within the getdata view. Quite dumb on my part.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.