Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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_Sql_Database_Django_Views - Fatal编程技术网

从数据库中替换句子中的单词(Python/Django)

从数据库中替换句子中的单词(Python/Django),python,sql,database,django,views,Python,Sql,Database,Django,Views,我的句子结构是这样的: 从$Duration%开始,$名词$将是$形容词$ 我需要一个视图来检查“$$”中的单词,并用从SQL文件导入的数据库中的随机单词替换它们 以下是模型/sql文件: from django.db import models from django.utils import timezone class Type(models.Model): type = models.CharField(max_length=50) value = models.Cha

我的句子结构是这样的:

从$Duration%开始,$名词$将是$形容词$

我需要一个视图来检查“$$”中的单词,并用从SQL文件导入的数据库中的随机单词替换它们

以下是模型/sql文件:

from django.db import models
from django.utils import timezone

class Type(models.Model):
    type = models.CharField(max_length=50)
    value = models.CharField(max_length=1)


class Word(models.Model):
    dict = models.CharField(max_length=200)
    kind = models.CharField(max_length=1)

    def randword(self):
        words = self.objects.all()
        number = randrang(0,len(words))
        return words[number]


class Question(models.Model):
    question = models.CharField(max_length=200)

    def randquest(self):
        quests = self.objects.all()
        numbeq = randrang(0, len(quests))
        return quests[numbeq]


class Meta(models.Model):
    question = models.ForeignKey(Question)
    score = models.ForeignKey("Score")
    user = models.CharField(max_length=200)
    time = models.DateTimeField('date published')


class Score(models.Model):
    word = models.ForeignKey(Word)
    score = models.IntegerField()
    question = models.ManyToManyField(Question, through=Meta)

    def __unicode__(self):
        return self.score
SQL文件

INSERT INTO quest_question (question) VALUES ('Starting in $Duration$, the $Noun$ will be $Adjective$');

我开始写这个观点。需要一些帮助:

from quest.models import Word, Type, Score, Question, Meta
from django.utils import timezone


def question(request):
    sentence = Question.objects.all()
    find_words = re.findall(r"\w+%0-9a-zA-Z%", sentence.question)

如果您愿意更改符号,您有两个选择:


  • 其余的部分是从数据库中提取并选择单词at,这是最简单的部分。

    是否固定了
    $…$
    符号?否。我只是使用了“$”作为标记。可以替换。是的,我可以更改符号。行“t=Template('从{Duration}开始,{{Noun}}}将是{{形容词}}'”,您是否假设问题保持不变?我也想为任何问题生成它。您可以将任何您喜欢的内容传递给
    模板
    构造函数。我将如何更改>>c=Context(dict(Duration='24小时',名词='Coffee Maker',形容词='Break'))所以它会生成随机单词?你有没有花点时间看看答案最后一句中的链接?
    from quest.models import Word, Type, Score, Question, Meta
    from django.utils import timezone
    
    
    def question(request):
        sentence = Question.objects.all()
        find_words = re.findall(r"\w+%0-9a-zA-Z%", sentence.question)
    
    >>> 'Starting in {Duration}, the {Noun} will be {Adjective}'.format(Duration='24 hours', Noun='Coffee Maker', Adjective='broken')
    'Starting in 24 hours, the Coffee Maker will be broken'
    >>> 'Starting in %(Duration)s, the %(Noun)s will be %(Adjective)s' % dict(Duration='24 hours', Noun='Coffee Maker', Adjective='broken')
    'Starting in 24 hours, the Coffee Maker will be broken'
    
    >>> from django.template import Context, Template
    >>> t = Template('Starting in {{Duration}}, the {{Noun}} will be {{Adjective}}') 
    >>> c = Context(dict(Duration='24 hours', Noun='Coffee Maker', Adjective='broken'))
    >>> t.render(c)
    u'Starting in 24 hours, the Coffee Maker will be broken'