无法在django / python应用程序中集成小脚本…您能解释urls.py的工作原理吗?

问题描述

| 我不是程序员,但是我在php / mysql中写了一些东西。因此,我掌握了编程知识,但是我无法理解Django应用程序的逻辑……而python是如此的复杂!请帮助我解决我遇到的一个小问题。 我的脚本很简单,它显示随机句子。没有什么花哨! 因此,我已经: random-quotes.py->
import random

#Random
def random_quotes (request,template_name=\'header.html\'):
    quotes = [\"\\\"There are always two people in every picture: the photographer and the viewer.\\\"\",\"\\\"My portraits are more about me than they are about the people I photograph.\\\" <br><span class=\\\"author_span_style\\\">~ Richard Avedon</span>\"]
    pick_quote = quotes[random.randint(0,len(quotes) - 1)]
    return render(request,template_name,{
                                           \'random_quote\': pick_quote,}) 
header.html->
{{ random_quote }}
urls.py->
url(r\'^$\',app.random-quotes.random_quotes,name=\'random_quotes\'),
这个网址没什么好奇怪的。我想到将模板调用映射到\“ action \”文件。但是细节和原因让我无所适从! 无论如何,我使用了现有django应用程序中的url结构,(我在其中集成了小脚本) 因此,app。[random-quotes]。{random_quotes} [是文件名]和{是函数名} 应用程式。我不确定,但我想指的是Django应用中的子文件夹。我猜这将是应用程序。就像在urls.py中一样,我发现了对该文件夹中文件的引用。 由于某种奇怪的原因,脚本无法运行,它什么也不显示...所以我想问题应该是第一部分r \'^ $ \'和我没有指定网址。 请帮助我理解这个概念...     

解决方法

        本教程很好地解释了这里发生的事情。 简而言之,任何与urls.py中的正则表达式匹配的URL都将映射到views.py中的指定函数。 views.py将执行任何必要的逻辑,并将加载模板(如果指定)。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...