background preloader

Python

Facebook Twitter

Django « Marteinn / Blog. IntegrityError at /***/ (1048, "Column '***' cannot be null") in python django. Part 1: The Basics — South 0.7.6 documentation. Welcome to the South tutorial; here, we’ll try and cover all the basic usage of South, as well as giving you some general hints about what else to do.

Part 1: The Basics — South 0.7.6 documentation

If you’ve never heard of the idea of a migrations library, then please read What are migrations? First; that will help you get a better understanding of what both South (and others, such as django-evolution) are trying to achieve. This tutorial assumes you have South installed correctly; if not, see the installation instructions. Starting off In this tutorial, we’ll follow the process of using migrations on a brand new app. The first thing to note is that South is per-application; migrations are stored along with the app’s code . Welcome. Managing static files. Websites generally need to serve additional files such as images, JavaScript, or CSS.

Managing static files

In Django, we refer to these files as “static files”. Django provides django.contrib.staticfiles to help you manage them. This page describes how you can serve these static files. Configuring static files¶ Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS.In your settings file, define STATIC_URL, for example: In your templates, either hardcode the url like /static/my_app/myexample.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files).{% load static %}<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>Store your static files in a folder called static in your app.

Your project will probably also have static assets that aren’t tied to a particular app. Note Testing¶ Writing a website in Python. 5. Object Oriented Programming — Python Practice Book. 4.1.

5. Object Oriented Programming — Python Practice Book

State Suppose we want to model a bank account with support for deposit and withdraw operations. One way to do that is by using global state as shown in the following example. balance = 0 def deposit(amount): global balance balance += amount return balance def withdraw(amount): global balance balance -= amount return balance The above example is good enough only if we want to have just a single account. We can solve the problem by making the state local, probably by using a dictionary to store the state. Python en:Object Oriented Programming - Notes. Introduction In all the programs we wrote till now, we have designed our program around functions i.e. blocks of statements which manipulate data. This is called the way of programming.

There is another way of organizing your program which is to combine data and functionality and wrap it inside something called an object. This is called the programming paradigm. Most of the time you can use procedural programming, but when writing large programs or have a problem that is better suited to this method, you can use object oriented programming techniques. Classes and objects are the two main aspects of object oriented programming.

Note for Static Language Programmers. Why PHP Is Fun and Easy But Python Is Marriage Material. Pylons Project : Home. Django - Detect mobile browser (not just iPhone) in python view. Django Tutorial 7 - User Authentication Part 2 - Hacked Existence. User authentication in Django. Django comes with a user authentication system.

User authentication in Django

It handles user accounts, groups, permissions and cookie-based user sessions. This section of the documentation explains how the default implementation works out of the box, as well as how to extend and customize it to suit your project’s needs. Overview The Django authentication system handles both authentication and authorization. Working with forms. In HTML, a form is a collection of elements inside <form>...

Working with forms

</form> that allow a visitor to do things like enter text, select options, manipulate objects or controls, and so on, and then send that information back to the server. Some of these form interface elements - text input or checkboxes - are fairly simple and built-in to HTML itself. Others are much more complex; an interface that pops up a date picker or allows you to move a slider or manipulate controls will typically use JavaScript and CSS as well as HTML form <input> elements to achieve these effects.

As an example, the login form for the Django admin contains several <input> elements: one of type="text" for the username, one of type="password" for the password, and one of type="submit" for the “Log in” button. User authentication in Django. Django comes with a user authentication system.

User authentication in Django

It handles user accounts, groups, permissions and cookie-based user sessions. This section of the documentation explains how the default implementation works out of the box, as well as how to extend and customize it to suit your project’s needs. Overview. - HACKED - Creating forms from models. Virtualenv 1.7.2. Introduction virtualenv is a tool to create isolated Python environments.

virtualenv 1.7.2

