为什么我的UserCreationForm无法按时间顺序渲染?

时间:2019-12-03 09:08:25

标签: html django forms

我希望表单显示

  1. 用户名
  2. 电子邮件
  3. 密码
  4. 密码(2)

此刻正在显示

  1. 用户名
  2. 密码
  3. 密码(2)
  4. 电子邮件

我正在尝试遵循本教程https://www.youtube.com/watch?v=q4jPR-M0TAQ

我在Github上查看了创作者的笔记,但这没有帮助。

我仔细检查了我的代码,看不到任何愚蠢的错字。

谁能提供任何见解?

enter image description here

from django import forms 
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

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

class Meta:
    model = User
    fields = ['username', 'email', 'password1', 'password2']


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

def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            messages.success(request, f'Account created for {username}!')
            return redirect ('blog-home')
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form':form})


{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
        <form method="POST">
            {% csrf_token %}
            <fieldset class="form-group">
                <legend class="border-bottom mb-4">Join Today</legend>
                {{ form|crispy }}
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Sign Up</button>
            </div>
        </form>
        <div class="border-top pt-3">
            <small class="text-muted">
                Already Have An Account? <a class="ml-2" href="#">Sign In</a>
            </small>
        </div>
    </div>
{% endblock content %}

1 个答案:

答案 0 :(得分:0)

您可以通过此方法自定义表单的外观和顺序,在account / template目录中创建一个新模板,将其命名为register.html,并使其外观如下:

{% extends "base.html" %}

{% block title %}Create an account{% endblock %}

{% block content %}
  <h1>Create an account</h1>
  <p>Please, sign up using the following form:</p>
  <form action="." method="post">{% csrf_token %}
    {{ form.username }}
    {{ form.other_fields_as_you_like }}
    <p><input type="submit" value="Create my account"></p>
  </form>
{% endblock %}