简体   繁体   中英

Input give in the form not saving to the database

This is the model i created make a form. Two more fields are added in the form by using abstract user. models.py

from django.db import models
from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    phone=models.CharField(max_length=20)
    profile_pic=models.ImageField(upload_to="dp",null=True,blank=True)

Registration form is inherited from UserCreationForm.

forms.py

from django.contrib.auth.forms import UserCreationForm
from socialapp.models import MyUser
from django import forms

class RegistrationForm(UserCreationForm):
    widgets={
        "password1":forms.PasswordInput(attrs={"class":"form-control"}),
        "password2":forms.PasswordInput(attrs={"class":"form-control"})
    }
    class Meta:
        model=MyUser
        fields=["first_name","last_name",
                "username","email",
                "phone","profile_pic",
                "password1","password2"]
        widgets={
            "first_name":forms.TextInput(attrs={"class":"form-control"}),
            "last_name":forms.TextInput(attrs={"class":"form-control"}),
            "username":forms.TextInput(attrs={"class":"form-control"}),
            "email":forms.TextInput(attrs={"class":"form-control"}),
            "phone":forms.TextInput(attrs={"class":"form-control"}),
            "profile_pic":forms.FileInput(attrs={"class":"form-control"}),
        }

Here is the view to map to the template views.py

from django.shortcuts import render
from socialapp.forms import RegistrationForm
from socialapp.models import MyUser
from django.views.generic import CreateView
from django.urls import reverse_lazy



class SignupView(CreateView):
    model=MyUser
    form_class=RegistrationForm
    template_name="register.html"
    success_url=reverse_lazy("register")
    
    

    

This probably didn't work because first of all, you have a model MyUser and all the attributes of this model are phone and profile_pic . Adding all those attributes in the form may or may not render well in the template but, that doesn't change the model.

The way to go about this is to add the attributes in the model and then modify these attributes to your taste in the forms.

Second of all, you didn't even save the form in your views:-) Try this:

 class SignupView(CreateView): model=MyUser form_class=RegistrationForm template_name="register.html" success_url=reverse_lazy("register") def form_valid(self,form): return super().form_valid(form)

remember to register the MyUser model in the admin if you haven't already done that.

 from django.contrib import admin # Register your models here. from.models import MyUser admin.site.register(MyUser)

You didn't post a template, but your template should resemble this:

 <form action="" method="POST" enctype="multipart/form-data"> {{form|crispy}}{%csrf_token%} <button class="btn btn-lg bg-primary " type="submit" style='float: right;margin: 5px 0 10px;'> Done! </button> </form>

You may also consider using function-based view so you are more in control of what goes on in the views.

Be sure to explain further how I can help you if you are still stuck.

Have a nice day:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM