Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django使用表单添加到数据库_Python_Django_Database_Python 2.7_Model View Controller - Fatal编程技术网

Python Django使用表单添加到数据库

Python Django使用表单添加到数据库,python,django,database,python-2.7,model-view-controller,Python,Django,Database,Python 2.7,Model View Controller,我试着用一个表格添加到我的数据库(这是一个大学列表),但它不起作用。知道为什么吗?它将我带到成功页面,但实际上并没有将其添加到数据库中 型号: class College(models.Model): college_name = models.CharField(max_length=120) logo = models.ImageField(upload_to="logos", default="logos/default.png") def __str__(self

我试着用一个表格添加到我的数据库(这是一个大学列表),但它不起作用。知道为什么吗?它将我带到成功页面,但实际上并没有将其添加到数据库中

型号:

class College(models.Model):
    college_name = models.CharField(max_length=120)
    logo = models.ImageField(upload_to="logos", default="logos/default.png")
    def __str__(self):
        return self.college_name
    @classmethod
    def create(cls, college_name):
        college = cls(college_name = college_name)
        return college
视图:

add.html:

<h2> Add your school </h2>
    <form action="{% url 'app:addcollege' %}" method="post">
        {% csrf_token %}
        <label for="collegename_input">School: </label>
        <input type="text" id="collegename_input" name="collegename_input" />
        <input type="submit" value="Add School" />
    </form>
添加您的学校
{%csrf_令牌%}
学校:

我遗漏了什么吗?

您处理POST请求的视图似乎不正确,因为它没有正确创建/保存模型。要保存学院,您应执行以下操作:

from django.shortcuts import redirect

def add_college(request):
    school = request.POST.get('collegename_input', '')
    # College.create(school)
    college = College(school_name=school)
    college.save()
    return redirect('add_success')
from django.shortcuts import redirect

def add_college(request):
    school = request.POST.get('collegename_input', '')
    # College.create(school)
    college = College(school_name=school)
    college.save()
    return redirect('add_success')