你可以在不使用模板的情况下使用Django框架吗?

huangapple go评论73阅读模式
英文:

Can I use Django framework w/o using templates?

问题

我正在学习如何使用Django框架,而且在学习Django的模板系统时,我在思考是否可能在视图中不使用模板来渲染所需的HTML文档。作为Django模板系统的替代方案,我更愿意使用JavaScript来从网站数据库中进行必要的数据查询。

这可能看起来有些不合理,但我个人倾向于先学习传统的前端Web技术栈(HTML、CSS、JS),在掌握这些技能后再深入学习模板系统。在这一阶段,我将仅将Django用作后端,直到准备好将模板纳入我的开发流程为止。

我曾尝试使用Django的模板系统,但与同时学习HTML和CSS相比,它有点复杂。

英文:

I am learning how to use the Django framework, and as I am learning about Django's template system, I was wondering, if it would be possible for me to use Django without using templates to render the necessary html documents from views? As an alternative to Django's template system, I would prefer to use JavaScript in order to make the necessary data queries from the sites database(s).

This might seem counter productive, but in my opinion, I would prefer to learn the traditional front-end web stack (HTML, CSS, JS), and solidify my skills this way before going down the journey of template systems. At this point, I would be using Django only as a back-end until I was ready to incorporate templates into my dev process.

I have tried to use Django's template system, but It's a little comprehensive to learn on top of HTML and CSS at the same time.

答案1

得分: 4

当然,可以在不使用Django的模板系统的情况下使用它,以便从视图中呈现HTML文档。实际上,在Web开发中,将Django与JavaScript框架集成是一种常见做法。

但你需要找到一种方法将前端框架(通常基于Javascript)与后端结合起来。最常用的方法是在你的Django项目中实现REST API端点,并从前端消费它们。

Django并不提供内置的REST API支持,但你可以使用第三方库,如Django REST FrameworkDjango Ninja

英文:

Certainly, it is possible to use Django without utilizing its template system for rendering HTML documents from views. In fact, integrating Django with a JavaScript framework is a common practice in web development.

You will need to find a way to make the frontend framework (usually Javascript based) with the backend, though. The most commonly used method is to implement REST API endpoints in your Django project and consume them from the frontend.

Django does not come with built-in REST API support, but you can use a third party one, such as Django REST Framework or Django Ninja.

huangapple
  • 本文由 发表于 2023年6月9日 10:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定