gpt4 book ai didi

Django's Success messages displayed like error messages(Django的成功消息显示为错误消息)

转载 作者:bug小助手 更新时间:2023-10-25 15:06:42 25 4
gpt4 key购买 nike



I'm working on a Django project and I'm using Django's built-in messaging framework to display success and error messages to the user. However, I'm encountering an issue where success messages are being displayed in a way that makes them look like error messages.error messages displyed like success messages

我正在处理一个Django项目,并使用Django的内置消息传递框架向用户显示成功和错误消息。但是,我遇到了一个问题,即成功消息的显示方式使其看起来像错误消息。错误消息显示为成功消息


here is my Html code:

以下是我的HTML代码:


{% extends 'base.html' %}
{% load widget_tweaks %}
{% block content %}
<div class="mx-auto max-w-screen-xl px-4 py-16 sm:px-6 lg:px-8">
<div class="mx-auto max-w-lg">
{% if messages %}
{% for message in messages %}
{% if message.tags == 'success' %}
<div class="rounded border-s-4 mb-1 border-green-500 bg-emerald-50 p-4">
<strong class="block font-medium text-green-800"> Successfully </strong>

<p class="mt-2 text-sm text-green-700">
{{ message }}
</p>
</div>
{% else%}
<div class="rounded border-s-4 mb-1 border-red-500 bg-red-50 p-4">
<strong class="block font-medium text-red-800"> Something went wrong </strong>
<p class="mt-2 text-sm text-red-700">
{{ message }}
</p>
</div>
{% endif %}
{% endfor %}
{% endif %}

<h1 class="text-center text-2xl font-bold text-black sm:text-3xl">Book Appointment</h1>

<p class="mx-auto mt-4 max-w-md text-center text-gray-500">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati sunt
dolores deleniti inventore quaerat mollitia?
</p>

<form method="post" action="" class="mb-0 mt-6 space-y-4 rounded-lg p-4 shadow-lg sm:p-6 lg:p-8">
<p class="text-center text-lg font-medium">Appointment form</p>
{% csrf_token %}
<div>
<label for="name" class="sr-only">Name</label>

<div class="relative">
<input type="text" type="text" name="name" class="w-full rounded-lg border-gray-200 p-4 pe-12 text-sm shadow-sm outline-blue-600" placeholder="Enter name">
</div>
</div>

<div>
<label for="email" class="sr-only">Email</label>

<div class="relative">
<input type="text" type="email" name="email" class="w-full rounded-lg border-gray-200 p-4 pe-12 text-sm shadow-sm outline-blue-600" placeholder="Enter email">
</div>
</div>

<div>
<label for="phonenumber" class="sr-only">Phone number</label>

<div class="relative">
<input type="tel" name="phone_number" class="w-full rounded-lg border-gray-200 p-4 pe-12 text-sm shadow-sm outline-blue-600" placeholder="Enter phone number" maxlength="10">
</div>
</div>
<button type="submit" class="block w-full rounded-lg bg-black px-5 py-3 text-sm font-medium text-white">Book appointment</button>
</form>
</div>
</div>
{% endblock %}

here is my views.py code

以下是我的views.py代码


def create_appointment(request):
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
phone_number = request.POST.get('phone_number')
date_str = request.session.get('date')
time = request.session.get('time')

date = datetime.fromisoformat(date_str)

if Appointment.objects.filter(email=email).exists():
messages.error(request, 'This email is already booked for an appointment.', extra_tags='error')
return redirect('appointment')

if Appointment.objects.filter(phone_number=phone_number).exists():
messages.error(request, 'This Phone number is already booked for an appointment.', extra_tags='error')
return redirect('appointment')

booking = Appointment.objects.create(name=name, email=email, phone_number=phone_number, date=date, time=time, user=request.user)
booking.save()

subject = 'Appointment Confirmation'
message = f'Thank you for the appointment \n\n Dear {booking.name},\n\nYour appointment for {booking.date.date()} at {booking.time} has been successfully booked.\n\nYour token number is: {booking.token} \n\nPhone Number: {booking.phone_number}\n\nIf you want to cancel the appointment, just call: 91XXXXXXXX \n\nThank you!'
from_email = settings.EMAIL_HOST_USER
recipient_list = [booking.email]

send_mail(subject, message, from_email, recipient_list, fail_silently=False)
messages.success(request, 'Appointment created successfully.', extra_tags='success')
del request.session['date']
del request.session['time']
return redirect('appointment')

return render(request, 'appointment.html' , {'time':'appointment' , 'title':'Appointment'})

click for image
error

单击以查看图像错误


更多回答

please upload your Django code as well, it would be helpful for debugging.

请上传您的Django代码以及,这将有助于调试。

@nithinks tnx for responding now updated my code

@niThink tnx现在更新了我的代码

优秀答案推荐

The issue might be related to the way you are handling message tags in your template. In your code you are checking if the message tags are exactly equal to 'success' or not and if not you assume it is an error message. This approach may not work correctly because there might be other tags attached to the success messages. You can modify your code in a different way like: check if 'success' is in the message tags this will allow you to handle success messages better even if they have additional tags attached.
For example:

该问题可能与您在模板中处理消息标记的方式有关。在您的代码中,您正在检查消息标记是否与‘Success’完全相等,如果不是,则认为这是一条错误消息。此方法可能无法正常工作,因为可能有其他标记附加到成功消息。您可以通过不同的方式修改代码,例如:检查消息标记中是否包含‘Success’这将允许您更好地处理成功消息,即使它们附加了额外的标记。例如:


{% if messages %}
{% for message in messages %}
{% if 'success' in message.tags %}
<div class="rounded border-s-4 mb-1 border-green-500 bg-emerald-50 p-4">
<strong class="block font-medium text-green-800"> Successfully </strong>

<p class="mt-2 text-sm text-green-700">
{{ message }}
</p>
</div>
{% else %}
<div class="rounded border-s-4 mb-1 border-red-500 bg-red-50 p-4">
<strong class="block font-medium text-red-800"> Something went wrong </strong>
<p class="mt-2 text-sm text-red-700">
{{ message }}
</p>
</div>
{% endif %}
{% endfor %}
{% endif %}

更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com