Django讯息无法运作

时间:2018-08-11 09:22:53

标签: django python-3.x bootstrap-4 django-messages

我正在尝试同时使用django消息和引导程序。

我也检查了这个问题;

django messages not showing

但是,我已经应用了那里写的内容。

我的settings.py;

from django.contrib.messages import constants as messages

# some other codes #

MESSAGE_TAGS = {
messages.DEBUG: 'alert-info',
messages.INFO: 'alert-info',
messages.SUCCESS: 'alert-success',
messages.WARNING: 'alert-warning',
messages.ERROR: 'alert-danger',
}

# some other codes #

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
.
.
# other apps
]

# some other codes #

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_pdb.middleware.PdbMiddleware',
]

# some other codes #

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.template.context_processors.media',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},
]

我的产品/views.py;

# some other imports #

from django.contrib import messages

# some other codes # 

def oxford(request):
    search_result = {}
    if 'word' in request.GET:
        form = DictionaryForm(request.GET)
        if form.is_valid():
            search_result = form.search()
            messages.success(request, 'Your query is successful!')
    else:
        form = DictionaryForm()
        messages.warning(request, 'Please correct the error.')
    return render(request, 'products/oxford.html', {'form': form, 'search_result': search_result})

我的产品/urls.py;

# some imports here #

app_name = 'products'
urlpatterns = [
    path('', views.IndexView.as_view(), name='base'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('oxford/', views.oxford, name='oxford'),
    # some other urls here #
]

myproject / templates / dashboard.html(不在app文件夹中);

{% load static %}

<html lang="en">
    <head>
        # some code here #
    </head>
    <body>
        # some code here #
        {% block main %}
        {% endblock main %}
    </body>
</html>

products / templates / products / base_products.html;

{% extends 'dashboard.html' %}

{% block page_main_title %}
{% endblock page_main_title %}

{% block list_html_app_css %}
{% endblock list_html_app_css %}

{% block main %}
  {% block show_message %}
  {% endblock show_message %}
{% endblock main %}

{% block graph %}
{% endblock graph %}

products / templates / products / oxford.html;

{% extends 'products/base_products.html' %}
{% load static %}

{% block main %}
  <main>
    <div>
      # some code here #
    </div>

    <div class="border-bottom">
      {% block get_oxford %}

      <h2>Oxford Dictionary</h2>

      <form method="get">
      {{ form.as_p }}
        <button type="submit">search</button>
      </form>

      {% if search_result %}
        <hr>
        {% if search_result.success %}
          {% for result in search_result.results %}
            <h3>{{ result.word }}</h3>
              {% for lexicalentry in result.lexicalEntries %}
                 <h4>{{ lexicalentry.lexicalCategory }}</h4>
                 <ul>
                 {% for entry in lexicalentry.entries %}
                    {% for sense in entry.senses %}
                       {% for definition in sense.definitions %}
                          <li>{{ definition }}</li>
                       {% endfor %}
                    {% endfor %}
                 {% endfor %}
                 </ul>
              {% endfor %}
          {% endfor %}
       {% else %}
         <p><em>{{ search_result.message }}</em></p>
       {% endif %}
     {% endif %}

     {% endblock get_oxford %}
   </div>
   <div class="container">
    {% block show_message %}
      {% include 'products/messages.html' %}
    {% endblock show_message %}
   </div>
 </main>

{% endblock main %}

products / templates / products / messages.html;

{% if messages %}
  {% for message in messages %}
    <div class="alert alert-{{ message.tags }} alert-dismissible" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
      {{ message }}
    </div>
  {% endfor %}
{% endif %}

我在这里找不到我想要的东西。谢谢您的时间和答复。

1 个答案:

答案 0 :(得分:0)

如果表单对于try无效,则添加其他消息:

nlog