Using classes for Django views, is it Pythonic?

I am currently learning Python and based on a strong C # background. I hear all the time about how to do things along the pythonic path in order to use the dynamic nature of the language, and some of them I get, and some not.

I am building a site with Django, and my approach to views is using classes. My real thinking is to have a base class that contains some information about the template and model to use. This will have a default funky 404 page, with a site search, etc. Then all other pages will be delayed. Thus, each area of ​​the site will have its own EG news, and all model-related functions and filtering will be in this class with an additional class on top of them for HTML or AJAX requests. So you will have something like this:

\ site \ Common \ ViewBase

- \ News \ NewsBase (ViewBase)

- \ News \ HtmlView (NewsBase)

- \ News \ AJAXView (NewsBase)

URLs will display as http: // tld / news / latest on .news.htmlview and http: // tld / news / / to / will also display site.news.htmlview, but the class will determine what to do with additional parameters.

This is pretty much what I would do in C #, but the Django tutorial only shows methods for views, making me wonder if this is a pythonic solution too?

Thoughts?

Edit: after S.Lott's comment on thread safety, is it better to leave the functions as they are and create an instance of the class and call a method on it?

What I'm looking for is a place where you can place a common code for each section of the site for filtering the model, authentication for the site, etc.

+3
4

, , URL- , .

+4

Django - django/contrib/admin.

, , , .

, 1.2, .

, - , , , "".

+2

(, ), , Model/View/Template.

, , url ( , :-). , .

0

, . - . , , / (python) , .

, , , , , .

p.s. .

0

Source: https://habr.com/ru/post/1728605/


All Articles