Django | Mitch Fournier

Tag: django

How To Import a CSV (or TSV) file into a Django Model

How to import a CSV file into Django

Recently I downloaded a US zip code data file in comma separated (CSV) format. Below are the steps and python script that I used to import that data into a django model for my site Wantbox.com.

The Steps to Import CSV Data into Django:

  1. Create your django model (mine is called “ZipCode”, see below).
  2. Create a python script called “load_data.py” with your correct django project and directory info (see mine below).
  3. Put the CSV file and “load_data.py” script in the same directory.
  4. From that directory run: python ./load_data.py”

That’s it. For a CSV file with about 42,000 rows this import took about 15 seconds. You can verify that the data loaded correctly by checking your django admin.

Continue reading »

Debugging Nginx and Django: Viewing HTTP headers in Google Chrome

It seems like every time I setup a new Django project, I spend an inordinate amount of time getting the site and admin media (IMG, CSS, JS) to display correctly in my templates.

It doesn’t help that I haven’t standardized on a best-practices Django layout. Dreamhost shared servers require one site structure while the Linode StackScript I use (thanks Filip) suggests another. A move from Django 1.2 to 1.3 hasn’t helped either.

On my home development environment (Debian in VirtualBox on Windows), I use Nginx to serve static media for my Django development server. Although this improves my dev server performance, it complicates the setup of a new environment.

Continue reading »

Getting Started with AJAX in Django: a Simple jQuery Approach

AJAX in Django using jQuery

AJAX in Django using jQuery

I recently created a side project to explore a few tech areas I wanted to learn more thoroughly. One of those areas was how to implement AJAX in Django using jQuery.

In a nutshell, the site, TruthTruthLie.me presents three facts about you and challenges your friends to click on the one that is a lie.

When your friend clicks on a fact, I send the clicked fact_id via AJAX to a url. A Django url pattern routes the click to a view where I check what “type” the fact is, return the result via JSON to the client and update the page dynamically without a page refresh.

Continue reading »

Solving “Caught TypeError while rendering: coercing to Unicode: need string or buffer”

I’ve been working on a little side project in Django 1.3 and have run into a couple instances in the Django admin where I would get the error:

"Caught TypeError while rendering: coercing to Unicode: need string or buffer"

I did a little Bing-ing (my new default search engine — Google is too big, powerful and, dare I say, evil) and found the following solution from Ryan Brady.

For a Bing search of “django admin ‘Caught TypeError while rendering: coercing to Unicode: need string or buffer‘” there were only 4 results, but lucky for me, Ryan’s solved my problem.

Continue reading »

7 SEO Tips for your Django Site

"It is legal because I wish it." - Louis XIV, France

Forget content. When it comes to the Internet, Google is king. If you please the king, you can do well.

If you cross the king, your disloyal ass will be thrown into the tower to watch your site wither and die, unless the king deigns to sets you free.

Some of the things that most please the king are “thick” (i.e. not “thin”) content pages, well structured site navigation, and honest links to and from your site.

In building my new site, Wantbox.com, I’ve both pleased and inadvertently crossed the king. The problem with this monarch: he doesn’t speak to his subjects, so if you end up in the tower, you’re never quite sure why.

Your best strategy is to know the rules of the kingdom, and do your best to follow them. Below are some of the best practice, white-hat SEO techniques that I have used to make sure the king stays happy.

Continue reading »

Basic Python Logging in Django 1.2: Debugging Made Simple

When I was deploying my new social shopping site Wantbox a couple of month ago, I discovered that I couldn’t use the Django print command in my live Dreamhost (Passenger WSGI) environment like this:

   print "Hello world!"

As a new site, I was still debugging a few persistent nits and really needed to print out messages when certain areas of the code were accessed. A few sites recommended that I try the Django debug toolbar, but I was having trouble getting that to work reliably in my Dreamhost setup.

I searched around a bit more and after consuming many web pages, finally discovered a dead simple solution using the built-in Python logging module.

Continue reading »

Redirect to the originally requested page after django registration and email activation

For my newly-launched, django-based social shopping site Wantbox.com, I have implemented @ubernostrum‘s ubiquitous django-registration 0.7 to handle user registrations.

Recently I completed a Wantbox feature that required me to redirect a new user back to their initially requested, authorization-required page after their registration and email activation. Below are the step-by-steps actions I took to accomplish this.

Continue reading »

Configure the output of the Django development server

I recently opened up my home dev server for a closed, friends and family alpha launch of my new commerce/community site Wantbox.com. Now that people besides me are hitting the server, I wanted to see client IP addresses in the dev server output.

Current output:

[19/Sep/2010 19:23:39] "GET / HTTP/1.1" 200 2555
[19/Sep/2010 19:23:39] "GET /static/media/css/site.css HTTP/1.1" 200 14311
[19/Sep/2010 19:23:40] "GET /static/media/js/site.js HTTP/1.1" 200 620
[19/Sep/2010 19:23:40] "GET /static/media/img/logo-home.gif HTTP/1.1" 200 4963

Desired output:

[19/Sep/2010 19:23:39] 166.137.139.17 "GET / HTTP/1.1" 200 2555
[19/Sep/2010 19:23:39] 166.137.139.17 "GET /static/media/css/site.css HTTP/1.1" 200 14311
[19/Sep/2010 19:23:40] 166.137.139.17 "GET /static/media/js/site.js HTTP/1.1" 200 620
[19/Sep/2010 19:23:40] 166.137.139.17 "GET /static/media/img/logo-home.gif HTTP/1.1" 200 4963

I wasn’t able to track down any built-in configuration options to runserver, but thanks to the sage advice of “Python/Django evangelist” Peter (most recently, the creator of The Prince of Pinot) I got this workaround (applies to Django 1.2.1):

  1. open django/core/servers/basehttp.py
  2. change line 614
    • from:  msg = "[%s] %s\n" % (self.log_date_time_string(), format % args)
    • to:  msg = "[%s] %s %s\n" % (self.log_date_time_string(), self.client_address[0], format % args

I’m not crazy about messing with the core Django code, but I found no other way to get the output I wanted short of running a “real” webserver like Apache, which I didn’t want to do in my closed, low traffic dev environment. By all means, if you have a better solution, let me know about it in the comments.

Also, if you happen to live in the greater Boston metro area and would like to help a local Django developer improve a new site, ping me on twitter (@mfournier). I’d love your feedback.

OAuthcalypse Now: Tweeting with Twitter, OAuth and Django

The Twitter OAuthcalypse hit me last week. Back in July, I used Twitter’s basic authentication scheme in a new site I’m working on (wantbox.com) to easily send out tweets via my @wantbox account whenever someone posted a new “want” to the site.

This integration took about 30 minutes to research and implement. All I had to do was put my twitter username and password in my Django project’s settings.py file, pip install the python-twitter app into my virtualenv and then use:

from django.conf import settings
import twitter
api = twitter.Api(username=settings.TWITTER_USERNAME, password=settings.TWITTER_PASSWORD)
api.PostUpdate('Someone in 02481 wants "a home gym" %s' % extra_stuff)

The resulting tweet would appear in my @wantbox stream appearing to come “via API”. Easy-peasy.

Continue reading »

Customizing Django Comments: Remove Unwanted Fields

I recently added comments to a new Django site that I’m working on (Wantbox.com = craigslist[::-1] + stack overflow). Comments pose an interesting problem as they can have a number of “parents”. In my case, the parent might be a user’s “Want” a respondent’s “Have” or possibly another “Comment.”

In the process of researching the best way to architect Wantbox’s comments app, I read about “polymorphic associations“, “exclusive arcs” and Django’s ContentType framework. Using this knowledge, I contemplated recreating the comment wheel, since I wanted my comment form to just be a simple “Stack Overflow-type” comment-only field and not the larger “WordPress-type” name/email/website/comment.

As I explored Django’s comments framework deeper, I realized that recreating another comment app was a waste of my time and my end product would be far less feature rich than Django’s bundled commenting system. Below are my modifications which allowed me to quickly and easily twist Django comments into what I needed.

My Django Comment Modifications:

To customize the default comment form and comment list display, I created a “comments” directory in my root “templates” directory and simply overrode the two default comment templates “form.html” and “list.html”.

My custom “/templates/comments/form.html”:

{% load comments i18n %}
{% if user.is_authenticated %}
   <form action="{% comment_form_target %}" method="post">
        {% csrf_token %}
        {% if next %}<input name="next" type="hidden" value="{{ next }}" />{% endif %}
        {% for field in form %}
            {% if field.is_hidden %}
                {{ field }}
            {% else %}
                {% if field.name != "name" and field.name != "email" and field.name != "url" %}
                    {% if field.errors %}{{ field.errors }}{% endif %}
                    {{ field }}
                {% endif %}
            {% endif %}
        {% endfor %}
        <input class="submit-post" name="post" type="submit" value="{% trans " />
   </form>
{% else %}
    I'm sorry, but you must be <a href="javascript:alert('send to login page')">logged in</a> to submit comments.
{% endif %}

Which is only slightly different from the default Django comments form.html, primarily suppressing the display of the not-wanted and not-required “name”, “email” and “url” input fields.

My custom “/templates/comments/list.html”:

<div class="comment_start"></div>
{% for comment in comment_list %}
   <div class="comment">
      {{ comment.comment }}
      (from <a href="javascript:alert('show user profile/stats')">{{ comment.user }}</a> - {{ comment.submit_date|timesince }} ago)
   </div>
{% endfor %}

In the template where I want to invoke the comments form, I first call {% load comments %} and then {% render_comment_form for [object] %} to show the form, or {% render_comment_list for [object] %} to generate a list of the comments on the object (replace [object] with your appropriate object name). So easy.

This solution is working great for me, and still giving me all the other “free” stuff that comes with django comments (moderation, flagging, feeds, polymorphic associations, helpful template tags, etc…). The moral of this story: don’t recreate the wheel when an hour or two of research can give it to you for free.

NOTE: This blog post is based on my Stack Overflow answer to Ignacio’s question “How to extend the comments framework (django) by removing unnecesary fields?

Web Analytics