Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Sending emails
If you've noticed, on many sites, once you sign up, it sends you an automated confirmation email. This is another function your site should be able to perform. So today let's take a look at that.Let's assume you have a Gmail account (if you don't, create one today, it's great!). We'll be using Gmail's email host (wow that rhymed).Go to the settings.py file and add the following code anywhere in between.EMAIL_USE_TLS = TrueEMAIL_HOST = "smtp.gmail.com"EMAIL_HOST_USER = "your_name@example.com"EMAIL_HOST_PASSWORD = "your_password"EMAIL_PORT = 587The port number 587 is pretty standard. Also, it is very important to set TLS to True. We'll go into why later.You have to enter your Gmail password there, but I assure you it is secure. Only the programmer(s) can see it. You may also create another common account and use those credentials instead.Ok, now we'll need to write the Python code to send emails.Open the views.py file in the appropriate app and write the following code:At the top, import the Django's send_mail:from django.core.mail import send_mailAlso import settings.py:from django.conf import settingsLater, create this function:def foo (request): title = "some title" message = "some message" host = settings.EMAIL_HOST_USER receiver = ["someone@example.com"] send_mail (title, message, host, receiver, fail_silently = … -
Github
Hey guys! For those of you who don't know, Github is a code sharing site on which you can review and edit other people's code and post your own. I have shown you a lot of code on this blog. Some of the code like file upload, login sessions etc is the same and can be reused. So I'm going to post such pieces of code on Github for you guys to review and edit. Please do so, I would be very happy if someone improved upon the code I've written.Also, my favorite language is Python and I do a lot of other things like software development, hacking in it. I've posted repos for these also. Please feel free to review and edit those too.Here's the link:https://github.com/AgentK1729Cheers! -
Intermittent renewal demonstrated through github graph
I've got a number of books at home in the "personal development" genre. One of the more practical and better ones is The Power of Full Engagement: Managing Energy, Not Time, Is the Key to High Performance and Personal Renewal. Core idea 1 A core idea in this book is that you've got four kinds of energy that you need to watch: Physical, health. Emotional, happiness. Mental, focus. Spiritual, purpose. As an exercise, rate yourself on a scale of 1 (bad) to 10 (good) and multiply the numbers. Should be a pretty good measurement of how well you're doing! The wake-up call is that you shouldn't disregard any of the four. If you're physically drained because of 4 hours of sleep and a sedentary lifestyle, you can be focused all you want, but you won't get too much out of yourself. And when emotionally drained because of some family mishap or huge stress, sleeping 8 hours a night won't help all that much. So... try to get all of them to a reasonable level. Core idea 2 The second core idea is that each of these four is trainable. Just like you can train your body, you can train the … -
Making a specific Django app faster
This is just a simple tale of making a specific Django app faster. Just a tale of the steps I took to give you an idea of what you can do to make your app faster. This isn't the definitive guide, it just aims at giving ideas and giving a feel for the process. tl;dr summary: First measure, then optimize. Django's caching helps. There's a nice algorithm to speed up javascript graphs. Background The app is lizard-fewsjdbc. It is on github, though we're basically the only ones using it. The basic setup is that we've got an external special-kind-of-database that talks xml-rpc to the outside world. Over the xml-rpc we can send SQL queries to the database that in turn uses a jdbc connection to execute the query. Don't ask. So... we talk to some xml-rpc and have to return information, mostly as graphs. The big performance problem is that the data is time-based. A measurement every minute. And the customers wanted to see a year of data in a graph. The graph is a javascript flot chart because of it provides nice interaction like zooming in and out. And... javascript, or rather Flot, or rather the browser, doesn't take … -
Writing a README
Good documentation is important. I just heard two colleagues talking and what one of them said sounded like a README: - Install the shit. - Run it. - Look for error messages. It triggered me to write a blog post as I was just busy writing a README for a small script. pip install is currently compiling numpy and matplotlib, so time enough for me to gather my thoughts. "Good documentation is important", yes. But there are many things that we can all agree on "yeah, that's important, too". "Being important" isn't the same as "actually getting written". Many of the small tools and django apps that we write aren't actually going to see widespread use. Many are company-internal, effectively. Is read-the-docs level documentation useful/necessary? In some cases, yes. One of the company-internal django apps I wrote has extensive documentation and 100% test coverage. That's what you get with something named "lizard-security" which uses django's row-level security and some special settings and other tricks. Especially when it is used in almost all our sites. In most cases, nobody is going to bother looking up the official documentation, even if you wrote it. All they'll glance at is the README. And … -
Profiling Django apps and Python scripts with cProfile and Kcachegrind
Some time ago I wrote about profiling Django with hotshot with mod_python that is now dead. To profile execution time of Python scripts as well as Django views, tests, or management commands we use cProfile and its wrappers. There are also converters that provide data for KCachegrind. Let us see what we have available to use. -
Uploading files on Google Cloud Storage with Django
Intro On of the features of Django Appengine Toolkit is simplifying the configurations neede to make Google Cloud Storage a static files storage for Django applications running on Google App Engine. Infact all you have to do to use GCS as the destination of your uploads is writing something like this in your settings.py module: APPENGINE_TOOLKIT = { 'APP_YAML': os.path.join(BASE_DIR, 'app.yaml'), 'BUCKET_NAME': 'media-uploads', } DEFAULT_FILE_STORAGE = 'appengine_toolkit.storage.GoogleCloudStorage' STATICFILE_STORAGE = 'appengine_toolkit.storage.GoogleCloudStorage' A complete example This repo contains a minimalistic Django project implementing a file storage application that permits users to upload files, listing and delete them. The project has just one app implementing all the logic, defining the model and exposing the views. For detailed instructions on how to setup a Django project on App Engine with django-appengine-toolkit please check out this blog post before playing with this project. Now let’s take a look at the code. The Model class Document(models.Model): docfile = models.FileField(upload_to='documents/%Y/%m/%d') def delete(self, *args, **kwargs): storage, path = self.docfile.storage, self.docfile.path super(Document, self).delete(*args, **kwargs) storage.delete(path) Pretty easy, we have just one field containing the file. Notice the delete method we’re going to use so that once deleted an instance, the same will happen to corresponding file on Cloud Storage. … -
Django Extensions 1.3.4
We are happy to release: Django-Extensions Version 1.3.4 This brings the usual tons of fixes and improvements Get it at: https://pypi.python.org/pypi/django-extensions/1.3.4 Changelog: Feature: Start maintaining a CHANGELOG file in the repository Feature: ActivatorModelManager now has an ActivatorQuerySet Feature: Add a deconstruct() method for future Django 1.7 migration compatibility Feature: show_urls, now support --language for i18n_patterns Feature: show_urls, now shows the decoraters set on a view function Feature: graph_models, now support --include-models to restrict the graph to specified models Feature: print_settings, allow to specify the settings you want to see Improvement: graph_models, use '//' instead of '#' as comment character in dot files Improvement: graph_models, added error message for abstract models without explicit relations Improvement: JSONField, use python's buildin json support by default with fallback on django.utils.simplejson Improvement: PostgreSQLUUIDField, parse value into UUID type before sending it to the database Improvement: Use django.JQuery in autocomplete.js if available Improvement: use "a not in b" instead of "not a in b" in the entire codebase Removed: clean_pyc command since it does not work correctly in many cases Removed: sync_media_s3 command in favor of sync_s3 BugFix: syncdata, use pk instead of id for identifying primary key of objects BugFix: sync_s3, use safer content type … -
Continuous integration with travis and coveralls.io for Django apps
Maintaining a solid rapport with the ongoing software development process always turns out to be a walk on air. Ensuring a software build integrity and quality in every single commit makes it much more exciting. If the current software bulid is constantly available for testing, demo or release isn't it a developer's paradise on earth? Giving a cold shoulder to "Integration hell" the 'Continuous integration' process stands out to deliver all the above assets. What's Continuous integration? Continuous integration is a development practice that requires a developer to integrate code into a shared repository in which isolated changes are immediately tested and reported on when they are added to a larger code base. Continuous integration (CI) originated from within the extreme programming paradigm, but the principles can be applied to any iterative programming model, such as agile programming. As critics noted several potential drawbacks on extreme programming many organizations have adopted CI without all of the extreme programming concepts. Principles of CI: Maintain a single source repository Automate the build Make the build self-testing Keep the build fast Everyone can see the results of the latest build Automate deployment Keywords: Build: All steps necessary to compile and create deliverables. Commit: … -
Absolute last chance for tickets!
We have been able to make a few more tickets available for DjangoCon Europe 2014. This last batch of sales must close at the very latest 1700 CET today, 29th April, and may close earlier. If you would like a ticket and don't have one, get it now. Please don't be disappointed! You can tell us about accommodation preferences, but please note that there are no more single rooms available. -
Working with JSON and Django
In this day and age of web development we work with json, a lot, especially with using web services. Unfortunately, sometimes you can forget what does what, when, and where when dealing with json. I know I have to look up the same thing over and over at times. This blog post is everything you need to deal with json "day-to-day" with Django. JSON to Dict One of the first things we, usually, do is convert json data that we get back from a web service call, or from a webhook, into a python datastructure. import json json_data = '{"hello": "world", "foo": "bar"}' data = json.loads(json_data) The key element from this is the json.loads which loads a string of json and converts the data to a python dict. From there you get to use it like normal. Dict to JSON import json data = {'baz': 'goo', 'foo': 'bar'} json_data = json.dumps(data) Normally you want to get your data into json from your dict, or model, which is just as easy using json.dumps. It converts your dict to a string of json so you can return it to in a response. HttpResponse with JSON There are a couple of key parts … -
Writing styled forms with absolutely minimal HTML (Part-1)
When controlling the form rendering, we all know that we can use the three pre-baked output format of the forms namely: as_u, as_p and as_table. For example: if we use: {{ registration_form.as_ul}} The output template would be: <li> <label for="id_uname">Userame:</label> <input type="text" name="username" id="id_uname"> <li> <li> <label for="id_email">Email:</lable> <input type="text" name="email" id="id_email"> <li> And so on... Here are some questions that come into the picture: What if we to want deal with divs? What if we want to reorder the fields? and what if I have 100 such fields? And what if I want to deal with model forms? How about field.errors and form.non_field_errors? To answer all those significant questions Django-crispy-forms steps into the arena. What are Django-crispy-forms? It's an app that brings up the best way to get the DRY Django forms, providing the tag and filter that lets us quickly render forms in a div format. This also provides an enormous amount of capability to configure and control the rendered HTML. As an added asset Django-crispy-forms support several frontend frameworks like Twitter Bootstrap(versions- 2 and 3), Uniform and Foundation. Using Django-crispy-forms is very simple and effortless. Let's dive in to learn to use the Django-crispy-forms with Twitter Bootstrap … -
Django backward relationship lookup
I often limit the lookup to fields of the model and forget about backward relations. Consider the following relationship: class Group(models.Model): name = models.CharField(max_length=100) class Student(models.Model): name = models.CharField(max_length=100) group = models.ForeignKey(Group) A group can have many students. We want to get the groups based on certain conditions on model Student. An example is getting the groups which contain a student named 'stud1'. If you can get it using model Group and without using Student, you can skip this post. In such a scenario, we tend to use Student model. It's more intuitive because the Foreign Key relationship exists from Student to Group. Let's load the following data to test our queries: group1 = Group.objects.create(name='Group 1') group1 = Group.objects.create(name='Group 2') Student.objects.create(name='stud1', group=group1) Student.objects.create(name='stud2', group=group1) Student.objects.create(name='stud3', group=group1) Student.objects.create(name='stud1', group=group2) Get the groups which contain student named stud1. Intuitive way: We first try to get all the students which satisfy the criteria. Query for that would be: Student.objects.filter(name='stud1') And then we try to get the groups of those students. If we use the Student model, we can't get a queryset of Group. So our approach would be to first get the ids of desired groups and then get a queryset of Group … -
Writing styled forms with absolutely minimal HTML (Part-2)
In the earlier post we have cruised through the fundamentals of using crispy forms for writing styled forms with minimal HTML. At this time, let us dip into getting a clear understanding on how to customize the crispy forms. Before diving into the crux of this post we will become aware with the following key words: FormHelper Layout FormHelper Django-crispy-forms implements a class called FormHelper to define the form rendering behavior. Helpers give us a way to control the form attributes and its layout. They control global form rendering behavior. Layout It is an another powerful class of Django-crispy-forms which allow us to change the way the form fields are rendered. This allows us to set the order of the fields, wrap them in divs or other structure, etc. Custom output Flexible and highly reususable In the first place we will get to know how to render a model form and then we will be customizing the divs and fields in the template. Handling ModelForm with a Django-crispy-form. As we know ModelForms render Model fields as HTML, let us do this using the crispy-forms now. Now lets render an Address form containing the fields like name, street, landmark and so … -
Webhooks, Django, and ngrok
Working with webhooks can be a pain. In this video learn more of what they are and how to easily work with them in your local environment using ngrok.Watch Now... -
Re: What Can Men Do?
TL;DR: Ignore Jeff; read Shanley. Jeff Atwood: If you see any behavior that gives you pause, behavior that makes you wonder “is that OK?” […] speak up. Honestly, as one man to another. OK, you got it. Jeff Atwood’s blog post today asks an important question: “what can men do?” (I’m not linking to Jeff’s post directly; it doesn’t need the signal boosting.) But it fails to answer that question effectively, reinforces several myths about gender in tech, and, worst of all, structurally reinforces male dominance rather than breaking it down. -
Two Scoops of Django 1.6 India Edition Available!
Newsletter #2 The India Edition of Two Scoops of Django 1.6 is now available at the following outlets: Within the next few weeks we hope to add Infibeam to the list of supported Indian retailers. Personal Note From Audrey The India Edition has special significance to us because my father is from India, and some of my family is still there. On my father's side, my grandfather was a civil engineer, and my grandmother was a mathematician. My grandmother passed away when I was very young, and my grandfather passed away a few years ago. I wish that they could have been around to see this. I grew up visiting my family in West Bengal often during the holidays. Information About the India Edition The India Edition, available internationally, is described in detail on our FAQ. -
Two Scoops of Django 1.6 India Edition Available!
Newsletter #2 The India Edition of Two Scoops of Django 1.6 is now available at the following outlets: Within the next few weeks we hope to add Infibeam to the list of supported Indian retailers. Personal Note From Audrey The India Edition has special significance to us because my father is from India, and some of my family is still there. On my father's side, my grandfather was a civil engineer, and my grandmother was a mathematician. My grandmother passed away when I was very young, and my grandfather passed away a few years ago. I wish that they could have been around to see this. I grew up visiting my family in West Bengal often during the holidays. Information About the India Edition The India Edition, available internationally, is described in detail on our FAQ. -
Two Scoops of Django 1.6 India Edition Available!
Newsletter #2 The India Edition of Two Scoops of Django 1.6 is now available at the following outlets: Within the next few weeks we hope to add Infibeam to the list of supported Indian retailers. Personal Note From Audrey The India Edition has special significance to us because my father is from India, and some of my family is still there. On my father's side, my grandfather was a civil engineer, and my grandmother was a mathematician. My grandmother passed away when I was very young, and my grandfather passed away a few years ago. I wish that they could have been around to see this. I grew up visiting my family in West Bengal often during the holidays. Information About the India Edition The India Edition, available internationally, is described in detail on our FAQ. -
DjangoCon Europe accommodation preferences
Our Django Island hotel offers accommodation in various different kinds of rooms (single, twin or double) in various different kinds of apartments (of one, two or three rooms). We need to know your preferences - tell us! Rooms The standard room is a twin (two single beds), which will be shared with another Django Islander of the same sex. If you would prefer to share with a particular person, you can let us know. There are 60 single rooms available. If you need - you don't need to tell us why - or strongly prefer a single room, just say so. If you are travelling with a partner (i.e. you have purchased a "companion" ticket) or you and your partner are both conference attendees, you can ask for a double room. Each room has its own private bathroom. Apartments You can also let us know whether you prefer to be in an apartment with only one room, or amongst others, and with whom. If you do have a preference about apartments, please tell us whether: you'd like to be in an apartment on your own you'd like to share the apartment with friends (tell us whom) you'd prefer to be … -
Assisted places for students
Thanks to the generosity of our sponsors and the attendees who bought a "Contribution" ticket we've been able to support a number of our speakers who were on tight budgets, and who would otherwise have been unable to attend. We are delighted to announce that the same generosity means that we're able to make an additional 7 conference places available to students at a discount of 270 € - that is, for 400 € each. The 400 € covers the conference, accommodation (Tuesday and Wednesday night) and all meals. These tickets are only available, on a first-come first-served basis, to students in full-time education. We'll require some proof of status. We're truly grateful to the community for providing the support that has made this possible. Student tickets -
Tutorials!
This year at DjangoCon Europe, as well as our packed programme of talks, we are running tutorials. They are all free, and all running on Friday, the first day of the sprints. One of them, Daniele Procida's Don't be afraid to commit, is even an introduction to contributing to projects such as Django, and will lead you seamlessly into the sprints! Harry Percival's Test-driven web development with Python and Django is frankly worth the price of admission to the entire conference. Patrick Lauber's Get started with django CMS will give you a big head-start with one of Django's most successful and well-known products, as a user and developer. The three tutorials we are offering represent our effort to use DjangoCon Europe to kickstart even more activity in the community. We know that people want to take part, and to contribute, but that it's not always so easy to get started. Our tutorials are for those people. They are three first-class reasons to stay on after the talks for the sprints, and will equip you with valuable skills. In short: don't think that the sprint days of DjangoCon are just for those 'other' people, the ones who can run while you're … -
教你开始写DJANGO1.6的第1个APP-4
回顾一下在第3部分中我们实现了投票应用的基本框架结构,但是并没有实现真正的投票表单和处理,以及结果显示等功能, […] -
Minimal Nginx and Gunicorn configuration for Django projects
We will see how to deploy a Django project with nginx and gunicorn. It was easier for me to understand nginx and gunicorn on development machine and then move to a publicly accessible server. So, we will cover this post in a similar sequence: On development machine Use django's runserver to serve the site. Stop runserver and start using gunicorn instead. Add nginx to this configuration. On server Deploy the project on a publicly accessible server using same stack. Use runserver We will use a minimal project with two apps and few static files. You can view this project at github. Project structure: |-- manage.py |-- other_app | |-- __init__.py | |-- models.py | |-- static | | `-- other_app | | `-- styles.css | |-- tests.py | |-- urls.py | `-- views.py |-- project_static | `-- base.css |-- requirements.txt |-- some_app | |-- __init__.py | |-- models.py | |-- static | | `-- some_app | | `-- styles.css | |-- tests.py | |-- urls.py | `-- views.py |-- templates | |-- other_app | | `-- home.html | `-- some_app | `-- home.html |-- test.db `-- test_project |-- __init__.py |-- settings.py |-- test_settings.py |-- urls.py `-- wsgi.py This project was … -
Django 1.6 最佳实践: 最佳Django开发环境设置
在本节中, 我们将介绍最佳的本地开发环境设置 1. 在开发和部署服务器中使用相同的数据库 可能有些开发者喜欢在本地开发Django程序时, 使用SqLite3数据库, 而在部署时使用PostgreSQL数据库(或任何不同于SqLite3的数据库). 但这一做法应当避免, 因为我们是希望不同的数据库表现的一致, 这显然是不现实的. 以下是一些我们使用不同数据库进行开发和部署时遇到的问题: Fixtures不是解决方法 或许你会想, 我们为何不能用fixtures来弥补不同数据库的不同呢? 是的, fixtures能用于创建简单的手写的测试数据, 它能在开发时填充进许多假的数据, 尤其是在开发初期阶段. 但是, fixtures对于从一个数据库到另一个未知数据库的大数据迁移而言, 并不是一个可靠地工具. 它本身也不是为了这一目的而设计的, 我们不应该将创建基本数据和数据迁移的用法混淆. 无法本地测试部署后数据 当你的本地数据库和部署数据库不同时, 你无法创建一个和部署服务器完全一样的拷贝进行测试. 当然你可能会觉得使用dump将数据完全拷贝出来放到本地数据库中测试, 但是这也无法保证导出和导入后的数据是完全一样的. 不同数据库拥有不同的数据类型 请牢记, 不同的数据库处理数据的类型是不同的! Django的ORM试图统一这些不同, 但毕竟所能实现的效果是有限的. 比如, 在使用SqLite3作为测试数据库, 而使用PostgreSQL作为部署数据库时, 由于SqLite3是动态, 弱类型数据库, 而PostgreSQL则是强类型数据库, 虽然程序在测试服务器上运行正常, 但最终可能导致在部署服务器上出现问题. 2. 使用Pip和Virtualenv 如果你还没有使用Pip和Virtualenv, 那么, 我们强烈推荐你现在就开始熟悉并使用Pip和Virtualenv. Pip和Virtuaenv是Django项目的标准配置, 并且大多数开发者依赖这两个工具. Pip是从Python Package Index下载Python模块的工具, 更是管理和安装Python模块的不二选择. 它有点像easy_install, 但具有更多的功能, 其中最重要的就是对于Virtualenv的支持 Virtualenv是创建独立的Python开发环境, 管理模块依赖性的工具. 当我们同时开发多个项目时, 且这些项目使用的库或模块都有所不同时, 它可以帮助我们轻松的在这些独立开发环境质检切换. 当然为了方便使用Virtualenv, 我们可以借助virtualenvwrapper. virtualenvwrapper为我们提供了诸多命令, 方便我们使用Virtualenv 3. 使用Pip安装Django和其他依赖库 Django官网提供了多种安装Django的方式, 但我们推荐使用pip来安装Django和其他项目依赖模块. 简单而言, 每个Django项目都需要一个requirements文本文件, 其中罗列了所有本项目中使用到的Python模块, 每条信息包括了模块的名字和版本号. 我们就使用pip将这些requirements安装到项目的virtualenv中, 具体的说明我们会在将来谈到requirements时做一个详细的介绍. 4. 使用版本控制系统 版本控制系统又叫源代码管理系通, 在进行任何语言的编程时, 你都应当使用版本控制系统来管理跟踪你的源代码. 如果你不了解的话, 可以搜索一下, 应该能找到各版本控制系统的详细比较. 在所有的版本管理系统中, 我们推荐使用git. 因为第一, git似乎是现在最为流行的版本控制系统, 第二它可以方便的建立和合并分支. 在使用版本控制软件的同时, 我们推荐使用GitHub或Bitbucket作为在线的源代码仓库, 用于备份源代码仓库. 5. 试着使用Vagrant Vagrant是一个用于创建, 设置和管理开发环境的工具. Vagrant借助VirtualBox等虚拟化工具, 可以轻松的创建我们需要的虚拟机开发环境. 例如, 我们的开发环境是OS X, 而部署环境是Ubuntu时, 我们就可以使用Vagrant创建一个Ubuntu虚拟机, 并在其中配置好开发环境. 详细的介绍, 我们会在将来讲到开发环境时再谈.