消息标签有问题

问题描述 投票:0回答:1

*尝试根据用户输入创建动态响应,但甚至没有出现警报按钮。但是当我摆脱 {% for message in messages%} 时,会出现一个空的警报按钮。 **

这是代码部分 视图.py 文件

from django.shortcuts import render, redirect

from django.contrib.auth.models import User 
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout


def signup(request):

    if request.method == "POST":
        username = request.POST.get('username')
        email = request.POST.get('email')
        pass1 = request.POST.get('pass1')
        pass2 = request.POST.get('pass2')
        if pass1 != pass2:
            messages.info(request, "Password is not matching")
            return redirect('/authapp/signup/')

        try:
            if User.odjects.get(username=get_email):
                messages.warning(request, 'User with email already exist')
                return redirect('/authapp/signup/')
        
        except Exception as identifier:
            pass


        myuser = User.objects.create_user(username, email, pass1)
        myuser.save()

        messages.success(request, "Your Account has been successfully created, Login")
        return redirect('/authapp/signin/')

    return render(request, 'authapp/signup.html')

signup.html
 {% for message in messages  %}
  <div class="alert alert-{{message.tags}} alert-dismissible fade show" role="alert">
    <strong> {{message}} </strong> 
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
  </div>          
 {% endfor %}
settings.py

   MESSAGE_TAGS = {
        messages.ERROR:'alert-info'
   }



python django bootstrap-4 django-messages
1个回答
0
投票
MESSAGE_TAGS = {
        messages.ERROR:'alert-info'
}

您已更改 消息标签 的默认设置,而不是将

error
作为字符串返回,而是返回
alert-info
,在 前端 上将转换为
alert alert-alert-info
类。引导程序中没有具有该名称的此类。去掉消息标签设置就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.