首页 \ 问答 \ django如何使用模板(django how to work with templates)

django如何使用模板(django how to work with templates)

我现在正在练Django,而且我被卡住了。 当我跑:

>>> from django import template
>>> t = template.Template('hi')

给我:

ImportError:无法导入设置,因为未定义环境变量DJANGO_SETTINGS_MODULE。

知道是什么原因引起的吗? 我该怎么办?


I'm practising Django now, and I'm stuck. When I run:

>>> from django import template
>>> t = template.Template('hi')

Gives me:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

Any idea what causes this? What should I do?


原文:https://stackoverflow.com/questions/4615530
更新时间:2022-01-10 16:01

最满意答案

我猜这是因为你的Views文件夹有一个web.config ,它明确阻止了对它的请求。 出于安全原因,默认情况下将其置于MVC项目中。

请注意Views/Web.Config的以下代码

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

如果您对此进行评论,那么您的代码应该可行。

但是,我建议将角度视图移动到一个不同于MVC的事实上保留文件夹的文件夹。 更改为ngView代码,此代码将起作用。 它会更清洁,更清晰,并且您不会冒险将您的剃刀代码暴露给窥探用户。

更新根据评论,上述问题不是问题。 问题是没有从提到的文件中使用重写规则。 如果可能的话,将它们移动到web.config - 这将避免设置适用于所有iis express应用程序的全局规则。


I am guessing this is because your Views folder has a web.config that explicitly blocks request to it. That is placed there by default in MVC projects for security reasons.

Note the following code in Views/Web.Config

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

If you comment this out, your code should work.

However, I would advise moving your angular views to a different folder that isn't sort of a de-facto reserved folder for MVC. Change to something like ngView and this code will work. It will be cleaner and clearer that way, and you don't run the risk of exposing your razor code to snooping users.

UPDATE The above is not the issue, per comments. The issue is that the rewrite rules were not being used from the file mentioned. Move them to web.config if possible - that will avoid setting up global rules that will apply to all iis express apps anyway.

相关问答

更多
  • 这解决了我的问题: https://github.com/alisabzevari/ngval 如果我们想要更深入(我现在不需要),我们可以有这样的: https://bitbucket.org/asiemer/angularjs.mvc/wiki/Home This solves my problem: https://github.com/alisabzevari/ngval If we want to go deeper (which I don't need now), we can have th ...
  • 这取决于您如何设计应用程序架构和通信。 首先,如果您使用Action方法中的ViewResult结果类型,则必须使用MVC路由。 了解angularjs $routeProvider仅启用客户端路由,并且与MVC(服务器端)路由不同。 您使用$location对象捕获地址栏的URL更改,并且角度路由捕获请求(在它有机会向服务器触发之前)并且提供配置的模板和控制器。 希望您了解,然后您(通常)会(通常)使用$http服务来触发对将使用MVC定义的路由的服务器上的相应操作的异步调用。 逻辑上,使用角度侧的模板类 ...
  • 在我的网站http://www.reviewstoshare.com上 ,我正在使用AngularJS以及ASP.NET MVC。 我没有一直用AngularJS的主要原因是SEO不容易实现与AngularJS。 请记住,我的网站已经根据需要使用ASP.MVC + Jquery进行页面交互。 另一方面,网站上仍然有一些“Ajaxy”性质,如评论,投票,举报等。与Stackoverflow本身不同。 在AngularJS之前,这是一个Jquery插件和$(document).ready()回调函数,更不用说J ...
  • 我的2美分值。 序言 - 我已经在角度和淘汰赛工作。 我在我的第三个非常简单的前端用MVVM / MVC lib构建。 我开始敲门,因为它的MVVM非常类似于wpf / silverlight机制。 它工作得很好 教程和文档是一流的。 所有您的编程人员将能够在几天内使用knockout.js,或者在几秒钟内使用mvvm .net。 然而,这些天我使用角度很大,可能会坚持下列原因。 角度是一个完整的框架 - 淘汰是真正的只是2路绑定。 您仍然需要其他库,如backbone.js / jquery来完成其余的工 ...
  • 我认为WebApi和SPA应该是完全不同的项目。 当你像nginx一样设置你的前端web服务器时,你可以配置它来提供来自ASP.NET web api的所有/ api请求,并通过SPA处理其他请求。 因此,它可以清晰分离您应用程序的不同层次,并允许您将来轻松添加其他后端服务和前端SPA应用程序(如果需要)。 I think WebApi and SPA should be completely different projects. When you setup your frontend webserve ...
  • 我猜这是因为你的Views文件夹有一个web.config ,它明确阻止了对它的请求。 出于安全原因,默认情况下将其置于MVC项目中。 请注意Views/Web.Config的以下代码
  • 使用您的配置进行基本设置: 从那以后你应该能够关注其他NG2教程,祝你好运! Visual Studio 2013社区版,带有.NET 4.5 Angular 2设置 Visual Studio 2013与Angular 2 Basic setups using your config: From there on you should be able to follow other NG2 tutorials, good luck! Visual Studio 2013 Community Edition ...
  • 经过多年努力解决这个问题仍然无法解决这个问题,但提出了另一种解决方案。 分享解决方案。 希望它可以帮助其他人。 我们的目标是重写网址,以便我们实际登陆我们要求的视图。 因此,为此,我使用了这种方法,我们为asp.net应用程序提供了Application_BeginRequest事件/方法。 这是在服务器处理每个请求之前执行的。 我们可以使用这种方法进行网址重写这是一个简单的例子。 protected void Application_BeginRequest(object sender, EventArg ...
  • 我可以想到两种可能的实现方式 完全分开的AngularJS & C#应用程序 。 AngularJS应用程序将是独立的应用程序,处理所有UI逻辑,路由逻辑,用户界面模板,拥有自己的视图模型。 C#应用程序(我建议你使用WebAPI / WCF或任何你喜欢的)将公开HTTP端点为AngularJS应用程序调用 PROS : 可重用性 (终端可以在任何地方使用,例如原生移动应用), 轻量级 (部署包可能只包含一些js文件和html文件), 灵活性 (您可以轻松切换技术,例如:将服务器端切换为使用NodeJS ) ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。