Opened 18 years ago

Closed 17 years ago

Last modified 17 years ago

#2784 closed enhancement (wontfix)

extend URL resolver support for HTTP Methods

Reported by: simon@… Owned by: Adrian Holovaty
Component: Core (Other) Version: dev
Severity: normal Keywords: HTTP Methods
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This patch introduces HTTP method support into the URL resolver allowing for easy creating of REST style applications.
At the urls file you can specify which view is supposed to respond to what HTTP method. Currently only POST,GET,PUT,DELETE are supported and those are specified in django.conf.urls.defaults

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    # Example:
    (r'^django_rest_urls/get/(?P<id>\d+)', 'django_rest_urls.restful.views.get'),
    (r'^django_rest_urls/(?P<id>\d+)', {
											'GET':		'django_rest_urls.restful.views.get',
											'POST':		'django_rest_urls.restful.views.post',
											'PUT':		'django_rest_urls.restful.views.put',
											'DELETE':	'django_rest_urls.restful.views.delete',
										}),

    # Uncomment this for admin:
#     (r'^admin/', include('django.contrib.admin.urls')),
)


Attachments (1)

http-methods-patch.diff (4.3 KB ) - added by simon@… 18 years ago.
HTTP Method support in URLs patch

Download all attachments as: .zip

Change History (7)

by simon@…, 18 years ago

Attachment: http-methods-patch.diff added

HTTP Method support in URLs patch

comment:1 by simon@…, 18 years ago

Your views won't change, they'll just be like so:

# Create your views here.
from django.http import HttpResponse

def get(request,id):
	return HttpResponse("get %s\n" % id)


def post(request,id):
	return HttpResponse("post %s\n" % id)


def put(request,id):
	return HttpResponse("put %s\n" % id)


def delete(request,id):
	return HttpResponse("delete %s\n" % id)


comment:2 by simon@…, 18 years ago

Version: 0.95SVN

Sorry - the version is SVN not 0.95

comment:3 by Malcolm Tredinnick, 18 years ago

I think this is doing too much overloading of things inside of patterns() -- using dictionaries as the second argument gives vastly different behaviour to using strings or functions.

The idea itself might have some merit, although it can be pretty easily done via a view dispatcher, so it's not blindingly urgent right now. Could you bring it up on the django-developers list for some discussion about possible syntax, please? Most of the main developers are quite busy at the moment, so you may need to wait a day or two for a response, but this proposal needs a bit more discussion before it can be accepted. Off the top of my head, I can't think of a syntax that seems reasonable, but I'd be interested in hearing a few ideas from other people.

comment:4 by simon@…, 18 years ago

You are probably right - my patchy implementation is too complex, the fruit of hacking it together in a few minutes. But I still like the idea of specifying a view per HTTP method - seems cleaner than doing an 'if request.POST' inside the view.

I'll pitch it in django-developers and I'd be interested in the discussion.

comment:5 by Adrian Holovaty, 17 years ago

Resolution: wontfix
Status: newclosed

This is a bit too hackish. Please bring this up on the django-developers mailing list first.

Note: See TracTickets for help on using tickets.
Back to Top