Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I created a reusable function for changing block name in Djago templates. I want to know if the code is good or is there any probable issues in it. I developed the function as a hack for the following issue issue.

Template Code

{% extends base_name %}
{% block main-contents %}
 <h2>{{ message_heading }}</h2>
 <div class="alert alert-{{ box_color|default:"info" }}">
 {{ message }}
 {% if btn_1_text and btn_1_url %}
 <a href="{{ btn_1_url }}" class="btn btn-{{ btn_1_color }}">{{ btn_1_text }}</a>
 {% endif %}
 {% if btn_2_text and btn_2_url %}
 <a href="{{ btn_2_url }}" class="btn btn-{{ btn_2_color }}">{{ btn_2_text }}</a>
 {% endif %}
 </div>
{% endblock %}

Function to change block name

def change_block_names(template, change_dict):
 """
 This function will rename the blocks in the template from the
 dictionary. The keys in th change dict will be replaced with
 the corresponding values. This will rename the blocks in the 
 extended templates only.
 """
 extend_nodes = template.nodelist.get_nodes_by_type(ExtendsNode)
 if len(extend_nodes) == 0:
 return
 extend_node = extend_nodes[0]
 blocks = extend_node.blocks
 for name, new_name in change_dict.items():
 if blocks.has_key(name):
 block_node = blocks[name]
 block_node.name = new_name
 blocks[new_name] = block_node
 del blocks[name]
tmpl_name = 'django-helpers/twitter-bootstrap/message.html'
tmpl1 = loader.get_template(tmpl_name)
change_block_names(tmpl1, {'main-contents': 'new-main-contents'})
return HttpResponse(tmpl.render(Context()))

This function seems to work for my issue. But I don't know if this breaks any of the Django's functionality. Can anyone verify this and give me some advice?

I created a reusable function for changing block name in Djago templates. I want to know if the code is good or is there any probable issues in it. I developed the function as a hack for the following issue.

Template Code

{% extends base_name %}
{% block main-contents %}
 <h2>{{ message_heading }}</h2>
 <div class="alert alert-{{ box_color|default:"info" }}">
 {{ message }}
 {% if btn_1_text and btn_1_url %}
 <a href="{{ btn_1_url }}" class="btn btn-{{ btn_1_color }}">{{ btn_1_text }}</a>
 {% endif %}
 {% if btn_2_text and btn_2_url %}
 <a href="{{ btn_2_url }}" class="btn btn-{{ btn_2_color }}">{{ btn_2_text }}</a>
 {% endif %}
 </div>
{% endblock %}

Function to change block name

def change_block_names(template, change_dict):
 """
 This function will rename the blocks in the template from the
 dictionary. The keys in th change dict will be replaced with
 the corresponding values. This will rename the blocks in the 
 extended templates only.
 """
 extend_nodes = template.nodelist.get_nodes_by_type(ExtendsNode)
 if len(extend_nodes) == 0:
 return
 extend_node = extend_nodes[0]
 blocks = extend_node.blocks
 for name, new_name in change_dict.items():
 if blocks.has_key(name):
 block_node = blocks[name]
 block_node.name = new_name
 blocks[new_name] = block_node
 del blocks[name]
tmpl_name = 'django-helpers/twitter-bootstrap/message.html'
tmpl1 = loader.get_template(tmpl_name)
change_block_names(tmpl1, {'main-contents': 'new-main-contents'})
return HttpResponse(tmpl.render(Context()))

This function seems to work for my issue. But I don't know if this breaks any of the Django's functionality. Can anyone verify this and give me some advice?

I created a reusable function for changing block name in Djago templates. I want to know if the code is good or is there any probable issues in it. I developed the function as a hack for the following issue.

Template Code

{% extends base_name %}
{% block main-contents %}
 <h2>{{ message_heading }}</h2>
 <div class="alert alert-{{ box_color|default:"info" }}">
 {{ message }}
 {% if btn_1_text and btn_1_url %}
 <a href="{{ btn_1_url }}" class="btn btn-{{ btn_1_color }}">{{ btn_1_text }}</a>
 {% endif %}
 {% if btn_2_text and btn_2_url %}
 <a href="{{ btn_2_url }}" class="btn btn-{{ btn_2_color }}">{{ btn_2_text }}</a>
 {% endif %}
 </div>
{% endblock %}

