2010-09-15 15 views
5

मैं 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 लाइन नहीं लगाते हैं तो वह चर दिखाना चाहिए खाली है।

उत्तर

16

आप एक Context Processor का उपयोग करके हर टेम्पलेट के लिए एक चर पारित कर सकते हैं:

1. अपनी सेटिंग्स दर्ज करने के लिए

पहले संदर्भ प्रोसेसर जोड़ा जा रहा है, तो आप करने की आवश्यकता होगी करने के लिए जोड़ने अपने कस्टम संदर्भ प्रोसेसर अपने settings.py:

# settings.py 

TEMPLATE_CONTEXT_PROCESSORS = (
    'myapp.context_processors.default', # add this line 
    'django.core.context_processors.auth', 
) 

से आप प्राप्त कर सकते हैं कि है कि आप एक मॉड्यूल context_processors.py कहा जाता है बना सकते हैं और यदि आपके ऐप की fol अंदर यह जगह करने की आवश्यकता होगी डेर। आप आगे देख सकते हैं कि इसे default नामक एक फ़ंक्शन घोषित करने की आवश्यकता होगी (जैसा कि हमने settings.py में शामिल किया है), लेकिन यह मनमाने ढंग से है। आप जो भी फ़ंक्शन नाम पसंद करते हैं उसे चुन सकते हैं।

2. संदर्भ प्रोसेसर

# context_processors.py 

from datetime import datetime 
from django.conf import settings # this is a good example of extra 
            # context you might need across templates 
def default(request): 
    # you can declare any variable that you would like and pass 
    # them as a dictionary to be added to each template's context: 
    return dict(
     example = "This is an example string.", 
     current_date = datetime.now(),     
     MEDIA_URL = settings.MEDIA_URL, # just for the sake of example 
    ) 

3. अपने विचारों

अंतिम चरण RequestContext() का उपयोग कर अतिरिक्त संदर्भ की प्रक्रिया और टेम्पलेट के लिए यह एक चर के रूप में पारित करने के लिए है करने के लिए अतिरिक्त संदर्भ जोड़ना बनाना ।

# old views.py 
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()) 


# new views.py 
from django.template import RequestContext 

def homepage(request): 
    current_section = 'Temporary Home Page' 
    return render_to_response("base.html", locals(), 
           context_instance=RequestContext(request)) 

def contact(request): 
    current_section = 'Contact page' 
    return render_to_response("contact.html", locals(), 
           context_instance=RequestContext(request)) 
3

तो, आप django.views उपयोग कर सकते हैं, render_to_response के बजाय generic.simple.direct_to_template: नीचे views.py फाइल करने के लिए संशोधन की तरह है कि आवश्यकता होगी की एक बहुत ही साधारण उदाहरण है। यह RequestContext internaly का उपयोग करता है।

from django.views,generic.simple import direct_to_template 

def homepage(request): 
    return direct_to_template(request,"base.html",{ 
     'current_section':'Temporary Home Page' 
    }) 

def contact(request): 
    return direct_to_template(request,"contact.html",{ 
     'current_section':'Contact Page' 
    }) 

या आप भी इसे सीधे urls.py जैसे

urlpatterns = patterns('django.views.generic.simple', 
    (r'^/home/$','direct_to_template',{ 
     'template':'base.html' 
     'extra_context':{'current_section':'Temporary Home Page'},   
    }), 
    (r'^/contact/$','direct_to_template',{ 
     'template':'contact.html' 
     'extra_context':{'current_section':'Contact page'},   
    }), 
0

Django v1.8 + चर context processor अंदर लौटे लिए कम से निर्दिष्ट कर सकते हैं पहुँचा जा सकता है।

1. अंदर settings.py

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 

       'your_app.context_processor_file.func_name', # add this line 

      ], 
     }, 
    }, 
] 

2 अपने TEMPLATES सूची में संदर्भ प्रोसेसर जोड़ें।संदर्भ प्रोसेसर के लिए नई फ़ाइल बनाएं और संदर्भ के लिए विधि को परिभाषित

context_processor_file.py

def func_name(request): 
    test_var = "hi, this is a variable from context processor" 
    return { 
    "var_for_template" : test_var, 
    } 

3. अब आप किसी भी टेम्पलेट्स

उदाहरण के लिए

में var_for_template प्राप्त कर सकते हैं, अंदर इस पंक्ति जोड़ें: base.html

<h1>{{ var_for_template }}</h1> 

इस प्रस्तुत करना होगा:

<h1>hi, this is a variable from context processor</h1>

को अद्यतन करने के लिए टेम्पलेट्स का पालन 1.8+ django को this django doc

संबंधित मुद्दे