简体   繁体   English

以不保存到数据库的形式输入

[英]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模型.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.注册表单继承自UserCreationForm。

forms.py表单.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这是映射到模板 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 .这可能不起作用,因为首先,您有一个模型MyUser并且该模型的所有属性都是phoneprofile_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.如果您还没有注册MyUser模型,请记住在管理员中注册。

 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:-)祝你今天过得愉快:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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