
Five Days to a Django Web App: Day Three, Coding - The Daily Build Thanks for coming back for Day Three! [Note: Sorry this post is a day late. It was all ready to go late yesterday, but some of the code included below triggered a bug either in WordPress or ScribeFire and the whole post got mangled. Progress So Far Yesterday we built some mockups, just HTML and CSS — nothing active. Armed with these mockups, we’re ready to get started coding. Foundation MySQL First, go to your web host’s control panel and create two MySQL databases for this project. You’ll get an error/warning if the test database exists when you first run tests. Set up the same databases on your local system: bash$ mysql -u root Welcome to the MySQL monitor. Then generate a skeleton for your project: django-admin.py startproject YOURPROJECT. Then dive right in to settings.py and change a few things: To make it easier to use the same settings file to test both locally and on your host, I add the following to my settings.py to set MEDIA_ROOT and TEMPLATE_DIRS: Adding Some Meat bash$ . <!
Python Programming Language – Official Website Serving Static Content With Django A question that is frequently asked by new Django programmers is: "How can I serve static content (css, images, javascript) with the Django development server?". This article is my attempt to answer that question by demonstrating the best practices way to do so. Why Doesn't Django Serve Static Content Automatically? Well, Django (and python in general) is built around the idea that it is better to be explicit than implicit. This means that Django doesn't force us to put all of static content into a single, specific folder or tree, we can set it up however we like. This flexibility is what Django provides for us at the cost of not being able to automatically detect / serve our static content--which is why you are reading this article :) Where Should I Put My Static Content? In general, the convention I like to use is to put all static content in my project directory underneath the 'static' folder. Which allows me to have a good looking URL schema for my projects. Configure Your Settings
Django aggregation tutorial — The Uswaretech Blog - Django Web Development By : Shabda Raaj One of the new and most awaited features with Django 1.1 was aggregation. As usual, Django comes with a very comprehensive documentation for this. Here, I have tried to put this in how-to form. Jump to howtos or Get source on Github. Essentially, aggregations are nothing but a way to perform an operation on group of rows. To do these operations Django added two new methods to querysets. aggregateannotate When you are have a queryset you can do two operations on it, Operate over the rowset to get a single value from it. The thing to notice is that option 1, will create one row from rowset, while option 2 will not change the number of rows in the rowset. In sql terms, aggregate is a operation(SUM, AVG, MIN, MAX), without a group by, while annotate is a operation with a group by on rowset_table.id. Ok enough talk, on to some actual work. Find the total number of employees. In sql you might want to do something like, select count(id) from hrms_employee Which becomes, The sql is
Functional Programming HOWTO In this document, we’ll take a tour of Python’s features suitable for implementing programs in a functional style. After an introduction to the concepts of functional programming, we’ll look at language features such as iterators and generators and relevant library modules such as itertools and functools. Introduction This section explains the basic concept of functional programming; if you’re just interested in learning about Python language features, skip to the next section on Iterators. Programming languages support decomposing problems in several different ways: Most programming languages are procedural: programs are lists of instructions that tell the computer what to do with the program’s input. The designers of some computer languages choose to emphasize one particular approach to programming. In a functional program, input flows through a set of functions. Functional programming can be considered the opposite of object-oriented programming. Formal provability Modularity Iterators
Writing your first Django app, part 4 This tutorial begins where Tutorial 3 left off. We’re continuing the Web-poll application and will focus on simple form processing and cutting down our code. Write a simple form Let’s update our poll detail template (“polls/detail.html”) from the last tutorial, so that the template contains an HTML <form> element: polls/templates/polls/detail.html A quick rundown: Now, let’s create a Django view that handles the submitted data and does something with it. polls/urls.py url(r'^(? We also created a dummy implementation of the vote() function. polls/views.py This code includes a few things we haven’t covered yet in this tutorial: request.POST is a dictionary-like object that lets you access submitted data by key name. As mentioned in Tutorial 3, request is a HttpRequest object. After somebody votes in a question, the vote() view redirects to the results page for the question. This is almost exactly the same as the detail() view from Tutorial 3. Now, create a polls/results.html template: Amend views
clemesha/hotdot - GitHub Откуда идут «функциональные» корни Python / Python Я никогда не полагал, что Python попадет под влияние функциональных языков, независимо от того что люди говорят или думают. Я знаком с императивными языками, такими как C и Algol68 и хотя я сделал функции объектами «первого класса», я не рассматривал Python как язык функционального программирования. Однако, было ясно, что пользователи хотят больше от списков и функций. Операции над списками применялись к каждому элементу списка и создавали новый список. def square(x): return x*x vals = [1, 2, 3, 4] newvals = [] for v in vals: newvals.append(square(v)) На функциональных языках, таких как Lisp и Scheme, операции, такие как эта были разработаны как встроенные функции языка. def map(f, s): result = [] for x in s: result.append(f(x)) return result def square(x): return x*x vals = [1, 2, 3, 4] newvals = map(square,vals) Тонкость вышеупомянутого кода в том, что многим людям не нравился факт, что функция применяется к элементам списка как набор отдельных функций.