Tag: Web Development

Django & Webpack

Table Of Contents

  1. Overview
  2. Environment Setup & Requirements
  3. Setup Webpack
  4. First Webpack Packaged Assets

Overview

I’ll assume that you already know Django and is already well versed with it and you just want to integrate Webpack into your project. If you already know what is Webpack and how it works please proceed to Environment Setup & Requirements. For those of you that is not quite sure what Webpack will bring into your Django project then please read on for brief introductory of it. Continue reading

Access Django Development Server Remotely

By default Django development server will only be accessible through the local machine(127.0.0.1). If you want to access the server remotely then you need to run the the development server with your IP as an argument:

python manage.py runserver [your-ip-here]:8000

Change the “[your-ip-here]” to your actual IP. If you IP address is 174.143.2.123 then you will need to run the server using this:

python manage.py runserver 174.143.2.123:8000

If you have multiple network configured or you want the server to be automatically available on what your IP now currently is. Then enter 0.0.0.0 as your IP, like this:

python manage.py runserver 0.0.0.0:8000

This will bind the development server of Django on all available network/IP address. Then you will the see same message when you use just use python manage.py runserver  upon successfully creating the server but instead of http://127.0.0.1:8000/ you will see a http://0.0.0.0:8000/, then you will now be able to access your server remotely.

Validating models...
 
0 errors found
February 01, 2014 - 10:13:18
Django version 1.5.1, using settings mysite.settings
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

Warning: This server is only meant for development use so you must not use this as a production solution. This is also discouraged by Django team.

Making Python Datetime Timezone Aware For Django

To make a Python Datetime timezone aware for Django Datetime field. You can use the utc from django.utils.timezone for replacing the tzinfo of Python Datetime but make sure that it is in UTC format. Below is a simple example of how to retrieve the current date and time and make it timezone aware for Django Datetime model field.

from django.db import models
from django.utils.timezone import utc
import datetime 
 
class Foo( models.Model ): 
 #datetime field sample 
 date_posted = models.DateTimeField( auto_now_add=True, blank=True ) 
 def save(self, *args, **kwargs): 
 #the datetime generated by datetime module will now be timezone aware 
 self.last_date_updated = datetime.datetime.utcnow().replace(tzinfo=utc) 
 super(Foo, self).save(*args, **kwargs)

Django 1.5 Upgrade From 1.4 Problem On DEBUG=False

Ive upgraded the Django version Im using to 1.5 from 1.4.5 several days earlier and since then I was unable to run my app in release mode. Whenever I set DEBUG=False in settings.py a “HTTP Error 500” page is always served but setting it to DEBUG=True will make the app run fine.

Ive found out that now on version 1.5, Django has a new setting in settings.py:

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []

This setting is not included in version 1.4 and is now needed in 1.5. You need to add your domain here ex: www.jamesbaltar.com. See this link for more

South Database Migration Tool

Update: South migration tool is now pulled into Django core as of Django 1.7. So if you are starting out on Django 1.7 then you don’t need to install South anymore because it has now it’s own implementation of a database migration tool.

For sometime now Ive been playing with Django. Making small applications that is not worth mentioning. When I first touched Django Ive read somewhere that it would help me greatly if I used South, a database migration tool but I just brushed it off too early at that time since I didnt want to learn something on top of Django. Now that Ive tried it Ive realized that Ive truly made a big mistake of not trying it earlier.

South is a tool for database migration. What it does is help you sync your database tables with your models. Youre really going to appreciate it when you have a live production setup and then you find yourself needing to change some tables, like add columns or change the default values. Imagine if you have made a lot of changes and when you need to put your project into the live setup youre going to write a bunch of SQL scripts to patch the live setup. With South you wont need to create your own SQL scripts to patch your tables.

Like all Django application you will need to add it in your settings.py. It also creates it own tables in your database. For now I dont really care what it adds to my project since it doesnt seem to get in my way. It is really a great app/tool to have specially for me since Im only learning Django and web development so I always ends up modifying my models too often.

You can get South from here. Installation and a tutorial is also in their website to help you get started quickly.

Make Your Django Project Movable

For every Django projects we have a settings.py that contains all the settings for a project but some of these must have an absolute path. So moving a Django project to another directory can be a pain. Luckily we have the os module. These module can and will help us make our projects easy to move.

Just add these line in your settings.py

import os
APP_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) + "/"

Now for every settings that needs an absolute path just prepend the path with APP_PATH

#for static root
STATIC_ROOT = APP_PATH+static/
#For templates
TEMPLATE_DIRS = (
 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
 # Always use forward slashes, even on Windows.
 # Dont forget to use absolute paths, not relative paths.
 APP_PATH+template,
)

© 2024 James Baltar

Theme by Anders NorenUp ↑