Last active
May 4, 2020 07:37
-
-
Save einnar82/45ece2a96a19181c4b6515104ccf3590 to your computer and use it in GitHub Desktop.
Mass assignment in Django Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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