Asp。Django中的Net MVC样式的路由

时间:2022-08-26 20:49:01

I've been programming in Asp.Net MVC for quite some time now and to expand a little bit beyond the .Net world I've recently began learning Python and Django. I am enjoying Django but one thing I am missing from Asp.Net MVC is the automatic routing from my urls to my controller actions.

我一直在用Asp编程。Net MVC已经有一段时间了,为了扩展一下。Net世界,我最近开始学习Python和Django。我喜欢《被解救的姜戈》,但有一点我在Asp中漏掉了。Net MVC是从我的url到控制器动作的自动路由。

In Asp.Net MVC I can build much of my application using this single default route:

在Asp。我可以使用这个单一的默认路径来构建我的应用程序:

routes.MapRoute(
      "Default",                                              // Route name
      "{controller}/{action}/{id}",                           // URL with parameters
       new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
 );

In Django I've found myself adding an entry to urls.py for each view that I want to expose which leads to a lot more url patterns than I've become used to in Asp.Net MVC.

在Django中,我为url添加了一个条目。对于我要公开的每个视图,py都会导致比我在Asp中习惯的更多的url模式。净MVC。

Is there a way to create a single url pattern in Django that will handle "[Application]/view/[params]" in a way similar to Asp.Net MVC? Perhaps at the main web site level?

是否有一种方法可以在Django中创建一个url模式,以类似于Asp的方式处理“[Application]/view/[params]”。净MVC吗?也许是在主要的网站层面?

1 个解决方案

#1


2  

View may not only be function, but also a class.

视图不仅是函数,也是类。

You can easily specify some kind of DispatchedView class with __call__ method and dispatch to method according to remaining URI. Also, You can inspire yourself with CherryPy dispatcher.

您可以轻松地使用__call__方法指定某种DispatchedView类,并根据剩余的URI分派到方法。此外,您还可以使用CherryPy dispatcher来激励自己。

However, it's considered beter to use named patterns and have URIs and views completely decoupled.

但是,它被认为是使用命名模式,并且有uri和视图完全解耦。

#1


2  

View may not only be function, but also a class.

视图不仅是函数,也是类。

You can easily specify some kind of DispatchedView class with __call__ method and dispatch to method according to remaining URI. Also, You can inspire yourself with CherryPy dispatcher.

您可以轻松地使用__call__方法指定某种DispatchedView类,并根据剩余的URI分派到方法。此外,您还可以使用CherryPy dispatcher来激励自己。

However, it's considered beter to use named patterns and have URIs and views completely decoupled.

但是,它被认为是使用命名模式,并且有uri和视图完全解耦。