Registration Form not submitting in Django

穿精又带淫゛_ 提交于 2019-12-13 00:07:26

问题


I am doing registration in django using a form with overriding save method. I am not having any error but not submitting form. Following is my code: Models.py:

from django.db import models
from django.contrib.auth.models import User 
class UserProfile(models.Model):
    user=models.OneToOneField(User) 
    meta_keywords=models.CharField("Meta Keywords",max_length=255,
            help_text="Comma delimited set of keywords of meta tag")
    meta_description=models.CharField("Meta Description",max_length=255,
            help_text='Content for description meta tag')

    def __unicode__(self):
        return "User Profile for: "+self.username

    class Meta:
        ordering=['-id']

forms.py

from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
from accounts.models import UserProfile
from pprint import pprint


class RegisterationForm(UserCreationForm):
    email = forms.EmailField(label = "Email")
    fullname = forms.CharField(label = "Full name")

    class Meta:
        model = User
        fields = ("username", "fullname", "email", )

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

views.py

from django.contrib.auth.forms import UserCreationForm
from django.template import RequestContext
from django.shortcuts import render_to_response,get_object_or_404
from django.core import urlresolvers
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from accounts.forms import RegisterationForm
#from accounts.forms import UserProfile

def register(request,template_name="account/register.html"):
    if request.method=='POST':
        postdata=request.POST.copy()
        form=RegisterationForm(postdata)
        if form.is_valid():
            form.save
            un=postdata.get('username','')
            pw=postdata.get('password','')
            from django.contrib.auth import login,authenticate
            new_user=authenticate(username=un,password=pw)
            if new_user and new_user.is_active:
                login(request,new_user)
                url=urlresolvers.reverse('dashboard')
                return HttpResponseRedirect(url)            
    else:
        form=RegisterationForm()
    page_title="User Registration"
    return render_to_response(template_name,locals(),context_instance=RequestContext(request))


@login_required
def dashboard(request):
    pass

@login_required
def settings(request):
    pass

templatefile(register.html):

    <div class="register_form">
    <form method="post" action="." class="cart"> 
        {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Register" name="submit" alt="Register" />
    </form>
</div>

After submitting form, it doesn't show any thing except the form again. In db no new records are inserted, I also checked it from admin panel.

I am wondering that what can be the problem because every thing seems fine. Is there some good way to debug problem? some thing like pprint some thing but where to and how to debug in this situation?


回答1:


Let me take a wild guess

form.save

to

form.save()

I am nervous! Prove me right!!! (or partially right)

Whether I am right or wrong, I usually use Python's traceback module. It's really simple to use.

http://docs.python.org/library/traceback.html



来源:https://stackoverflow.com/questions/10290461/registration-form-not-submitting-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!