我的应用名称是 search_keywords。在创建这个应用程序时,我将有一个名为 models.py 的文件,我在其中编写了这段代码:
from django.db import models
class Keywords(models.Model):
file_name = models.CharField(primary_key=True, max_length=100)
frequency_count = models.IntegerField()
然后将此应用程序添加到 INSTALLED_APPS 并运行 python manage.py syncdb。运行此命令时,我将在 django 中自动创建一个表。然后运行 python manage.py sql search_keywords。它将根据需要显示表格。
然后下一步是运行 python manage.py shell。而不是运行这个,我想在通过我创建的 python 代码创建的表中插入值。代码是:
#!/usr/bin/python
#here skey.py is another file created by me and has the imported functions
#in this code
from skey import find_root_tags, count, sorting_list
str1 = raw_input("enter the word to be searched\n")
list = []
fo = open("xml.txt","r")
for i in range(count.__len__()):
file = fo.readline()
file = file.rstrip('\n')
find_root_tags(file,str1,i)
list.append((file,count[i]))
sorting_list(list)
fo.close()
我想在 django 创建的表中插入这个列表元素,并且在调用函数排序列表之后也是如此。该列表包含文件名及其计数。例如 list=[('books.xml','3'),('news.xml,'2')]。
我怎样才能做到这一点?
请帮忙。
///////////////////////////////////////// /////////////////////////
嘿,我已经写了代码:
#!/usr/bin/python
#to tell django which settings module to use
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from search.models import Keywords
from skey import find_root_tags, count, sorting_list
str1 = raw_input("enter the word to be searched\n")
list = []
fo = open("xml.txt","r")
for i in range(count.__len__()):
file = fo.readline()
file = file.rstrip('\n')
find_root_tags(file,str1,i)
list.append((file,count[i]))
sorting_list(list)
for name, count in list:
s = Keywords(file_name=name,frequency_count=count)
s.save()
fo.close()
这里 django_project = mysite #my project's name and app = search #my app's name
在运行此代码时,它给了我错误:
回溯(最近一次通话最后):
文件“call.py”,第 7 行,在
从 search.models 导入关键字
ImportError:没有名为 search.models 的模块
并包括:
import sys
sys.path.insert(0, path_to_django_project)
这在上面的代码中给出了错误:
回溯(最近一次通话最后):
文件“call.py”,第 4 行,在
sys.path.insert(0,path_to_mysite)
NameError:名称'path_to_mysite'未定义
为什么?我在桌面上有我的项目和上面的python代码文件。请帮忙!!
/////////////////////////////////////////////////////////////////////////////////////////////////////////现在它给我这个错误help.see it at: 在 python 代码中访问在 django 中创建的表时出错