Function to change block name

def change_block_names(template, change_dict):
 """
 This function will rename the blocks in the template from the
 dictionary. The keys in th change dict will be replaced with
 the corresponding values. This will rename the blocks in the 
 extended templates only.
 """
 extend_nodes = template.nodelist.get_nodes_by_type(ExtendsNode)
 if len(extend_nodes) == 0:
 return
 extend_node = extend_nodes[0]
 blocks = extend_node.blocks
 for name, new_name in change_dict.items():
 if blocks.has_key(name):
 block_node = blocks[name]
 block_node.name = new_name
 blocks[new_name] = block_node
 del blocks[name]
tmpl_name = 'django-helpers/twitter-bootstrap/message.html'
tmpl1 = loader.get_template(tmpl_name)
change_block_names(tmpl1, {'main-contents': 'new-main-contents'})
return HttpResponse(tmpl.render(Context()))

This function seems to work for my issue. But I don't know if this breaks any of the Django's functionality. Can anyone verify this and give me some advice?

fixed a bug: missed closing single-quote
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

I created a reusable function for changing block name in Djago templates. I want to know if the code is good or is there any probable issues in it. I developed the function as a hack for the following issue.

Template Code

{% extends base_name %}
{% block main-contents %}
 <h2>{{ message_heading }}</h2>
 <div class="alert alert-{{ box_color|default:"info" }}">
 {{ message }}
 {% if btn_1_text and btn_1_url %}
 <a href="{{ btn_1_url }}" class="btn btn-{{ btn_1_color }}">{{ btn_1_text }}</a>
 {% endif %}
 {% if btn_2_text and btn_2_url %}
 <a href="{{ btn_2_url }}" class="btn btn-{{ btn_2_color }}">{{ btn_2_text }}</a>
 {% endif %}
 </div>
{% endblock %}

Function to change block name

def change_block_names(template, change_dict):
 """
 This function will rename the blocks in the template from the
 dictionary. The keys in th change dict will be replaced with
 the corresponding values. This will rename the blocks in the 
 extended templates only.
 """
 extend_nodes = template.nodelist.get_nodes_by_type(ExtendsNode)
 if len(extend_nodes) == 0:
 return
 extend_node = extend_nodes[0]
 blocks = extend_node.blocks
 for name, new_name in change_dict.items():
 if blocks.has_key(name):
 block_node = blocks[name]
 block_node.name = new_name
 blocks[new_name] = block_node
 del blocks[name]
tmpl_name = 'django-helpers/twitter-bootstrap/message.html'
tmpl1 = loader.get_template(tmpl_name)
change_block_names(tmpl1, {'main-contents': 'new-main-contentscontents'})
return HttpResponse(tmpl.render(Context()))

This function seems to work for my issue. But I don't know if this breaks any of the Django's functionality. Can anyone verify this and give me some advice?

I created a reusable function for changing block name in Djago templates. I want to know if the code is good or is there any probable issues in it. I developed the function as a hack for the following issue.

Template Code

{% extends base_name %}
{% block main-contents %}
 <h2>{{ message_heading }}</h2>
 <div class="alert alert-{{ box_color|default:"info" }}">
 {{ message }}
 {% if btn_1_text and btn_1_url %}
 <a href="{{ btn_1_url }}" class="btn btn-{{ btn_1_color }}">{{ btn_1_text }}</a>
 {% endif %}
 {% if btn_2_text and btn_2_url %}
 <a href="{{ btn_2_url }}" class="btn btn-{{ btn_2_color }}">{{ btn_2_text }}</a>
 {% endif %}
 </div>
{% endblock %}

Function to change block name

