2013-05-12 40 views
1

我想知道如果你们能与我分享你在Django中显示警报和确认消息的策略。我真的不确定什么是实现这一目标的最佳方式。你是否将一个单独的.py文件与消息和一个包装函数保存在一起,或者你只是沿代码硬编码一切?你认为这应该是在视图或模板中的优先事项?实现确认和提醒消息的最佳方式

无论如何,谢谢。

回答

0

我使用了重新使用视图显示确认

重新使用视图

def bootstrap_confirm(
     request, 
     heading, 
     message, 
     yes_color='success', 
     yes_text='Yes', 
     cancel_color='danger', 
     cancel_text='No', 
     cancel_url='', 
     box_color='info', 
     base_name='base.html'): 
    if request.method == 'POST': 
     confirm = request.session['confirm_value'] 
     assert request.POST.get('confirm') == confirm, "Error" 
     return request.POST.get('submit_button') == yes_text 
    else: 
     confirm = get_random_string(16) 
     request.session['confirm_value'] = confirm 
     return render_to_response('django-helpers/twitter-bootstrap/confirm.html', request, { 
      'confirm_value': confirm, 

      'confirm_heading': heading, 
      'message': message, 
      'base_name': base_name, 
      'box_color': box_color, 

      'yes_color': yes_color, 
      'yes_text': yes_text, 

      'cancel_color': cancel_color, 
      'cancel_text': cancel_text, 
      'cancel_url': cancel_url, 
     }) 

重新使用的模板

{% extends base_name %} 

{% block main-contents %} 
    <h2>{{ confirm_heading }}</h2> 
    <form action="" method="post"> 
     {% csrf_token %} 
     <input type="hidden" name="confirm" value="{{ confirm_value }}"> 

     <div class="alert alert-{{ box_color|default:"info" }}"> 
      <div>{{ message }}</div> 
     <br> 
      <div> 
       <input type="submit" class="btn btn-{{ yes_color|default:"success" }}" name="submit_button" type="submit" value="{{ yes_text|default:"Yes" }}"> 
       {% if cancel_url %} 
        <a href="{{ cancel_url }}" class="btn btn-{{ cancel_color|default:"danger" }}">{{ cancel_text|default:"No" }}</a> 
       {% endif %} 
      </div> 
     </div> 
    </form> 
{% endblock %} 

查看

def confirm(request): 
    msg = '...' 
    heading = '...' 
    op = bootstrap_confirm(request, heading, msg) 
    if isinstance(op, HttpResponse): 
     return op 

    if op == True: 
     # Implement custom logic 
    elif op == False: 
     # Implement custom logic 

您也可以使用类似的可重用视图来显示消息。此代码来自我的图书馆django-helpers。我也有兴趣了解更多的策略。纠正我,如果我错了?

2

Django带有一个messages应用程序,可以帮助解决这个问题。

在你看来,你想补充一点,你需要显示的消息:

from django.contrib import messages 
from django.shortcuts import render 

def someview(request): 
    # your normal code 
    messages.add_message(request, messages.INFO, 'Yeehaw!') 
    return render(request, 'sometemplate.html') 

注意我没有在我看来返回的消息,这是因为messages middleware需要照顾这对我来说。我只需要返回RequestContext,即render shortcut

在模板:

{% if messages %} 
    {% for message in messages %} 
    <div{% if message.tags %} class="alert alert-{{ message.tags }}"{% endif %}> 
     <a class="close" data-dismiss="alert" href="#">&times;</a> 
     {{ message }} 
    </div> 
    {% endfor %} 
{% endif %} 

通常你把上面的代码中的每一个模板,从继承您的基本模板之一;就是这样。

+0

并且为了显示带引导程序样式的错误消息,您应该将此行添加到settings.py中:'MESSAGE_TAGS = {message.ERROR:'danger' }'MESSAGE_TAGS = {} – 2017-03-30 09:14:47