0Pricing
Flask Academy · درس

render_template ومجلد templates

أعِد ملف HTML بدلًا من سلسلة نصية.

render_template ومجلد templates درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

From Strings to Pages

So far your routes returned plain strings. Real pages need HTML, and Flask gives you render_template to serve full HTML files cleanly.

What render_template Does

render_template finds an HTML file, runs it through the template engine, and returns the finished page as your response body.

from flask import render_template

@app.route('/')
def home():
    return render_template('index.html')

The templates Folder

Flask looks for your HTML in a folder named templates next to your app file. Put index.html there and Flask finds it automatically. 📁

myapp/
  app.py
  templates/
    index.html

Why a Fixed Name

The name templates is a convention, not a guess. Flask knows where to look, so you never write the folder path inside render_template.

Reference Only the Filename

You pass just the filename, not a full path. Flask joins it onto the templates folder for you, keeping your code short and portable.

return render_template('about.html')

Subfolders Inside templates

You can group pages in subfolders and reference them with forward slashes. This keeps a large project tidy as it grows.

return render_template('users/profile.html')

A Minimal Template

A template is ordinary HTML at heart. The simplest one is just a regular page with no special syntax yet.

<!doctype html>
<title>Home</title>
<h1>Welcome</h1>

Templates Get Rendered

Even a plain file is rendered: Flask processes it through Jinja2 first. That step is what lets you add dynamic pieces later.

Returning the Result

render_template returns a string of HTML, which you return from your view. Flask wraps it in a proper response with the right content type.

@app.route('/')
def home():
    return render_template('index.html')

TemplateNotFound Errors

If Flask cannot find the file, you get a TemplateNotFound error. Check the spelling and that the file truly sits inside templates.

Separating Logic and Markup

Templates keep HTML out of your Python. Views handle logic, templates handle presentation, and that split keeps both far easier to read.

Quick Check

Where does Flask expect your HTML files to live by default?

Recap

You learned that render_template serves HTML from the templates folder by filename, so logic stays in Python and markup stays in files. Nice work! 🎉

الأسئلة الشائعة

هل درس «render_template ومجلد templates» مجاني؟

نعم — نص درس «render_template ومجلد templates» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «render_template ومجلد templates»؟

أعِد ملف HTML بدلًا من سلسلة نصية. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «render_template ومجلد templates»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. render_template ومجلد templates
  2. تمرير البيانات إلى قالب
  3. الحلقات والشروط في Jinja2
  4. التحويل التلقائي ومرشّح safe
← العودة إلى Flask Academy