django url rule not working - django-1.3

I'm a django newbie. Here is my problem.... My main urls.py has a rule for checking published section to published app, like this:
(r'^(published/)$', include('published.urls')),
My published app urls.py is like:
urlpatterns = patterns('published.views',
# Examples:
(r'^$', 'index',),
(r'^(?P<id>\d+)/$', 'article'),
)
I'm trying to fetch a url like this
http://localhost:8000/published/2/
Problem is its showing a 404 error. Just to clarify my view is like this:
def article(request):
try:
p = Published.objects.get(pk = id)
except Published.DoesNotExist:
raise Http404
return render_to_response('published/inner.html', {'pubs': p}, context_instance = RequestContext(request))
Can anyone tell me what is the problem?

You should try removing the $ from the end of r'^(published/)$'. If it still does not work, try removing the ^ from the beginning of r'^(?P<id>\d+)/$'. Since $ matches the end of a string, the regex fails to match when there is a character after the first /.

Related

Django Tastypie prevent file uri's being saved to a FileField

I've got a Django app with Tastypie, and mainly BackBone client side. One of my models has a few ImageFields. Here is a similar setup to help me explain the issue.
settings.py
MEDIA_URL = "/media/"
models.py
class Foo(models.model):
bar = models.ImageField()
baz = models.CharField()
api.py
class FooResource(ModelResource):
class Meta:
queryset=models.Foo.objects.all()
resource_name = "foo"
authorization = Authorization()
When I make a GET request to the API, it appends the MEDIA_URL to the file names to return the URI where a bar can be accessed. However, when I change the value of baz on a row, and then make a PUT request with that, it also changes the value for a bar to the URI. This means that the next time I GET the row, it appends the MEDIA_URL again, breaking the system and appending it for each successive GET and PUT. I end up with values for bar in the DB that look like.
/media/media/media/bar.jpg
I think I should fix this by overriding a method in my ModelResource, so that when there is a PUT request, it recognizes that it's getting either a URI or a real file, and alters its behavior in some way.
Is this the correct fix? Could you provide some implementation details of a fix?
I found the answer. Tastypie is well designed, similarly to Django. Unfortunately I was not familiar with the terminology so when I read the docs I didn't understand. You can easily modify behavior of the API at many levels. Here is my new API definition, which fixed the issue.
api.py
class FooResource(ModelResource):
class Meta:
queryset=models.Foo.objects.all()
resource_name = "foo"
authorization = Authorization()
def hydrate_bar(bundle):
bundle["bar"] = bundle["bar"].strip(MEDIA_URL)
return bundle
I should add that this only works for me because I exclusively POST my image files individually with a post_detail method which doesn't call this method. If I was to POST or PUT image files as part of the entire row, I expect this might raise an error if that isn't considered.

Gae, webapp2 url with multiple parameters

I am currently work on a web app using webapp2, that deals with restaurant in several cities. Some of the url would look like
1. www.example.com/newyork
2. www.example.com/newyork/fastfood
3. www.example.com/newyork/fastfood/tacobell
To handle the first url, I used the following
CITY_RE = r'(/(?:[a-zA-Z0-9]+/?)*)'
app = webapp2.WSGIApplication([(CITY_RE, CityHandler)], debug = True)
How would I handle the url with multiple parameters such as 2 and 3.
I have a similar approach to match urls like /<country>/<region>/<city>/<category>e.g. /usa/california/losangeles/restaurants where I use this regex:
app = webapp2.WSGIApplication([('/([^/]+)/?([^/]*)/?([^/]*)', RegionSearch)], config=settings.w2config, debug=True)
The declare the relevant parameters in the handler class.
class RegionSearch(SearchBaseHandler):
"""Handles regional search requests."""
def get(
self,
region=None,
city=None,
category=None,
subcategory='For sale',
PAGESIZE=50, # items on page
limit=60, # number of days
year=2012,
month=1,
day=1,
next_page=None,
):
I think that you could even do it this way
webapp2.Route('/passwdresetcomplete/<city>/<category>/<name>', handler=RegionSearch, name='regionsearch')

