Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#2784 closed enhancement (wontfix)

extend URL resolver support for HTTP Methods

Reported by: simon@… Owned by: Adrian Holovaty
Component: Core (Other) Version: master
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


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':		'',
											'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@… 13 years ago.
HTTP Method support in URLs patch

Download all attachments as: .zip

Change History (7)

Changed 13 years ago by simon@…

Attachment: http-methods-patch.diff added

HTTP Method support in URLs patch

comment:1 Changed 13 years ago by simon@…

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 Changed 13 years ago by simon@…

Version: 0.95SVN

Sorry - the version is SVN not 0.95

comment:3 Changed 13 years ago by Malcolm Tredinnick

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 Changed 13 years ago by simon@…

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 Changed 12 years ago by Adrian Holovaty

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