保存表单提交中的数据

时间:2015-07-28 19:24:52

标签: python django django-models django-forms modelform

我想创建一个帐户注册页面,该页面直接链接到帐户信息页面并显示帐户信息。我很难获得表单将帐户信息保存到我的模型中,但我不知道为什么。

models.py:

class Owner(models.Model):
    fullname = models.CharField(max_length=255)
    username = models.CharField(max_length=255)
    password = models.CharField(max_length=255)
    email = models.EmailField()

    def __unicode__(self):
        return self.fullname

""" This is a Form class and not a regular Model class """
class OwnerForm(forms.Form):
    class Meta:
        model = Owner
        fields = ['fullname', 'username', 'password', 'email']

views.py:

def register(request):
    form = OwnerForm()
    if request.POST:
        form = OwnerForm(request.POST)
        if form.is_valid():
            fullname = request.POST.get('fullname', '')
            username = request.POST.get('username', '')
            password = request.POST.get('password', '')
            email = request.POST.get('email', '')
            owner_obj = Owner(fullame=fullname, username=username, password=password, email=email)
            owner_obj.save()
            return HttpResponseRedirect('courses/accountinfo.html')
    else:
        form = OwnerForm()
    return render_to_response('courses/register.html', {'form': form}, context_instance=RequestContext(request))

register.html:

{% extends "layout.html" %}
{% load static from staticfiles %}

{% block title %}{{ page.title }}{% endblock %}
{% block content %}
<article>
        <div id="Register">
            <form action="{% url 'courses:accountinfo' %}" method="post"> {% csrf_token %}
                <p>
                    <label for="fullname">Full name:</label>
                    <input id="fullname" name="fullname" type="text">
                </p>
                <p>
                    <label for="email">Email</label>
                    <input id="email" name="email" type="text">
                </p>
                <p>
                    <label for="username">Username</label>
                    <input id="username" name="username" type="text">
                </p>
                <p>
                    <label for="password">Password</label>
                    <input id="password" name="password" type="password">
                        <span>Enter a password longer than 8 characters</span>
                </p>
                <p>
                    <label for="confirm_password">Confirm Password</label>
                    <input id="confirm_password" name="confirm_password" type="password">
                        <span>Please confirm your password</span>
                </p>
                <p>
                    <input type="submit" value="REGISTER" id="submit">
                </p>
            </form>
            <script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
            <script src="{% static 'js/app.js' %}" charset="utf-8"></script>
      </div>
</article>
{% endblock %}

2 个答案:

答案 0 :(得分:1)

您可以在检查form.is_valid后调用此方法,然后发送重定向并让其他视图处理其余视图。问题也可能出在accountinfo视图中,但您没有发布该代码。

owner = form.save()
owner.set_password(owner.password)
owner.save()

答案 1 :(得分:0)

使用表单上的save方法保存数据,而不是在表单有效后创建模型对象。但是我看到该模板包含其他字段。你有有效的方法吗? 你得到的错误是什么?你发布到正确的网址了吗?