Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django does it make sense to define get_create_url(), get_update_url() and get_delete_url() for object

Every object which have unique url should have get_absolute_url() which usually returns detail view of the object. Often those kind of objects also have create, update and delete views and urls need some parameters to work. You can provide parameters to those urls in your templates but if you ever change your url structure etc. you have to change all your templates also instead of that one function. So: Is it good practice to define methods for create, edit and delete urls as well or are there some problems with that approach?

Consider an example where I have a blog and blog owner can add posts to it, edit and delete them. Post has a foreign key to blog. Detail template for post could contain following links:

<a href="{% url 'post-edit' blog=post.blog.pk pk=post.pk %}">edit post</a>
<a href="{% url 'post-delete' blog=post.blog.pk pk=post.pk %}">delete post</a>
<a href="{{ post.blog.get_absolute_url }}">back to blog</a>

and urls:

url(r'^blog-(?P<blog>\d+)/post-(?P<pk>[-\w]+)/$', views.PostView.as_view(),  name='post-view'),
url(r'^post-new/blog-(?P<blog>\d+)/$', views.CreatePostView.as_view(), name='post-new'),
url(r'^post-edit/blog-(?P<blog>\d+)/post-(?P<pk>\d+)/$', views.UpdatePostView.as_view(), name='post-edit',),
url(r'^post-delete/blog-(?P<blog>\d+)/post-(?P<pk>\d+)/$', views.DeletePostView.as_view(), name='post-delete',),

Not the best possible example but I hope you got my point.

like image 791
m5seppal Avatar asked Oct 19 '25 08:10

m5seppal


1 Answers

At least it is all up to you how you're designing your internal naming.

For me, as a long term djangoist, I'm using the very same and had never a problem. My team also loves this "development paradigm".

We're including the apps urls.py into the root urls.py and name the urls almost the same like you...except we're adding, if needed, the applications name into the url-name, too. In your example this would be blog-post-new or blog-post-delete.

example

urlpatterns = patterns('service.views',
    # /services/1/edit/
    url(r'^(?P<service_pk>\d+)/edit/$',
        view = 'edit',
        name = 'service-edit',
    ),
    # /services/add/
    url(r'^add/$',
        view = 'add',
        name = 'service-add',
    ),
    # /services/1/delete/
    url(r'^(?P<service_pk>\d+)/delete/$',
        view = 'delete',
        name = 'service-delete',
    ),
    # /services/1/permissions/
    url(r'^(?P<service_pk>\d+)/permissions/$',
        view = 'permissions',
        name = 'service-permissions',
    ),
)

So, do what ever feels right for you - there is no "wrong", just keep in mind that there maybe will come another app into your project with "post" ;)

like image 157
Thomas Schwärzl Avatar answered Oct 20 '25 21:10

Thomas Schwärzl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!