简体   繁体   English

Django用于简单的Web应用程序

[英]Django for a simple web application

I'm developing an app (an API) in python and I would like to offer some of its functionality through a web interface (like web services do). 我正在开发python中的应用程序(API),我想通过Web界面(如Web服务)提供一些功能。 I've been looking at django, but I don't know if really fits well in my idea. 我一直在看django,但我不知道我的想法是否真的很合适。 I only want to create a web page that invokes to my API methods in order to acomplish the functionality that offers that web page. 我只想创建一个调用我的API方法的网页,以便完成提供该网页的功能。 But, after followed the tutorial, I feel a little confused about the point of django. 但是,在遵循教程之后,我对django的观点感到有些困惑。 It seems to me to be more related with an ORM than a classic web application. 在我看来,与ORM相比,与经典的Web应用程序更相关。

Is django a solution so heavy for such a simple development (as I mentioned, make calls to my API through the web)? 对于这样一个简单的开发来说,django是一个如此沉重的解决方案(正如我所提到的,通过网络调用我的API)? Do I always have to use a database? 我总是要使用数据库吗?

Thanks. 谢谢。

I love django but there is an lot of it to get your head around! 我喜欢django,但有很多东西可以让你满意! If you don't want the database bit, focus on urls.py and views.py that will process your urls and return the info you want as an http response. 如果您不想要数据库位,请关注urls.py和views.py,它们将处理您的网址并将您想要的信息作为http响应返回。

eg. 例如。 urls.py urls.py

urlpatterns += patterns('myapp.views',

    url(r'^getstuff/$', 'getstuff' ),
)

in views.py 在views.py中

def getstuff(request):

   do whatever in python

   return HttpResponse(stuff to return)

You don't need to use database in Django projects. 您不需要在Django项目中使用数据库。 Basically django comes with some standardized architecture that follows MVC pattern (or MVT as sometimes described). 基本上django带有一些遵循MVC模式的标准化体系结构 (或者有时描述的MVT)。 This includes models, views, url dispatching, templates, etc. 这包括模型,视图,URL调度,模板等。

Probably you need to do following things to accomplish your task: 可能你需要做以下事情来完成你的任务:

  1. create url definition in urls.py to some django view 在urls.py中创建url定义到某个django视图
  2. write django view that call somehow your api and displays result as a web page 写django视图,以某种方式调用您的api并将结果显示为网页

you don't need models and database at all but you need to get familliar with views, urls, templates. 你根本不需要模型和数据库,但你需要获得视图,网址,模板的功能。 It might look like a big machinery for your simple case but if you have some time I encourage you to these django basics. 对于你的简单案例,它可能看起来像一个大机器,但如果你有一些时间,我鼓励你这些django基础知识。

If you are looking for somethin much simpler I heard about webpy project . 如果你正在寻找更简单的东西我听说过webpy项目 This might be better option if you need something really simple. 如果您需要非常简单的东西,这可能是更好的选择。

An important question is: Do you want the web services to be provided by a full-featured server like Apache, or are you just looking at the "web server" to be a thread (or equivalent) in your program? 一个重要的问题是:您是否希望Web服务由Apache等功能齐全的服务器提供,或者您只是将“Web服务器”视为程序中的一个线程(或等效服务器)?

If you want to run Apache, then I'd recommend something like Werkzeug , which will handle most of the WSGI stuff for you. 如果你想运行Apache,那么我会推荐像Werkzeug这样的东西,它会为你处理大部分的WSGI。 For templating, I've heard good things about Jinja2 . 对于模板,我听说过关于Jinja2的好东西。

If that is too much, and all you want is a lightweight, simple server (something that, say, just spits out some HTML or XML when asked, and doesn't need any fancy URL handling), you can use the SimpleHTTPServer or CGIHTTPServer modules that ship with Python. 如果那太多了,你想要的只是一个轻量级,简单的服务器(比如,当被问到时只是吐出一些HTML或XML,并且不需要任何花哨的URL处理),你可以使用SimpleHTTPServerCGIHTTPServer随Python一起提供的模块。

Django is a full-featured, integrated package that provides almost everything you need to write database-backed web applications. Django是一个功能齐全的集成软件包,几乎可以提供编写数据库支持的Web应用程序所需的一切。 While its various components can be used in isolation, if you're only using one thing (the template and view engines, in your case), it is probably overkill. 虽然它的各种组件可以单独使用,但如果你只使用一件事(模板和视图引擎,在你的情况下),它可能是过度的。

No need for a framework at all. 根本不需要框架。 Raw wsgi isn't hard but a little verbose. 原始的wsgi并不难,但有点冗长。 So I like to use WebOb 所以我喜欢使用WebOb

Here's Raw wsgi 这是Raw wsgi

def application(environ, start_response):
    start_response("200 OK", [])
    return ["<html><body><h1>Hello World</h1></body></html>"]

Here's the webob version 这是webob版本

from webob.dec import wsgify
from webob import Request

@wsgify
def application(request):
    return Response("<html><body><h1>Hello World</h1></body></html>")

That's enough to run under apache mod_wsgi, and there are plenty of libraries that you can use that expect/produce webob Request and Responses. 这足以在apache mod_wsgi下运行,并且有很多库你可以使用它们期望/产生webob请求和响应。 Anything that Turbogears 2 or repoze.bfg uses is fair game at that point. 任何事情的TurboGears 2repoze.bfg采用的是在这一点公平的游戏。

You definitely don't have to use a database with Django. 你绝对不必使用Django数据库。 Whether it fits your needs, only you can tell. 是否符合您的需求,只有您能说清楚。 There are other Python web frameworks that you can use. 您可以使用其他Python Web框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM