django - Django 中的自定义用户注册表单

标签 django django-models django-templates

我已经为 django 创建了自定义用户注册表单,但它不起作用:-(

我创建了 LOGINSYS 应用程序来操纵用户(登录、个人资料页面、注册)

我的 forms.py 看起来像这样:


    #-*- coding:utf-8 -*-

    from django import forms
    from django.contrib.auth.models import User
    from django.contrib.auth.forms import UserCreationForm
    from datetime import date, timedelta

    class MyRegistrationForm(UserCreationForm):

        def get_image_path(self, filename):
            path = ''.join([date.today().strftime('../static/user_image/%Y/%m/%d/'), translit.slugify(filename), ".jpg"])
            return path

        first_name = forms.CharField (required = True)
        last_name = forms.CharField (required = True)
        telephone = forms.CharField (required = True)
        email = forms.EmailField (required = False)
        #user_image = forms.ImageField(path = get_image_path, required = False, allow_empty_file = True )


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

        def save (self, commit=True):
            user = super(UserCreationForm, self).save(commit=False)
            user.first_name = self.cleaned_data['first_name']
            user.last_name = self.cleaned_data['last_name']
            user.telephone = self.cleaned_data['telephone']
            user.email = self.cleaned_data['email']
            user.set_password(self.cleaned_data["password1"])
            #user.user_image = self.cleaned_data['user_image']
            if commit:
                user.save()

我的views.py看起来像:


    #-*- coding:utf-8 -*-

    from django.shortcuts import render, render_to_response, redirect
    from django.http.response import HttpResponse
    from django.http import HttpResponseRedirect
    from django.template.loader import get_template
    from django.template import Context
    from django.template import RequestContext
    from advert.models import Advert, AdvertCategory, AdvertSection
    from django.core.exceptions import ObjectDoesNotExist
    from django.core.context_processors import csrf
    from django.core.urlresolvers import reverse
    from django.core.paginator import Paginator
    from datetime import date, timedelta
    from PIL import Image
    from django.conf import settings
    from django.contrib.syndication.views import Feed
    from django.contrib import auth
    from django.contrib.auth.forms import UserCreationForm
    from loginsys.forms import MyRegistrationForm

    def register(request):
        args = {}
        args.update(csrf(request))
        args['form'] = MyRegistrationForm()
        if request.POST:
            newuser_form = MyRegistrationForm(request.POST)
            if newuser_form.is_valid():
                username = newuser_form.cleaned_data['username']
                password = newuser_form.cleaned_data['password1']
                #password2 = newuser_form.cleaned_data['password2']
                first_name = newuser_form.cleaned_data['first_name']
                last_name = newuser_form.cleaned_data['last_name']
                telephone = newuser_form.cleaned_data['telephone']
                email = newuser_form.cleaned_data['email']
                newuser_form.save()
                newuser = auth.authenticate(username=newuser_form.cleaned_data['username'], password=newuser_form.cleaned_data['password2'])
                auth.login(request, newuser)
                return redirect('/')
            else:
                args['reg_form'] = newuser_form
        return redirect('/')

不幸的是,当我在前端注册时,它没有执行任何操作:-(

请帮助我

最佳答案

您的代码未渲染表单,请参阅 this examplerender 的用法.

我建议使用django-auth-tools用于构建您自己的自定义用户模型。它提供了可以轻松扩展的基本模型、 View 和表单。

关于django - Django 中的自定义用户注册表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29697409/

相关文章:

python - 关于 parent 的结构模型布局?

python - 如何在 Django 的 latex 模板中正确设置变量

django - 在一个 html 表单中处理多个模型表单

html - bootstrap4.0 包括搜索表单和登录导航栏

mysql - 如何使用 Django ORM 在字段上运行

python - __init__() 得到了一个意外的关键字参数 'user'

python - 如何确定 Django 中的模板 block 是否为空/空白?

django 基于条件应用样式类

python - 各种 Python CMS 及其状态是什么?

Django filter_水平过滤