Code

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#2784 closed enhancement (wontfix)

extend URL resolver support for HTTP Methods

Reported by: simon@… Owned by: adrian
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: UI/UX:

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@… 8 years ago.
HTTP Method support in URLs patch

Download all attachments as: .zip

Change History (7)

Changed 8 years ago by simon@…

HTTP Method support in URLs patch

comment:1 Changed 8 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 8 years ago by simon@…

  • Version changed from 0.95 to SVN

Sorry - the version is SVN not 0.95

comment:3 Changed 8 years ago by mtredinnick

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 8 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 8 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.