javascript 您可以使用 Django 框架将 Python 用于前端和后端吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46016736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you use Python for both front end and back end using Django framework?
提问by Beginner Programmer
I'm watching the udemy Django tutorial that requires using JavaScript as the front-end and Python for the back-end:
我正在看 udemy Django 教程,该教程需要使用 JavaScript 作为前端,使用 Python 作为后端:
Can you replace JavaScript with Python?
What are the advantages and disadvantages of that?
你能用 Python 代替 JavaScript 吗?
这样做的优点和缺点是什么?
采纳答案by Astik Anand
No, python can't be used in frontend.
不,python 不能用于前端。
You need frontend technologies like html, css, javascript, jQueryetc...
for frontend.
你需要像前端技术html,css,javascript,jQuery等...的前端。
Python can be used as scripting language in backend.
Python 可以用作后端的脚本语言。
回答by d2718nis
As others already said, basically you can't use Python at the front-end. You can use Django Template Languageor something like Jinja2in your templates, but generally speaking these engines run on your server, not in the browser.
正如其他人已经说过的,基本上你不能在前端使用 Python。你可以在你的模板中使用Django 模板语言或类似Jinja2 的东西,但一般来说这些引擎运行在你的服务器上,而不是在浏览器中。
Actually, you canuse Python to write the code, that runs in the browser, using transpilerslike Transcrypt. But I don't recommend you to do such a thing on a real project because of raising complexity and difference between these two languages. The only advantage would be that you can use one language to write both front-end and back-end. But the complexity of supporting this code will not worth it.
其实,你可以在浏览器中使用Python编写的代码,即运行,使用transpilers像Transcrypt。但是我不建议你在真正的项目上做这样的事情,因为这会增加这两种语言之间的复杂性和差异。唯一的优势是您可以使用一种语言来编写前端和后端。但是支持此代码的复杂性将不值得。
回答by Mauricio Cortazar
Its true that you can do something interesting with Django template system. but like @Astik said you cant replace javascript in front end. Personally I don't use javascript for some Django projects because I don't need it, but there are many situation that I have to. However that depends about your project is going to work.
确实,您可以使用 Django 模板系统做一些有趣的事情。但就像@Astik 说的,你不能在前端替换 javascript。就我个人而言,我不将 javascript 用于某些 Django 项目,因为我不需要它,但是有很多情况我必须这样做。但是,这取决于您的项目是否可行。
Something better than Django templates system is jinja2read more about it maybe help you
比 Django 模板系统更好的东西是jinja2阅读更多关于它可能对你有帮助