def change_block_names(template, change_dict):
 """
 This function will rename the blocks in the template from the
 dictionary. The keys in th change dict will be replaced with
 the corresponding values. This will rename the blocks in the 
 extended templates only.
 """
 extend_nodes = template.nodelist.get_nodes_by_type(ExtendsNode)
 if len(extend_nodes) == 0:
 return
 extend_node = extend_nodes[0]
 blocks = extend_node.blocks
 for name, new_name in change_dict.items():
 if blocks.has_key(name):
 block_node = blocks[name]
 block_node.name = new_name
 blocks[new_name] = block_node
 del blocks[name]
tmpl_name = 'django-helpers/twitter-bootstrap/message.html'
tmpl1 = loader.get_template(tmpl_name)
change_block_names(tmpl1, {'main-contents': 'new-main-contents})
return HttpResponse(tmpl.render(Context()))

This function seems to work for my issue. But I don't know if this breaks any of the Django's functionality. Can anyone verify this and give me some advice?

I created a reusable function for changing block name in Djago templates. I want to know if the code is good or is there any probable issues in it. I developed the function as a hack for the following issue.

Template Code

{% extends base_name %}
{% block main-contents %}
 <h2>{{ message_heading }}</h2>
 <div class="alert alert-{{ box_color|default:"info" }}">
 {{ message }}
 {% if btn_1_text and btn_1_url %}
 <a href="{{ btn_1_url }}" class="btn btn-{{ btn_1_color }}">{{ btn_1_text }}</a>
 {% endif %}
 {% if btn_2_text and btn_2_url %}
 <a href="{{ btn_2_url }}" class="btn btn-{{ btn_2_color }}">{{ btn_2_text }}</a>
 {% endif %}
 </div>
{% endblock %}

Function to change block name

def change_block_names(template, change_dict):
 """
 This function will rename the blocks in the template from the
 dictionary. The keys in th change dict will be replaced with
 the corresponding values. This will rename the blocks in the 
 extended templates only.
 """
 extend_nodes = template.nodelist.get_nodes_by_type(ExtendsNode)
 if len(extend_nodes) == 0:
 return
 extend_node = extend_nodes[0]
 blocks = extend_node.blocks
 for name, new_name in change_dict.items():
 if blocks.has_key(name):
 block_node = blocks[name]
 block_node.name = new_name
 blocks[new_name] = block_node
 del blocks[name]
tmpl_name = 'django-helpers/twitter-bootstrap/message.html'
tmpl1 = loader.get_template(tmpl_name)
change_block_names(tmpl1, {'main-contents': 'new-main-contents'})
return HttpResponse(tmpl.render(Context()))

This function seems to work for my issue. But I don't know if this breaks any of the Django's functionality. Can anyone verify this and give me some advice?

deleted 1 character in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

django Django template block name changing function

I created a reusable function for changing block name in djagoDjago templates. I want to know if the code is good or is there any probable issues in it. I developed the function as a hack for the following issue.

Template Code

{% extends base_name %}
{% block main-contents %}
 <h2>{{ message_heading }}</h2>
 <div class="alert alert-{{ box_color|default:"info" }}">
 {{ message }}
 {% if btn_1_text and btn_1_url %}
 <a href="{{ btn_1_url }}" class="btn btn-{{ btn_1_color }}">{{ btn_1_text }}</a>
 {% endif %}
 {% if btn_2_text and btn_2_url %}
 <a href="{{ btn_2_url }}" class="btn btn-{{ btn_2_color }}">{{ btn_2_text }}</a>
 {% endif %}
 </div>
{% endblock %}

Function to change block name

def change_block_names(template, change_dict):
 """
 This function will rename the blocks in the template from the
 dictionary. The keys in th change dict will be replaced with
 the corresponding values. This will rename the blocks in the 
 extended templates only.
 """
 extend_nodes = template.nodelist.get_nodes_by_type(ExtendsNode)
 if len(extend_nodes) == 0:
 return
 extend_node = extend_nodes[0]
 blocks = extend_node.blocks
 for name, new_name in change_dict.items():
 if blocks.has_key(name):
 block_node = blocks[name]
 block_node.name = new_name
 blocks[new_name] = block_node
 del blocks[name]
tmpl_name = 'django-helpers/twitter-bootstrap/message.html'
tmpl1 = loader.get_template(tmpl_name)
change_block_names(tmpl1, {'main-contents': 'new-main-contents})
return HttpResponse(tmpl.render(Context()))

This function seems to work for my issue. But I dontdon't know if this breaks any of the django'sDjango's functionality. Can any oneanyone verify this and give me some advices.advice?

django template block name changing function

I created a reusable function for changing block name in djago templates. I want to know if the code is good or is there any probable issues in it. I developed the function as a hack for the following issue.

Template Code

{% extends base_name %}
{% block main-contents %}
 <h2>{{ message_heading }}</h2>
 <div class="alert alert-{{ box_color|default:"info" }}">
 {{ message }}
 {% if btn_1_text and btn_1_url %}
 <a href="{{ btn_1_url }}" class="btn btn-{{ btn_1_color }}">{{ btn_1_text }}</a>
 {% endif %}
 {% if btn_2_text and btn_2_url %}
 <a href="{{ btn_2_url }}" class="btn btn-{{ btn_2_color }}">{{ btn_2_text }}</a>
 {% endif %}
 </div>
{% endblock %}

Function to change block name

def change_block_names(template, change_dict):
 """
 This function will rename the blocks in the template from the
 dictionary. The keys in th change dict will be replaced with
 the corresponding values. This will rename the blocks in the 
 extended templates only.
 """
 extend_nodes = template.nodelist.get_nodes_by_type(ExtendsNode)
 if len(extend_nodes) == 0:
 return
 extend_node = extend_nodes[0]
 blocks = extend_node.blocks
 for name, new_name in change_dict.items():
 if blocks.has_key(name):
 block_node = blocks[name]
 block_node.name = new_name
 blocks[new_name] = block_node
 del blocks[name]
tmpl_name = 'django-helpers/twitter-bootstrap/message.html'
tmpl1 = loader.get_template(tmpl_name)
change_block_names(tmpl1, {'main-contents': 'new-main-contents})
return HttpResponse(tmpl.render(Context()))

This function seems to work for my issue. But I dont know if this breaks any of the django's functionality. Can any one verify this and give me some advices.

Django template block name changing function

I created a reusable function for changing block name in Djago templates. I want to know if the code is good or is there any probable issues in it. I developed the function as a hack for the following issue.

Template Code

{% extends base_name %}
{% block main-contents %}
 <h2>{{ message_heading }}</h2>
 <div class="alert alert-{{ box_color|default:"info" }}">
 {{ message }}
 {% if btn_1_text and btn_1_url %}
 <a href="{{ btn_1_url }}" class="btn btn-{{ btn_1_color }}">{{ btn_1_text }}</a>
 {% endif %}
 {% if btn_2_text and btn_2_url %}
 <a href="{{ btn_2_url }}" class="btn btn-{{ btn_2_color }}">{{ btn_2_text }}</a>
 {% endif %}
 </div>
{% endblock %}

Function to change block name

def change_block_names(template, change_dict):
 """
 This function will rename the blocks in the template from the
 dictionary. The keys in th change dict will be replaced with
 the corresponding values. This will rename the blocks in the 
 extended templates only.
 """
 extend_nodes = template.nodelist.get_nodes_by_type(ExtendsNode)
 if len(extend_nodes) == 0:
 return
 extend_node = extend_nodes[0]
 blocks = extend_node.blocks
 for name, new_name in change_dict.items():
 if blocks.has_key(name):
 block_node = blocks[name]
 block_node.name = new_name
 blocks[new_name] = block_node
 del blocks[name]
tmpl_name = 'django-helpers/twitter-bootstrap/message.html'
tmpl1 = loader.get_template(tmpl_name)
change_block_names(tmpl1, {'main-contents': 'new-main-contents})
return HttpResponse(tmpl.render(Context()))

This function seems to work for my issue. But I don't know if this breaks any of the Django's functionality. Can anyone verify this and give me some advice?

Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /