django用户表单未显示在HTML页面中

时间:2019-02-04 00:16:07

标签: django django-forms django-templates django-views

我是django的新手。在这里我想注册用户,但遇到问题,用户表单未显示在注册html页面中

这是我的代码:

views.py:

enter code here
 from django.views.generic.edit import CreateView, UpdateView, DeleteView
 from django.core.urlresolvers import reverse_lazy
 from django.shortcuts import render, redirect
 from django.contrib.auth import authenticate, login
 from django.views import generic
 from django.views.generic import View
 from .forms import UserForm
 from .models import Album


class UserFormView(View):
   form_class = UserForm
   template_name = 'music/registration_form.html'

# display blank form

def get(self, request):
    form = self.form_class(None)
    return render(request, self.template_name, {'form': form})

def post(self, request):
    form = self.form_class(request.POST)

    if form.is_valid():

        user = form.save(commit=False)

        # cleaned (Normalized) data
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user.set_password(password)
        user.save()

# return user objects if info r correct

        user = authenticate(username=username, password=password)

        if user is not None:

            if user.is_active:
                login(request, user)
                return redirect('music:index')

    return render(request, self.template_name, {'form': form})

form.py:

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


class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

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

只要页面弹出,我认为url.py没什么问题,我不知道自己在哪里弄错了

0 个答案:

没有答案