微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有自定义表单的Django UserCreationForm不起作用

如何解决具有自定义表单的Django UserCreationForm不起作用

I have a custom template where you have to put (first_name,last_name,email,password1,password2) for registration --> no username. I tried to use CserCreationForm in the below form but it is not workin,at the end after Feeding up the form with data,nothing happens by clicking on submit. Could you please advice where is the problem? Thanks in advance guys! 

forms.py-我创建了从USerCreationForm继承的SignUpForm,因此我需要占位符__init_方法

class SignUpForm(UserCreationForm):

    class Meta:
        model = User
        fields = ('first_name','last_name','username','email','password1','password2',)

    def __init__(self,*args,**kwargs):
        super(SignUpForm,self).__init__(*args,**kwargs)
        self.fields['first_name'].widget.attrs['placeholder'] = 'Imie'
        self.fields['last_name'].widget.attrs['placeholder'] = 'Nazwisko'
        self.fields['email'].widget.attrs['placeholder'] = 'Email'
        self.fields['password1'].widget.attrs['placeholder'] = 'Hasło'
        self.fields['password2'].widget.attrs['placeholder'] = 'Powtórz Hasło'


    def save(self,commit=True):
        user = super(SignUpForm,self).save(commit=False)
        user.first_name = self.cleaned_data["first_name"]
        user.last_name = self.cleaned_data["last_name"]
        user.username = self.cleaned_data["first_name"]
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

views.py-这里我正在使用django提供的东西:)

class RegisterView(CreateView):
   form_class = SignUpForm
   template_name = "register.html"
   success_url = "login"

template:我有一个模板,其中要填写几个字段,如顶部所述,因此我没有使用简单的{{form.as_table},用户名不是必填名称,应该是和名字一样

{% extends 'base.html' %}
{% block content %}
{% load static %}
  <body>
  {% block header %} {% endblock %}
  {% block slogan %} {% endblock %}
    <section id = "register" class="login-page">
      <h2>Załóż konto</h2>
      <form method="POST" action="">
        {% csrf_token %}
        <div class="form-group">
            {{form.first_name}}
        </div>
         <div class="form-group">
            {{form.last_name}}
        </div>
        <div class="form-group">
            {{form.email}}
        </div>
        <div class="form-group">
            {{form.password1}}
        </div>
        <div class="form-group">
            {{form.password2}}
        </div>
        <div class="form-group form-group--buttons">
          <a href="login.html" class="btn btn--without-border">Zaloguj się</a>
          <button class="btn" type="submit">Załóż konto</button>
        </div>
      </form>
    </section>
  </body>
{% endblock %}

urls.py

  path('register/',RegisterView.as_view(),name = "register")

解决方法

“ password2”后还有一个逗号。 类Meta: 型号=用户 字段=('first_name','last_name','username','email','password1','password2',)

无论如何,您都应该在窗体的操作中放置要调用的函数的名称。 应该是这样的。

 <section id = "register" class="login-page">
  <h2>Załóż konto</h2>
  <form method="POST" action="{% url 'login'%}">
    {% csrf_token %}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。