I'm developing software on google app engine. I'm using the webapp framework. is there any way to avoid hard-coding urls in the html templates? django provides a solution, but I couldn't find one for webapp. thanks in advance..
WebApp is really very basic.
Django url templatetag is closely tied to its idea of URL resolving by matching regex patterns. As WebApp does not have any serious URL resolver, I imagine this would be really hard task to implement something in this line.
But you are not totally out of luck. Werkzeug has very easy to understand yet powerful URL routing mechanism that allows for reversing urls. You can try to base your implementation on this one (or throw away WebApp and use Werkzeug ;), it's really worth a try).
Webapp uses Django templates so:
check out Django templates here
The Django solution is the web app solution.
Related
I've been tasked with converting a service from Flask to aiohttp. Originally this service used Flask admin, but we are moving away from Flask. I saw that there is a aiohttp-admin package, but frankly, the documentation is very poor to almost non-existent and I can't wrap my head around the examples. Are there any other options for aiohttp admin pages out there? I've been using Jinja2 templates, but I think my solution is lacking.
Which exactly functionality do you lack in aiohttp admin? Please note the demos https://github.com/aio-libs/aiohttp_admin/tree/master/demos, they might contain fully or partially what you're looking for.
If you prefer to use jinja2 templates then aiohttp_admin2 is better for your case. The setup of aiohttp_admin2 is very simple and good cover into the documentation.
from aiohttp import web
from aiohttp_admin2 import setup_admin
app = web.Application()
# setup admin interface
setup_admin(app)
web.run_app(app)
It doesn't cover all flask-admin features, but you can add your own views or automatically generate CRUD views based on sqlAlchemy's models (or mongodb models) without JavaScript code.
This link to the official repository you can find here. Here you can find documentation with demo and examples of code.
I just started learning django. I was a rails developer before that. I was wondering if there is anything similar to resource in rails in django? Or do I have to make different urls and corresponding views for those urls? Thanks in advance!
django-smarter provides a similar pattern.
You are asking about RESTful APIs to the django models if I understand correctly. Django does not do it for you natively (as jpic said) but there are some packages you can install to do that:
django-smarter (as jpic said)
tastypie
django rest framework
See https://www.djangopackages.com/grids/g/rest/ for a list of APIs that can be added in to django. I see django-smarter isn't on that list. Maybe someone should add it.
I just started with Google App Engine using python and I was following a tutorial and writing my own little app to get familiar the webapp framework. Now I just noticed the tutorial does the following self.redirect('/'). So that got me wondering: is there a way to redirect to a handler instead of a hardcoded path? Thought that might be better so that you can change your urls without breaking your app.
One alternative would be to have a map of symbolic names to URLs, that way you could redirect to the mapped URL - you could then update your URLs with impunity.
Or if you'd rather just execute the code from another handler, I don't know why you couldn't just make a method call - worst case, you could extract a common method from the two handlers and call that.
This isn't a limitation of App Engine so much as the webapp framework. webapp is intended to be a lightweight framework, providing just the essentials.
If you want fancier redirect behavior, try Django.
The accepted answer made me built my own little router before I realized that in webapp2 you can name your routes and then redirect using that name as described in webapp2 Uri routing
app = webapp2.WSGIApplication(
routes=[webapp2.Route('/', handler='RootController', name='root')])
and then redirect to them in the RequestHandler
self.redirect_to('root')
if your path contains placeholders you can supply the values for the placeholders and the webapp2.Router will build the correct uri for you. Again take a look at webapp2 Uri routing for more detailed information.
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).
I've been looking at django, but I don't know if really fits well in my idea. 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. But, after followed the tutorial, I feel a little confused about the point of django. It seems to me to be more related with an ORM than a classic web application.
Is django a solution so heavy for such a simple development (as I mentioned, make calls to my API through the web)? Do I always have to use a database?
Thanks.
I love django but there is an lot of it to get your head around! 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.
eg. urls.py
urlpatterns += patterns('myapp.views',
url(r'^getstuff/$', 'getstuff' ),
)
in views.py
def getstuff(request):
do whatever in python
return HttpResponse(stuff to return)
You don't need to use database in Django projects.
Basically django comes with some standardized architecture that follows MVC pattern (or MVT as sometimes described). This includes models, views, url dispatching, templates, etc.
Probably you need to do following things to accomplish your task:
create url definition in urls.py
to some django view
write
django view that call somehow your
api and displays result as a web
page
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.
If you are looking for somethin much simpler I heard about webpy project. 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?
If you want to run Apache, then I'd recommend something like Werkzeug, which will handle most of the WSGI stuff for you. For templating, I've heard good things about 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.
Django is a full-featured, integrated package that provides almost everything you need to write database-backed web applications. 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. So I like to use WebOb
Here's 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
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. Anything that Turbogears 2 or repoze.bfg uses is fair game at that point.
You definitely don't have to use a database with Django. Whether it fits your needs, only you can tell. There are other Python web frameworks that you can use.
I am learning Django and got it to work with wsgi. I'm following the tutorial here:
http://docs.djangoproject.com/en/1.1/intro/tutorial01/
My question is: how can I customize the look and feel of Django? Is there a repository of templates that "look good", kind of like there are for Wordpress, that I can start from? I find the tutorial counterintuitive in that it goes immediately toward customizing the admin page of Django, rather than the main pages visible to users of the site. Is there an example of a "typical" Django site, with a decent template, that I can look at and built on/modify? The polls application is again not very representative since it's so specialized.
any references on this would be greatly appreciated. thanks.
Search for generic CSS/HTML templates, and add in the Django template language where you need it. Because unless you are trying to skin a particular app (such as the admin system), there is nothing Django-specific about any of your HTML.
The fact that you're thinking in terms of Wordpress templates, and that you think the tutorial's poll application is highly specialised, are hints that you haven't really grasped what Django is. It isn't a content management system or a blog engine, although it can be used to build those things.
There's no such thing as a typical Django site, and it simply doesn't make sense to have pre-packaged templates, because the front end could be absolutely anything at all - like a poll.
You write the template like you would write any standalone HTML+CSS page, perhaps with placeholders for the content, then turn those placeholders into actual Django template tags. If you know how to do write HTML, then you know how to make a Django template.
Actually Django does not have a "look and feel". You are probably referring to the built in Django Admin application. That app comes with its own templates.
There are third party applications that can change the Admin interface, Django Grapelli is a great example.
For any other application you want to build yourself, or download. Most likely you'll have to do the templates yourself. In order to come up with something pretty you need to learn about CSS/HTML/JS and design principles as the Django Templates will quite likely be out of your way.
I always recommend HTML Dog for learning the basics of HTML, CSS and JS.