简体   繁体   English

在源代码中没有HTML混乱的Python CGI?

[英]Python CGI without HTML clutter in source code?

I am new to web development. 我是Web开发的新手。 After much consideration I picked Python over Ruby simply because code readability is important to me. 经过深思熟虑之后,我之所以选择Python而不是Ruby是因为代码的可读性对我很重要。 I have looked at a few Python cgi code and I wondered if it is possible to polish that code in such a manner that I can focus on program functionality without having to have all the HTML formatting code embedded (cluttering) in the Python source, ie separate the actual Python code from the HTML code. 我看了一些Python cgi代码,我想知道是否有可能以一种这样的方式来抛光该代码,即使我可以专注于程序功能,而不必将所有HTML格式设置代码嵌入(混乱)在Python源代码中,即将实际的Python代码与HTML代码分开。 Perhaps this style of programming then leaves the realm of CGI into something else? 也许这种编程风格将CGI的领域留给了别的东西? (ie CGI = script that generates a web page). (即CGI =生成网页的脚本)。

Pick a template engine . 选择一个模板引擎 And it's still CGI, it just happens to be CGI that uses a template engine; 而且它仍然是CGI,恰好是使用模板引擎的CGI。 CGI is the interface, not the programming methodology. CGI是接口,而不是编程方法。

is possible to polish that code in such a manner that I can focus on program functionality without having to have all the HTML formatting code embedded (cluttering) in the Python source 可以以无需关注所有HTML格式代码(杂乱无章)嵌入Python源代码的方式来完善代码,从而专注于程序功能

Use a templating engine 使用模板引擎

Perhaps this style of programming then leaves the realm of CGI into something else? 也许这种编程风格将CGI的领域留给了别的东西?

CGI isn't a style of programming. CGI不是一种编程风格。 It is a means for a web server to execute a program to determine what data to provide to the user. 它是Web服务器执行程序以确定要提供给用户的数据的一种方式。 (It is a slow and inefficient method, but it is also a very simple one). (这是一种缓慢且效率低下的方法,但它也是一种非常简单的方法)。

If you want to get away from CGI and stay with Python, then you should probably look towards wsgi 如果您想摆脱CGI并继续使用Python,那么您应该考虑使用wsgi

New web application code in Python isn't written using CGI, it's based on WSGI . Python中的新Web应用程序代码不是使用CGI编写的,而是基于WSGI的 Separating your HTML generation from the rest of your program logic is indeed a good thing, and should be done using a templating engine. 将HTML生成与程序逻辑的其余部分分开确实是一件好事,应该使用模板引擎来完成。 Python also has several frameworks for writing web applications; Python还有一些用于编写Web应用程序的框架。 if you're just starting out, I recommend Flask . 如果您刚刚开始,我建议Flask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM