最像ASP.NET MVC 3的Python Web框架

7 投票
1 回答
1525 浏览
提问于 2025-04-17 12:55

虽然我很喜欢在ASP.NET MVC上开发,但现在是时候离开Windows了。

我想换成一个基于Python的框架,希望这个过程尽量简单。

不讨论换框架的好处或原因,我想知道哪个Python的网页框架在架构上最像ASP.NET MVC 3。

架构示例

我说的是流程,而不是语言。

典型的.NET路由

routes.MapRoute( // maps requests at /Product/ to ProductController
    "Products", // Route name
    "Product/{action}/{id}", // URL with parameters
    new { controller = "Product", action = "Index", id = UrlParameter.Optional }
        // Parameter defaults
);

典型的.NET控制器

public class ProductController
{
    public ActionResult Index(IndexInputModel inputModel)
    {
        // do something with inputModel ...
        var viewModel = new ProductIndexViewModel()
        {
            Products = productList;
        }
        return View("~/Views/Product/Index.cshtml", viewModel);
    }
    // ...
}

典型的~/Views/Product/Index.cshtml .NET Razor视图

@model ProductIndexViewModel

<h2>Products</h2>
@foreach (var product in Model.Products)
{
    <h3>@product.Title</h3>
    <p>@product.Description</p>
    <span class="price">@product.Price</span>
}

1 个回答

2

Django 和其他框架有一些相似之处。但是,Python 这种语言不像 .Net 那样严格,所以无论你选择哪个框架,你都会发现它们之间有不少不同之处。

撰写回答