I'm new to Python and Django, but now have a pretty firm understanding of basic database and back end programming. However, I am finding it hard to learn the views and templates layers. I was wondering if anyone can suggest additional tutorials and resources, other than the official Django documentation.
I am also new to HTML, and am open to tutorials using Mako or Jinja2.
Thanks!
We've been using Mako+Django for almost two years on a large project, and it's been great. Mako is immensely better than the standard Django templates. Why on Earth would I want to learn another templating language when I already have to know Python, Javascript, HTML, JQuery, etc.? With Mako I can use regular Python, including all the libraries, in my HTML pages. And with Django integrated on the back end, the templates stay simple.
You might want to look at my Django+Mako starter kit. It does the templates the way I like to do them (not necessarily the standard way), but it is a project fully set up and ready to run. You can start from it and customize. http://warp.byu.edu/dist/django-mako.zip
Update: blog has moved to http://www.goalbrecht.com/2013/10/django-mako-plus-a-starter-kit/
and additional updates to the project are on Django-Mako-Plus' Github.
We use Mako in our workplace, and I highly recommend not using it. It's great for your own views, but if you want to use ANY third party libraries that include templates, they simply won't work.
Other than that, the official documentation, and googling for specific problems you're having is the best way to go when using django templating. Unfortunately, I can't comment on jinja templating.
Related
This is not a HOWTO question, as it, as such, has been answered before here
I am trying to integrate Django with modern frontend framework, and I found that it is possible to store and render Django templates from models. Since it is not a standard, I am wondering what are the advantages (or disadvantages if that's the case) of file based templates.
Reading though the documentation, I have seen that it is recommended to actually cache templates and models as much as possible for best performance, so why would it not be recommended to store templates in the database? It seems very convenient to me that in doing so pages can be edited from the admin panel (where you can add a code editor), which, along with the rest framework and a front end framework synergize very well.
From my research, the template tags and template language seem to work and the context can be passed in a view as well. the only thing I cannot figure out is the {include .. } tag, as it does not seem to point to a view. (although a custom tag cam be made to have this function)
Can such a setup be used in production? or are there security/performance/other concerns?
It's not recommended because templates are the developer's responsibility, not the site admin's.
Otherwise, there's not much performance difference between reading files directly from filesystem and from database.
One drawback of this approach is you don't get revision history (using git, mercurial etc). Sure, you can implement something similar to save the revisions in the database, but then why not use the better tools which already exist?
There's a Django flatpages app which lets you save HTML content in database. But the purpose of the app is to allow site admins to edit HTML content, such as information for an "About Us" page, because writing this info is not a developer's responsibility.
I am new to Django, but heard it was promising when attempting to create a a custom CMS.
I am looking to get started, but their seems to be a lack of documentation, tutorials, etc on how to actually get something like this going.
I am curious if their are any books/tutorials/guides that can help me get started with CMS django building.
PS- I have heard of django-cms, but am unsure what exactly it is and how it is different from django.
Here is a list of tutorials that has been put together by authors of Two Scoops of Django. http://twoscoopspress.com/pages/django-tutorials
django CMS is a CMS on top of django. It supports multiple languages really well and plays nice together with your own django apps. The basic idea is that you define placeholders in your template and then are able to fill those placeholders with content plugins. A content plugin can be a anything from text, picture, twitter stream, multi column layout etc.
You can look into this, it's about creating placeholder that can be replaced later, which is what CMS is, technically
https://docs.django-cms.org/en/latest/introduction/02-templates_placeholders.html
Say you have an existing database-backed Django site. Something simple, like single model containing a record for every minor league baseball team. The model is accessed by one view that lists all the teams, and another that accepts a slug and then creates a detail page all about that team.
Is there a good option for converting the app into a stack of baked out flat files, so that it could be served from a static file service like Amazon's S3?
I've toyed with Hyde but it's not clear to me how it applies to an existing site backed by a database.
Any advice would be very much appreciated. Thank you in advance.
django-medusa is largely unmaintained. These are some alternatives mentioned in the project's README:
django-bakery, built and maintained the lovely people at the Los Angeles Times Data Desk. (Read about it
here.)
The alsoicode/django-medusa fork, by Brandon
Taylor. Among other things, it's been
kept up to date for newer versions of Django.
django-freeze by Fabio Caccamo.
django-staticgen by Mishbah Razzaque.
I understand your intent, but any decent framework these days offers some sort of caching mecanism that alleviate the pains of dynamic content. With a properly implemented cache, the difference between static and dynamic will be trivial. Trust me.
Happy coding, friend.
A new one has just been announced, though it has existed and been used by its author for quite some time:
django-medusa.
I haven't tried using it yet, but it sure looks good: I will be!
I have a similar setup using Django but on GAE. I have created a build script which I use to build my static HTML files. Since GAE's version of Django templates is slightly different, this might need some testing.
But essentially you do something like
from django.template import Template, Context
from django.template.loader import get_template
t = get_template('template1.html') #Need to verify this. GAE template allows me to just call template.render(path, context). Not sure about pure Django templates
c = Context({ 'variable' : 'value'})
with open("file1.html", "w") as f:
f.write(t.render(c)) #This works for me, but if this doesn't in pure Django, try render_to_string
HTH
You are asking for a dynamic site to host in a static environment, that is impossible. The only way is to export all the files, put them into a static server. You can do it with wget, it will copy all the files and convert them to html.
One fallback of this problem is, it can only create html files, if there are links to the pages, ie. search forms etc. will not work, Javascript based linking may not work.
I'm not familiar with anything that will convert an existing django site to a static html site. It might be worth looking at the suite of new django/python PaaS providers. A site like this should be easy to get running on one of these platforms and it should be pretty cheap month to month.
Some providers you might want to checkout include:
Gondor.io
ep.io
Django Zoom
I'm a C/C++ developer and I also have experience developing web apps with C#, ASP.NET MVC and fluent nhibernate. I'm looking for non-MS alternatives for web development and I'm really interested in python so I went out after Django but I've been told that Django makes it difficult for me to personalize my HTML (not sure if this is accurate).
What I'm looking for is a Python web development framework that is integrated with an ORM, is able to generate the interfaces BUT provides an easy way for me to customize the interface to create an AJAX intensive app
go for django.
does all you wanted,
has perfect docs and even free book,
partially runs on appengine,
has really large user base,
it is mature:
db sharding, (With model router)
xss protection in forms
memcache,
localisation,
well tested support for unicode,
really easy to learn because of level of it documentation.
I'm using Flask (a very minimal web framework) and SQLAlchemy as my ORM. I'm exceedingly happy with it. Disclaimer: I'm only using this for personal projects at the moment, though I do plan to launch a web app in the next 6 months using this setup.
Various options in Python you can look at -
Django (obviously!)
Pylons
Nagare
Flask
Django is really good. And no your info is not correct, HTML templates are real easy to edit them.
Also this is from a developer of Nagare -
Ajax without to write any Javascript
code or the use of continuations makes
a Web application looks like a desktop
one. In fact we have often found that
developers like you, without prior Web
experiences, can be quicker to get
Nagare because they have nothing to
"unlearn".
I am going deeper into this framework Since you said that your app is AJAX intensive. From what I have heard, Nagare makes it easy to do so...
All these frameworks are really good. Some are really good in some areas, others not. So may be explore them all & see which best suits your purpose.
For Web applications development, we're using Nagare, coming with YUI for AJAX communications.
Having a look to Nagare might be an option.
I'm in agreement with the rest of the answers and think that Django is by-far the best choice as a "complete framework" and I think their template system is second-to-none.
If you are looking to create an ajax intensive application, I'd suggestion checking out django-piston (http://bitbucket.org/jespern/django-piston/wiki/Home). Piston is a REST API framework built on top of Django. I've used it for a number of ajax intensive applications and have found it's workflow to be incredibly clean, quick and flexible.
If you are wanting to go a bit slimmer and lighter-weight though, I'd suggest checking out web.py (http://webpy.org/) or Tornado (http://www.tornadoweb.org/).
I would definitely look into Pylons which is very thoroughly documented and has sql alchemy (one of the best python ORM's) baked in. Plus it's easy to setup and learn.
I currently am working with a framework called restish which is flavor of pylons that (surprise, surprise) puts the focus on sticking to RESTful web design. I don't think it's exactly what you're looking for in that it lacks good documentation and any form of an ORM.
Just A side note I'm pretty sure that Django uses Mako templating which gives you excellent control over the HTML.
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.