uri_for fails to build name for Route

I am trying to build a uri using uri_for and a named Route.
This is my barebones code:
app = webapp2.WSGIApplication([
(r'/screenshot_worker', 'mkt.ScreenshotWorker'),
webapp2.Route(r'/screenshot/<screenshot_key>',
handler='mkt.ScreenshotHandler', name='screenshot')
])
and the handler
class ScreenshotWorker(webapp2.RequestHandler):
def post(self):
d = date.today().strftime('%d%m%Y')
key = ndb.Key('Screenshot', d)
url = self.uri_for('screenshot', screenshot_key=key) #fails
which fails with
KeyError: "Route named 'screenshot' is not defined."
The handler you have defined and built URI is ScreenshotWorker , whereas for the Route named screenshot, the handler mentioned is ScreenshotHandler
It looks like there is an issue with your route definitions. I'm not sure if it's a typo from your code excerpting or not, but the line:
(r'/screenshot_worker', 'mkt.ScreenshotWorker'),
is missing the leading webapp2.Route (which your second definition correctly uses), which may be breaking your entire url map.

How to redirect from appspot domain to custom domain?

I found this post from Amir in regards to redirecting request from google.appspot domain to the custom domain. My question is where do you put something like this using Web2py?
**To just add a custom domain, just follow the instructions here: http://code.google.com/appengine/articles/domains.html
And once that works, you can put a check in your code to forward anyone landing on the appspot.com domain to your domain: (example in python)
def get(self):
if self.request.host.endswith('appspot.com'):
return self.redirect('www.jaavuu.com', True)
# ... your code ...**
At the beginning of your first model file, you can do:
if request.env.http_host.endswith('appspot.com'):
redirect(URL(host='www.yourdomain.com', args=request.args, vars=request.vars))
This will preserve the entire original URL, except for replacing yourdomain.appspot.com with www.yourdomain.com. Note, URL() will automatically fill in the current controller and function, but you have to explicitly pass the current request.args and request.vars to make sure they get preserved.
That goes into your request handler.
Using example from web2py documentation:
Example 8
In controller: simple_examples.py
def redirectme():
redirect(URL('hello3'))
You'd want to do something like this:
def some_function():
if request.env.http_host.endswith('appspot.com'):
redirect(URL('www.yourdomain.com'))
With webapp2 here is something like what I did, where BaseHandler is the type of all my handlers:
class BaseHandler(webapp2.RequestHandler):
def __init__(self, request, response):
self.initialize(request, response)
if request.host.endswith('appspot.com'):
query_string = self.request.query_string
redirect_to = 'https://www.example.com' + self.request.path + ("?" + query_string if query_string else "")
self.redirect(redirect_to, permanent=True, abort=True)

Catch-all routing using Tipfy

Using tipfy, how does one express a catch-all route in urls.py if more specific routes do not match?
Tipfy uses Werkzeug-like routing, so there's this (in urls.py):
def get_rules(app):
rules = [
Rule('/<any>', endpoint='any', handler='apps.main.handlers.MainHandler'),
Rule('/', endpoint='main', handler='apps.main.handlers.MainHandler'),
]
This will match most random entry points into the application (app.example.com/foo, app.example.com/%20 etc) but does not cover the app.example.com/foo/bar case which results in a 404.
Alternatively, is there a graceful way to handle 404 in Tipfy that I'm missing?
I think you want:
Rule('/<path:any>', endpoint='any', handler='apps.main.handlers.MainHandler')
The path matcher also matches slashes.
Maybe you could write custom middle ware:
class CustomErrorPageMiddleware(object):
def handle_exception(self, e):
return Response("custom error page")
To enable it add somewhere to tipfy config:
config['tipfy'] = {
'middleware': [
'apps.utils.CustomErrorPageMiddleware',
]
}
It gives you quite a flexibility - you could for example send mail somewhere to inform that there was a problem. This will intercept all exceptions in your application

Resources