如何从views.py文件向django数据库插入数据?

nil*_*far 10 python django django-models django-views pymysql

如何从视图中的函数py文件向我的django数据库插入数据?是python manage.py shell唯一的插入方式吗?

有关我正在使用的更多解释:

  • python 3.4
  • django 1.8.2
  • PyMySQL

例如:

models.py:

from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    city = models.CharField(max_length=60)
Run Code Online (Sandbox Code Playgroud)

views.py:

from django.http import HttpResponse
import pymysql
from books.models import Publisher

def send(request):
    p = Publisher(name='Apress', city='Berkeley')
    p.save()
Run Code Online (Sandbox Code Playgroud)

urls.py

来自niloofar.views import send

url(r'^ index /',发送),

我想在加载页面索引时,send函数工作并将数据插入数据库.

这是行不通的.它没有给出任何错误,当我刷新索引页面时没有任何反应,没有任何内容被发送到数据库.我认为在我尝试插入数据的方式上存在语法错误.

让我注意到即使我跑的python manage.py shell话:

来自books.models import Publisher

p =出版商(name ='Apress',city ='Berkeley')

p.save()

什么都不会插入django数据库.

ils*_*005 20

你的问题很不清楚.你应该通过django教程.

但是确保您可以从视图中将数据插入到数据库中.假设您有一个名为的模型Foo:

models.py

class Foo(models.Model):
    name = models.CharField(max_length=100)
Run Code Online (Sandbox Code Playgroud)

view.py

from .models import Foo

def some_name(request):
    foo_instance = Foo.objects.create(name='test')
    return render(request, 'some_name.html.html')
Run Code Online (Sandbox Code Playgroud)


Kri*_*aes 11

您只需创建一个模型的实例并保存即可.假设您有一个文章模型:

from django.http import HttpResponse
from django.template import loader

from .models import Article


def index(request):
    article = Article()
    article.title = 'This is the title'
    article.contents = 'This is the content'
    article.save()

    template = loader.get_template('articles/index.html')
    context = {
        'new_article_id': article.pk,
    }
    return HttpResponse(template.render(context, request))
Run Code Online (Sandbox Code Playgroud)


Tan*_*yas 8

一个简单的方法是使用 create 函数。通过提及字段名称及其值。以下图示代码可帮助您将数据从 views.py 插入数据库并将数据库内容显示到 html 页面中。

假设我们有一个看起来像这样的表

Name      Age     Marks
Bunny      4       10
Tanishq    12      12
Run Code Online (Sandbox Code Playgroud)

models.py 看起来像这样

Name      Age     Marks
Bunny      4       10
Tanishq    12      12
Run Code Online (Sandbox Code Playgroud)

所以 views.py 看起来像

from django.db import models

# Create your models here.
class Student(models.Model):

    student_name = models.CharField(max_length = 120)
    student_age = models.IntegerField()
    student_marks = models.IntegerField()

Run Code Online (Sandbox Code Playgroud)

下面应该是home.html文件,显示所有输入的数据

from django.shortcuts import render
from .models import Student    # Student is the model class defined in models.py

# Assuming the data to be entered is presnet in these lists
stud_name = ['Aman', 'Vijay']
stud_age = [13, 12]
stud_marks = [20, 22]

def my_view(request, *args, **kwargs):
    
    # Iterate through all the data items
    for i in range(len(stud_name)):

        # Insert in the database
        Student.objects.create(Name = stud_name[i], Age = stud_age[i], Marks = stud_marks[i])


    # Getting all the stuff from database
    query_results = Student.objects.all();

    # Creating a dictionary to pass as an argument
    context = { 'query_results' : query_results }

    # Returning the rendered html
    return render(request, "home.html", context)


Run Code Online (Sandbox Code Playgroud)

插入所需的以下更改,否则您将收到类型错误

Student.objects.create(stud_name =stud_name[i],stud_age =stud_age[i],stud_marks =stud_marks[i])


Pet*_*ner 5

你可以尝试这个:

def myFunction(request):
    myObj = MyObjectType()
    myObj.customParameter = parameterX
    ...
    myObj.save()
Run Code Online (Sandbox Code Playgroud)