मैं django के लिए निश्चित मार्गदर्शिका पढ़ रहा हूं और टेम्पलेट विरासत पर अध्याय 4 में हूं। ऐसा लगता है कि मैं कुछ भी ऐसा नहीं कर सकता जैसा कि संभव हो सकता है क्योंकि मुझे बच्चे के दृश्य को कॉल करते समय संदर्भ के लिए कुछ कोड डुप्लिकेट करना पड़ रहा है। Views.py:django टेम्पलेट विरासत और संदर्भ
def homepage(request):
current_date = datetime.datetime.now()
current_section = 'Temporary Home Page'
return render_to_response("base.html", locals())
def contact(request):
current_date = datetime.datetime.now()
current_section = 'Contact page'
return render_to_response("contact.html", locals())
प्रत्येक कोड में current_date लाइन को शामिल करने के लिए अनावश्यक लगता है।
<html lang= "en">
<head>
<title>{% block title %}Home Page{% endblock %}</title>
</head>
<body>
<h1>The Site</h1>
{% block content %}
<p> The Current section is {{ current_section }}.</p>
{% endblock %}
{% block footer %}
<p>The current time is {{ current_date }}</p>
{% endblock %}
</body>
</html>
और एक बच्चे टेम्पलेट फ़ाइल:
यहाँ आधार html फ़ाइल है कि कॉल मुखपृष्ठ है
{% extends "base.html" %}
{% block title %}Contact{% endblock %}
{% block content %}
<p>Contact information goes here...</p>
<p>You are in the section {{ current_section }}</p>
{% endblock %}
जब बच्चे फ़ाइल बुला, जहां मैं CURRENT_DATE लाइन नहीं लगाते हैं तो वह चर दिखाना चाहिए खाली है।