The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. Django Tutorial 6 - User Authentication Part 1 - Hacked Existence. Django. To get started with Django in PyDev, the pre-requisite is that Django is installed in the Python / Jython / IronPython interpreter you want to use (so, "import django" must properly work – if you're certain that Django is there and PyDev wasn't able to find it during the install process, you must go to the interpreter configuration and reconfigure your interpreter so that PyDev can detect the change you did after adding Django). If you don't have Django installed, follow the steps from Note that this tutorial won't teach you Django. It'll only show how the Django integration is available in PyDev, so, if you're not familiar with Django, it's useful to learn a bit about how it works and then use this help to know how the PyDev Django integration can help you.

The Django integration in PyDev works through 3 main configurations:

Windows

Templating. Templating, and in particular web templating is a way to represent data in different forms.

Templating

These forms often (but not always) intended to be readable, even attractive, to a human audience. Frequently, templating solutions involve a document (the template) and data. Template usually looks much like the final output, with placeholders instead of actual data (or example data in simplified form), bears common style and visual elements. Data which is presented using that template may be also separated in two parts - data required to be rendered, and data required for template itself (navigation elements if it is a site, button names if it is some UI). Combining template+data produces the final output which is usually (but not always) a web page of some kind. Templating Engines There are many, many different HTML/XML templating packages and modules for Python that provide different feature sets and syntaxes. Engines using Value Substitution. Update django database to reflect changes in existing models.

Django - How to use a python class with data objects in mysql. Model field reference. This document contains all the gory details about all the field options and field types Django’s got to offer.

Model field reference

Field options The following arguments are available to all field types. All are optional. null Field.null If True, Django will store empty values as NULL in the database. Avoid using null on string-based fields such as CharField and TextField because empty string values will always be stored as empty strings, not as NULL. Mechanical Girl : Working with the Django admin and legacy databases, pt.3. Part 1 | Part 2 After you've run inspectdb and done all your syncing and basic admin setup, take a peek at the models you generated - depending on the state of your legacy db, you've probably wound up with something that looks like this (column names have been changed to protect the innocent, although I'm not sure that the so-called architects who created these tables deserve anyone's protection): For the record, my traversal produced upwards of 80 model classes, all in similar states of disarray - I had a lot of cleanup to do just to get the admin to stop barfing every time I fired it up.

Mechanical Girl : Working with the Django admin and legacy databases, pt.3

Here's a sort of informal checklist of the things you should look over: Primary Keys First things first - wherever you have a model with a primary key named "id", just delete it, e.g class NewsContent(models.Model): id = models.IntegerField() title = models.TextField(blank=True) ... class NewsContent(models.Model): title = models.TextField(blank=True) ... Python - Django mysql error. PyDev. Peewee 0.9.9. Peewee is a simple and small ORM.

It has few (but expressive) concepts, making it easy to learn and intuitive to use. New to peewee? Here is a list of documents you might find most helpful when getting started: Quickstart guide – this guide covers all the essentials. It will take you between 5 and 10 minutes to go through it.Guide to the various query operators describes how to construct queries and combine expressions.Field types table lists the various field types peewee supports and the parameters they accept. For flask helpers, check out the flask_utils extension module. Examples. Set Up Python and Install Django on Mac OS X Lion 10.7. NOTE: This guide was written for Lion 10.7 and has since been superceded by these new guides: First steps Lion has done away with the “Sites” folder by default, but it’s easy to add it back — the custom icon will even show up automatically. Use the Finder, or enter the following in a Terminal session: Another change is the hidden ~/Library folder.

We can make it visible again with the following command (which gets overridden by OS updates and must be run again afterwards): How to setup Django and MySQL-python on Mac OS X Lion « Decoding the Web. January 23, 2012 by decoding. How to leave a python virtualenv. A Virtual Exit. Setting up Django with MySql – with and without MAMP. In this post, we will discuss the steps needed to get your django work with mysql database on Mac OS Lion – there are 2 ways you can use mysql. How do I connect to a MySQL Database in Python. Development Environment Setup Python 2.7.2 + MySQL in Mac OSX 10.7. Mechanical Girl : Installing Django with MySQL on Mac OS X. Django's official installation instructions are here: However, they get a little vague around the section titled "Get your database running".

I followed this post and, aside from a few missing notes, got the Django/MySQL install done in about 15 minutes: Mechanical Girl : All Entries.