表单未呈现给FrontEnd

时间:2018-05-23 06:31:32

标签: django python-3.x django-forms django-templates

当我点击URL时,只有html按钮被渲染,form.as_p现在正在渲染。请帮帮我。谢谢

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('signup/',views.SignUp, name = 'signup_view'),
]

我的代码: forms.py

from django import forms
from .models import Profile
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['user','bio','location','birth_date']

class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=200,required=True,help_text='Hello')
    last_name = forms.CharField(max_length=200,required=True)
    email = forms.EmailField(max_length=500,help_text='Enter a valid email address')

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

views.py

from django.shortcuts import render,redirect
from django.contrib.auth import login,authenticate
from .forms import SignUpForm

def SignUp(request):
    form = SignUpForm()
    if request.method == 'POST':
        print(request.method)
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            raw_username = form.cleaned_data.get['username']
            raw_password = form.cleaned_data.get['password1']
            user = authenticate(username = raw_username,password = raw_password)
            login(request,user)
            return redirect('home')

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

模板/注册/ signup.html

<form method="post">
    {% csrf_token %}
    {{ from.as_p }}
    <button value="submit" type="submit">SignUp</button>
</form>

0 个答案:

没有答案