简体   繁体   English

如何将表单连接到 Django 中的模型?

[英]How do I connect forms to models in Django?

I've just started learning Django, and I have questions regarding forms and models.我刚刚开始学习 Django,我对表单和模型有疑问。

So what I'm trying to create, in simplified feature, is a user inputs his/her basic information--phone #, instagram account, facebook account, and so on--then the data is stored in database and show up dynamically to the user.所以我试图在简化功能中创建的是用户输入他/她的基本信息——电话号码、instagram 帐户、facebook 帐户等——然后数据存储在数据库中并动态显示到用户。 Just like social media.就像社交媒体一样。 But I'm having confusion with forms and models.但我对表格和模型感到困惑。

What I first did was create forms, like below (forms.py) :我首先做的是创建表单,如下所示(forms.py):

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

class InputUserInfo(forms.Form):
    phone = forms.CharField(max_length=20)
    instagram = forms.CharField(max_length=20)
    facebook = forms.CharField(max_length=20)
    # and so on. Don't mind about which field to use.

then I have my views.py file, written as below:然后我有我的 views.py 文件,如下所示:

from django.shortcuts import render, redirect
from .forms import InputUserInfo

def inputuserinfo(response):
    if response.method == "POST":
        form = InputUserInfo(response.POST)
        if form.is_valid:
            form.save()
        return redirect('home')
    else:
        form = InputUserInfo()
    return render(response, 'inputuserinfo.html', {'form' : form }

then I have my inputuserinfo.html file, like below:然后我有我的 inputuserinfo.html 文件,如下所示:

{% extends 'base.html' %}
{% block content %}
<form method="post" action='/inputuserinfo/'>
    {% csrf_token %}
    {{form}}
    <button type='submit'>Done</button>
</form>
{% endblock%}

Now the problem is, I don't know what to do with my models.py.现在的问题是,我不知道如何处理我的models.py。 I don't know which code to write in models.py to store the input data into my database.我不知道在models.py 中编写哪些代码来将输入数据存储到我的数据库中。

I would very much appreciate your help guys.我非常感谢你们的帮助。 :) :)

In Django, a model is an interface to a particular database.在 Django 中,模型是特定数据库的接口。 It will create and issue SQL queries for you.它将为您创建和发出 SQL 查询。

A form is an interface to HTTP "GET" and "POST."表单是 HTTP“GET”和“POST”的接口。 It can generate the HTML to insert into a form-tag (but do not provide the tag itself), and they can interpret the data that is presented to the host from such a form.它可以生成插入表单标签的 HTML(但不提供标签本身),并且它们可以解释从这样的表单呈现给主机的数据。 Instead of monkeying around with the HTML data yourself, you let the Form object do the heavy lifting, both coming and going.与其自己处理 HTML 数据,不如让 Form 对象来做繁重的工作,来来去去。

Django provides many convenience shortcuts, such as ModelForm, which is a Form that can be based on the content of a Model. Django 提供了很多方便的快捷方式,比如ModelForm,它是一个可以基于Model内容的Form It will "quickly produce an acceptable-looking form" when you are in a hurry ... as you often are.当您赶时间时,它会“快速生成一个看起来可以接受的表格”……就像您经常做的那样。 And it can do things like save() directly because it knows what model you want to save the data to.它可以直接执行诸如save() 之类的操作,因为它知道您要将数据保存到哪个模型。

it looks like you left out the ModelForm.看起来您遗漏了 ModelForm。 Is there any reason you arent using Class Based Views.您是否有任何理由不使用基于类的视图。 This would be much easier?这样会容易很多吗?

Your ModelForm will end up looking something like this您的 ModelForm 最终将看起来像这样

    class InputUserForm(forms.ModelForm):
'''user form to create the user profile'''
class Meta:
    model = InputUserInfo
    fields = '__all__'

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

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