开发者

Regex match in urls.py for django app view

开发者 https://www.devze.com 2023-04-10 11:55 出处:网络
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

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 s开发者_StackOverflowet 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.


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.


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")


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号