Skip to content

Instantly share code, notes, and snippets.

@einnar82
Last active May 4, 2020 07:37
Show Gist options
  • Save einnar82/45ece2a96a19181c4b6515104ccf3590 to your computer and use it in GitHub Desktop.
Save einnar82/45ece2a96a19181c4b6515104ccf3590 to your computer and use it in GitHub Desktop.
Mass assignment in Django Forms
from django import forms
class RegistrationForm(forms.Form):
username = forms.CharField(max_length=100)
password = forms.CharField(max_length=100)
email = forms.CharField(max_length=100)
phone = forms.CharField(max_length=100)
from django.db import models
from django.utils import timezone
# Create your models here.
class User(models.Model):
class Meta:
db_table = 'users'
# change the pluralize behavior of model
verbose_name_plural = 'users'
username = models.CharField(max_length=100)
password = models.CharField(max_length=100)
email = models.CharField(max_length=100)
phone = models.CharField(max_length=100)
from django.shortcuts import render
from .models import User
from .forms import RegistrationForm
from django.http import HttpResponseRedirect
# Create your views here.
def register(request):
form = RegistrationForm(request.POST)
if form.is_valid():
register = User.objects.create(**form.cleaned_data)
return HttpResponseRedirect('/news/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment