简体   繁体   English

django-crispy-forms 的替代品

[英]alternative to django-crispy-forms

I am developing an application in django 2.1 and using bootstrap 4 which requires a form for the registration of users.我正在 django 2.1 中开发一个应用程序并使用 bootstrap 4,它需要一个用于用户注册的表单。 In most of the examples I have seen, examples of single-column forms are shown, which is enough for a form with few fields, but for a more complex one it would be ugly since you would have to scroll through the number of fields.在我看到的大多数示例中,都显示了单列表单的示例,这对于字段很少的表单来说已经足够了,但是对于更复杂的表单来说,它会很难看,因为您必须滚动浏览字段的数量。 For which I wanted to design a form of this typeServer side我想为其设计一个这种类型的服务器端形式

用网格系统创建的表格

Investigating a bit I found this tutorial Advanced Form Rendering with Django Crispy Forms in which use is made of Django Crispy Forms and you get the form you wanted.稍微调查一下,我发现本教程使用 Django Crispy Forms进行高级表单渲染,其中使用了Django Crispy Forms ,您将获得所需的表单。 However I did not want to use it, so try to do it manually and get to this.但是我不想使用它,所以尝试手动完成并完成此操作。 I think that it can be improved a little more since it is not so clean, however it is an alternative to reach the same result as using Django Crispy Forms.I think this can be done using ajax but so far I have never used it.我认为它可以改进一点,因为它不是那么干净,但是它是达到与使用 Django Crispy Forms 相同的结果的替代方法。我认为这可以使用 ajax 来完成,但到目前为止我从未使用过它。 He will also ask himself why complicate doing this if someone already did it, well it is a way to understand how everything works without any magic.他还会问自己,如果有人已经这样做了,为什么要把这件事复杂化,这是一种无需任何魔法即可了解一切如何运作的方法。

views.py视图.py

def sign_up(request):
if 'code_user' in request.session:
    return redirect('home')
elif request.method == 'POST':
    form = SignupForm(request.POST)
    if form.is_valid():
        user = form.save()
        request.session['code_user'] = user.code
        request.session['username'] = user.username
        return redirect('home')
    else:
        for field in form:
            if field.errors:
                form.fields[field.name].widget.attrs['class'] = 'form-control is-invalid'
        return render(request, 'blog/signup.html', {'form': form})

else:
    form = SignupForm()
    return render(request, 'blog/signup.html', {'form': form})

To indicate which fields are not valid you must add this class 'is-invalid' otherwise it will not work.要指示哪些字段无效,您必须添加此类“is-invalid”,否则它将不起作用。

forms.py表格.py

class SignupForm(forms.ModelForm):

class Meta:
    model = User
    fields = ['username', 'email', 'password', 'first_name', 'last_name', 'age', 'gender', 'country']
    widgets = {
        'username': forms.TextInput(attrs={'class': 'form-control', }),
        'country': forms.Select(attrs={'class': 'form-control', }),
        'first_name': forms.TextInput(attrs={'class': 'form-control', }),
        'last_name': forms.TextInput(attrs={'class': 'form-control', }),
        'age': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
        'email': forms.EmailInput(attrs={'class': 'form-control', }),
        'password': forms.PasswordInput(attrs={'class': 'form-control', }),
        'gender': forms.Select(attrs={'class': 'form-control', }),
    }
    labels = {
        'username': _('Username'),
        'country': _('Country'),
        'first_name': _('First Name'),
        'last_name': _('Last Name'),
        'age': _('Birthdate'),
        'email': _('Email'),
        'password': _('Password'),
        'gender': _('Gender'),
    }
    error_messages = {
        'username': {
            'unique': _('The username is not available')
        },
        'first_name': {
            'required': _('The field can not be empty')
        },
        'last_name': {
            'required': _('The field can not be empty')
        },
        'password': {
            'required': _('The field can not be empty')
        }

    }

signup.html注册.html

    {% extends 'blog/base.html' %}

{% block content %}

 <div class="row justify-content-center" style="padding-top: 1rem">
     <div class="col-md-10">
         <div class="card">
             <div class="card-header text-center">Sign up</div>
             <div class="card-body">
                 <form method="POST">
                     {% csrf_token %}
                     <div class="form-row">
                         <div class="form-group col-md-4">
                             <label >{{ form.username.label }}</label>
                             {{ form.username }}
                             {% for error in form.username.errors %}
                                 <div class="invalid-feedback">{{ error }}</div>
                             {% endfor %}
                         </div>

                         <div class="form-group col-md-4">
                             <label>{{ form.first_name.label }}</label>
                             {{ form.first_name }}
                             {% for error in form.first_name.errors %}
                                 <div class="invalid-feedback">{{ error }}</div>
                             {% endfor %}
                         </div>

                         <div class="form-group col-md-4">
                             <label>{{ form.last_name.label }}</label>
                             {{ form.last_name }}
                             {% for error in form.last_name.errors %}
                                 <div class="invalid-feedback">{{ error }}</div>
                             {% endfor %}
                         </div>
                     </div>

                     <div class="form-row">
                         <div class="form-group col-md-4">
                             <label >{{ form.age.label }}</label>
                             {{ form.age }}
                             {% for error in form.age.errors %}
                                 <div class="invalid-feedback">{{ error }}</div>
                             {% endfor %}
                         </div>

                         <div class="form-group col-md-4">
                             <label >{{ form.gender.label }}</label>
                             {{ form.gender }}
                             {% for error in form.gender.errors %}
                                 <div class="invalid-feedback">{{ error }}</div>
                             {% endfor %}
                         </div>

                         <div class="form-group col-md-4">
                             <label >{{ form.country.label }}</label>
                             {{ form.country}}
                             {% for error in form.country.errors %}
                                 <div class="invalid-feedback">{{ error }}</div>
                             {% endfor %}
                         </div>
                     </div>

                     <div class="form-row">
                         <div class="form-group col-md-6">
                             <label >{{ form.email.label }}</label>
                             {{ form.email }}
                              {% for error in form.email.errors %}
                                 <div class="invalid-feedback">{{ error }}</div>
                             {% endfor %}
                         </div>

                         <div class="form-group col-md-6">
                             <label >{{ form.password.label }}</label>
                             {{ form.password }}
                              {% for error in form.password.errors %}
                                 <div class="invalid-feedback">{{ error }}</div>
                             {% endfor %}
                         </div>
                     </div>

                     <button type="submit" class="btn btn-primary">Sign in</button>
                 </form>
             </div>
         </div>
     </div>
 </div>

{% endblock %}

In the template I guess that is where everything is less clean see so many cycles overwhelms me and I think it is not optimal.在模板中,我想这就是一切都不那么干净的地方,看到如此多的循环让我不知所措,我认为这不是最佳选择。

Perhaps the post below helps, it shows how to use django-floppyforms to better organize the frontend files也许下面的帖子有帮助,它展示了如何使用 django-floppyforms 更好地组织前端文件

Better usage of JS & CSS inside Django 在 Django 中更好地使用 JS 和 CSS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM