2013-04-02 26 views
6

Có khả năng nào để viết vào các thông tin chi tiết về chế độ xem được tạo về tạo mẫu trong chế độ gỡ lỗi không? Ví dụ nó có thể tạo ra sản lượng như:Chế độ tiết trong các thẻ mẫu Django

base.html:

<html> 
<body> 
{% block content %} 
{% endblock %} 
</body> 
</html> 

page.html:

{% extend "base.html" %} 
{% block content %} 
Foo 
{% include "inner.html" %} 
Bar 
{% endblock %} 

Into hình thức như:

<html> 
<body> 
<!-- block content --> 
<!-- from "page.html" --> 
Foo 
<!-- include "inner.html" --> 
Bar 
<!-- endblock content --> 
</body> 
</html> 

Tại sao? Bởi vì đôi khi khám phá một số phụ thuộc lớn hơn là rất khó chỉ bởi IDE. Hoặc có thể bạn biết một số công cụ tốt để điều hướng dễ dàng hơn (tạo ra đồ thị, vv)? Tất nhiên thông tin này chỉ nên được tạo trong chế độ gỡ lỗi. Trong quá trình sản xuất, chúng sẽ biến mất.

+2

Câu hỏi hay! Bạn có thể thấy nếu ['django-debug-toolbar'] (https://github.com/django-debug-toolbar/django-debug-toolbar) hoặc [' django-template-repl'] (https: // github .com/codysoyland/django-template-repl) rất hữu ích. –

+0

Đối với tôi 'django-template-repl' là vô dụng, bởi vì tôi cần phải nhập toàn bộ cây của tập tin vào nó và nếu tôi biết câu trả lời cho tập tin nào là xấu, tôi sẽ có câu trả lời của tôi mà không có nó. Tôi tạo ra câu hỏi này vì ma thuật templatetags trong mã mà bây giờ tôi duy trì, mà đang sử dụng ma thuật tùy thuộc vào mô hình được chèn vào (các mẫu như danh sách: '(" {app_label}/{model}/{template} .html "," {template} .html ")') và đầu ra quá giống với mẫu riêng biệt sử dụng nội dung của nó. 'django-debug-toolbar' là gần, nhưng không đủ, bây giờ tôi đang sử dụng nó. – zwierzak

+0

phép thuật là điều ác. bạn đã nghĩ về một kịch bản sed nhỏ để thêm bình luận html sau mỗi khối/bao gồm và befor mỗi endblock? – ornoone

Trả lời

2

Bạn có thể đạt được điều này bằng cách sử dụng middlware. Tôi đã có một vấn đề tương tự một khi trở lại theo dõi các mẫu và các quan điểm gọi cho họ vì vậy tôi đã viết một đoạn mã trung gian mà thêm một khối bình luận để phía trên cùng của phản ứng html. Nó không hoàn toàn làm những gì bạn đang yêu cầu nhưng bạn có thể thích ứng với nó.

COMMENT_BLOCK = """ 
<!-- 
[ url  ] >> http://%(host)s%(path)s 
[ referer ] >> %(referer)s 
[ module ] >> %(module)s 
[ function ] >> %(function)s, line %(line)s 
[ args  ] >> args=%(args)s, kwargs=%(kwargs)s, defaults=%(defaults)s 
[ template ] >> %(template)s 
--> 

""" 

# Add any additional template types you wish to add the comment block to. 
MIMETYPES = (
    "text/html", 
    "text/xml", 
) 


class HtmlTemplateFinder: 

    def __init__(self): 
     self.host = None 
     self.referer = None 
     self.path = None 
     self.module = None 
     self.function = None 
     self.line = None 
     self.args = None 
     self.kwargs = None 
     self.defaults = None 
     self.template = None 
     self.valid_template = False 

    def _populate_comment_block(self): 
     return COMMENT_BLOCK % { 
           'host': self.host, 
           'referer': self.referer, 
           'path': self.path, 
           'module': self.module, 
           'function': self.function, 
           'line': self.line, 
           'args': self.args, 
           'kwargs': self.kwargs, 
           'defaults': self.defaults, 
           'template': self.template, 
           } 

    def process_view(self, request, view_func, view_args, view_kwargs): 
     self.host = request.META.get('HTTP_HOST', None) 
     self.referer = request.META.get('HTTP_REFERER', None) 
     self.path = request.path 
     self.module = view_func.func_code.co_filename 
     self.function = ('.').join((view_func.__module__, view_func.func_name)) 
     self.line = view_func.func_code.co_firstlineno 
     self.args = view_args 
     self.kwargs = view_kwargs 
     self.defaults = view_func.func_defaults 
     return None 

    def process_template_response(self, request, response): 
     from mimetypes import guess_type 
     # Use this rather than response.template_name, this always returns str 
     self.template = response.resolve_template(response.template_name).name 
     self.valid_template = guess_type(self.template)[0] in MIMETYPES 
     return response 

    def process_response(self, request, response): 
     from <your app> import settings 
     if settings.DEBUG: 
      if self.valid_template: 
       block = self._populate_comment_block() 
       response.content = "%s%s" % (block, response.content) 
     return response 
Các vấn đề liên quan