使Django消息出现在页面的特定位置

Making Django messages appear in a specific place on the page

我有一张注册表,我希望我的错误显示在我的提交按钮下。我怎样才能做到这一点? +我想在我的消息中添加自定义 类。

我的forms.py

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Row, Column



class UserRegisterForm(UserCreationForm):
email = forms.EmailField()

class Meta:
    model = User
    fields = ['username', 'email', 'password1', 'password2']
#override
def __init__(self, *args, **kwargs):
    super(UserRegisterForm, self).__init__(*args, **kwargs)

    #Custom classok
    self.fields['username'].widget.attrs['class'] = 'input'
    self.fields['email'].widget.attrs['class'] = 'input'
    self.fields['password1'].widget.attrs['class'] = 'input'
    self.fields['password2'].widget.attrs['class'] = 'input'

    #Custom label
    self.fields['username'].label = ""
    self.fields['email'].label = ""
    self.fields['password1'].label = ""
    self.fields['password2'].label = ""

    #Custom placeholder
    self.fields['username'].widget.attrs['placeholder'] = 'Username'
    self.fields['email'].widget.attrs['placeholder'] = 'Email address'
    self.fields['password1'].widget.attrs['placeholder'] = 'Password'
    self.fields['password2'].widget.attrs['placeholder'] = 'Confirm Password'
    
    
    
    #Help text off
    for fieldname in ['username', 'password1', 'password2']:
        self.fields[fieldname].help_text = None

我的 views.py 文件:

from django.shortcuts import render
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm

# Create your views here.
def register(request):
if request.method == 'POST':
    form = UserRegisterForm(request.POST)
    if form.is_valid():
        form.save()
        messages.success(request, f'Your account has been created! You are now able to log in!')
        return redirect('landing-home')
else:
    form = UserRegisterForm()
    
return render(request, 'users/register.html',{'form':form})

我的 html 代码也在这里:

{%block content%}
<form class="form" method="POST">
    {% csrf_token %} 
    <fieldset class="form-group text-center text-uppercase">
        <h1 class="sign-up__header">Sign up</h1>
        {{form.as_p}}
    </fieldset>
    <div class="form-group">
        <button class="submit" type="submit">Sign up</button>
    </div>
   
    </div>
</form>

</div>




{%endblock content%}

基本上我想要实现的是我的错误消息出现在我的表单中,它们将在我的提交按钮下组织。

也许像这样的东西可以解决问题:

<form class="form" method="POST">
    {% csrf_token %} 
    <fieldset class="form-group text-center text-uppercase">
        <h1 class="sign-up__header">Sign up</h1>
        {% for field in form %}
            <p>
                {{ field.label_tag }}
                {{ field }}
                {{ field.help_text }}
            </p>
        {% endif %}
    </fieldset>
    <div class="form-group">
        <button class="submit" type="submit">Sign up</button>
    </div>
    {% for error in form.errors %}
        <div class="text-danger">{{ error }}</p>
    {% endfor %}
    </div>
</form>