blackwindow
blackwindow

Reputation: 437

Django forms not working

Hi I have a simple django form, which enables the users to signup to the website. but am confused how can I submit my form fields. Am new to Django. Please help me on it. Thanks in advance.

Forms.py:

from django import forms
from django import forms
from django.contrib.auth.models import User   # fill in custom user info then save it
# from django.contrib.auth.forms import UserCreationForm


class UserForm(forms.Form):
    email = forms.EmailField(max_length=100, null=True, blank=False)
    first_name = forms.CharField(max_length=20)
    password = forms.CharField(max_length=20, required=True, label="password", widget=forms.PasswordInput)
    last_name = forms.CharField(max_length=20)
    date_joined = forms.DateTimeField(auto_now_add=True, auto_now=False)
    date_ = forms.DateTimeField(auto_now_add=False, auto_now=True)

Views.py

def register_user(request):
    if request.method == 'POST':
        print "Saisisis"
        form = UserForm(request.POST)     # create form object
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/register_success')
    print "blah"
    args = {}
    args.update(csrf(request))
    args['form'] = UserForm()
    # import pdb
    # pdb.set_trace()
    print args
    return render(request, 'pages/signup.html', args)

and my html:

{% extends 'pages/base.html' %}
{% block additional_styles %}
<style>
    body{
    background:url(static/img/nose.jpg) no-repeat center center fixed;
    -webkit-background-size: cover
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    }
</style>
{% endblock %}

{% block contentblock %}
<div class="container well">

    <h1> Please Sign Up fellas</h1>

    <form method="POST" action="login.html">
        {% csrf_token %}
        {{ form.as_table }}

        <input type="submit" value="OK">

    </form>
</div>

{% endblock %}

Upvotes: 2

Views: 4611

Answers (1)

markwalker_
markwalker_

Reputation: 12849

To do what you've got there, you'd need to have a ModelForm so that when you call form.save() Django knows what the model you are creating an instance of. For example;

Forms.py:

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


class UserForm(forms.ModelForm):
    email = forms.EmailField(max_length=100, null=True, blank=False)
    first_name = forms.CharField(max_length=20)
    password = forms.CharField(max_length=20, required=True, label="password", widget=forms.PasswordInput)
    last_name = forms.CharField(max_length=20)
    date_joined = forms.DateTimeField(auto_now_add=True, auto_now=False)
    date_ = forms.DateTimeField(auto_now_add=False, auto_now=True)

    class Meta:
        model = User

But going from what you've got you'd need to create the model instance yourself, then set the data, then save it;

def register_user(request):
    if request.method == 'POST':
        form = UserForm(request.POST)     # create form object
        if form.is_valid():
            email = form.cleaned_data['email']
            user = User(email=email)
            user.save()
            return HttpResponseRedirect('/accounts/register_success')

Upvotes: 3

Related Questions