Peter Møller
Peter Møller

Reputation: 147

Need help regarding design of Django urls and views

Apologies if this has been discussed before, have searched and searched but didn't find anything useful :)

But here goes. We're currently in the process of rewriting a portion of our webapp. Our app is rather old and therefore suffers from some rather cowboy'ish approaches to programming, conventions and urls.

What we're looking for is a simple clean way to design our views and urls so that we can maintain both easier in the future.

The problem is; as of now our urls.py file for the main site is one big mess. a lot of urls that point to a unique view that only does one thin. Ex. list_books/, edit_book/ etc. when it comes to specific formats etc. we have something like list_books_json/

(these aren't the actual urls though, but just used to prove a point since the real urls are much worse)

What we want to do now is clean it up a bit. And we we're wondering what the best way to get around it would be??

What we have thought of so far(after reading a lot of things on the subject):

We've thought of designing our urls after the following pattern: domain/object/action/

so the urls for the apps "staff" site for changing books in the app would be: staff/books - to view all books (GET) staff/books/ID - to view one books (GET) staff/books/new - to create a new book (POST) staff/books/ID/edit - to edit specific books (POST) staff/books/ID/delete - to delete specific books (POST)

The thought was then to have only 1 view, views.staff_books() to handle all these actions when dealing with books through the "staff" part of the site. so that staff_books() checks for ID or a certain "action" (edit, new, delete etc.)

The result would be fewer, but a lot larger views that have to handle all aspects of staff/books. Right now we have a ton of small views that handle only one thing.

Does this makes sense, can you see potential problems? How do you guys go about it??

One place where I think we're lost is in regards to formats. Where would you put ex. the request for returning the response in json? we're wondering "staff/books.json" or "staff/books/ID.json" etc. and then keeping all the json logic in the same "staff_books()" view.

So thats it basically. I'm sorry the question is a little "fluffy"... We basically need some examples or good design advice as to how to structure urls and views.

Kind Regards

pete

Upvotes: 2

Views: 149

Answers (1)

Juicef
Juicef

Reputation: 316

As an extension (and solution) to your problem I would suggest to use the strategy pattern. Since you already have a structure and the only thing that differs is "how" it is supposed to be carried out, this pattern fits your problem perfectly. What I mean by that is the following:

  1. Create a view which is your entry point to your application with functions named as your url-based functionality (edit, new, delete etc.). I.e where your url.py determines where to go from there.
  2. Create classes which do your stuff based on your domains etc. Lets call them Book, Calendar etc for now.
  3. Implement functionality of those classes, like edit, new, delete etc.
  4. in your view then, determine what class to instantiate and call the corresponding function, e.g in View.edit() call domain.edit()

I think that should do it ^^ Hope it helps :D

Upvotes: 1

Related Questions