Author Archives: Etay Cohen-Solal

About Etay Cohen-Solal

Development Specialist, Artist and Activist
Personal Website

CodeLobster Handy Code Tools Review – Free portable IDE for PHP/HTML/CSS/JavaScript development

Overview

Excited today, I’m going to try a new IDE for Windows: CodeLobster PHP Edition 4.5.1.

With almost 30 years experience in development, from the age of Commodore & QBasic to today’s VS, Eclipse, NetBeans, Vi, etc….  I’ve used them all to create some code (even  NotePad). Sometimes, I am using more then one editor at once!

 

Continue reading

Django: Preserve table data when changing model (adding fields)

Overview

Django, out-of-the-box doesn’t knows how to add rows to your db when the model changed, You have 2 options:

  1. manually add the field to the db. (I won’t demonstrate this)
  2. export data > reset your db > import data.

Continue reading

Django URL dispatcher routing beginners tutorial

How to use Django URL dispatcher

Django routing module called URL dispatcher

url.py

Normally, When a user requests a page from your Django-powered site Django loads the URLconf  module from a file called urls.py at the root of your project, Ordinarily, this is the value of the ROOT_URLCONF setting, The URL dispatcher can be overridden when the incoming HttpRequest object has an attribute called urlconf (set by middleware request processing), its value will be used in place.

First line in your urls.py should be:

1
from django.conf.urls import patterns

 

urlpatterns

Django loads that file and looks for the variable urlpatterns. This should be a Python list, in the format returned by the function django.conf.urls.patterns().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from django.conf.urls.defaults import patterns

urlpatterns = patterns('',
    # ex: /
    (r'^$', 'myapp.views.home', {}, 'index'),
    # ex: /about
    (r'^about', 'myapp.views.about', {}, 'about'),
    ....
)

# To add more urlpatterns in the same urls.py you can use:
urlpatterns += patterns('',
    ....
)

URL Pattern Arguments

  1. First Argument is URL pattern.
  2. Second is the View to send back.
  3. Third is Dictionary to send to the view.
  4. Last argument is Name. Naming enables calling {% url name %} from templates.

Notes

  • Django runs through each line, in order, and stops at the first one that matches the request.
  • The ‘r’ in front of each regular expression string is optional but recommended. It tells Python that a string is “raw” – that nothing in the string should be escaped. See Dive Into Python’s explanation.

url() function

You can use the url( convenience wrapper.

The url() function is passed five arguments, two required: regex and view, and three optional: kwargsname, and prefix.

url(regex, view, kwargs=None, name=None, prefix=”)

You can use the url() function, instead of a tuple, as an argument topatterns(). This is convenient if you want to supply a name but not extra_context you’d still have to include an empty optional extra arguments dictionary in the tuple version

Remember you need to include the url, from django.conf.urls.defaults.

1
from django.conf.urls.defaults import patterns, url

These are the same:

1
2
(r'^$', 'views.home', {}, 'index'),
url(r'^$', views.index, name='index'),

 

Import views to shorten code

You can use either way, as you feel convenient for you. It’s the same code above, but using Import to shorten code:

Method 1: Import Views Class

1
2
3
4
5
6
7
from django.conf.urls.defaults import patterns
from myapp import views

urlpatterns = patterns('',
    (r'^$', 'views.home', {}, 'index'),
    .....
)

Method 2: Import Views (or *)

1
2
3
4
5
6
from django.conf.urls.defaults import patterns
from myapp.views import home

urlpatterns = patterns('',
    (r'^$', 'home', {}, 'index'),
    .....

Method 3: Use Default Pattern

1
2
3
4
urlpatterns = patterns('myapp.views',
    (r'^$', 'home', {}, 'index'),
    .....
)

 

Include

You can include from another file, Django app or object. remember to import the include() function.

1
2
3
4
5
6
from django.conf.urls.defaults import patterns, url, include
urlpatterns = patterns('',
    # ex: /help
    url(r'^help/', include('help.urls')),
    .....
)

This code will search for url.py file inside help app in your django project. construct the file the same as the url.py file.

 

Include in the same file

1
2
3
4
5
6
7
8
9
10
from django.conf.urls.defaults import patterns, url, include

extra_patterns = patterns('',
    .......
)

urlpatterns = patterns('',
    (r'^extra/', include(extra_patterns)),
    .....
)

 

 

These are the URL dispatcher basics. Maybe I’ll be post later another post on more advanced aspects of the URL dispatcher include URL grouping, passing extra parameters to view and more…

CYA,

And good luck with Django – It worth it!

How to disable java in all browsers at once

Overview

Several major companies have been hacked lately. Security advice for web users last week from the US Department of Homeland Security encouraging to disable java on browsers. Disable java in each browser takes time. You will learn how to disable java for all at once. and also for each browser if needed.

Continue reading

Twitter hacked. users adviced to change all passwords and disable java!

You’ve been hacked?

Quarter of a million users (250,000) details had been stolen from twitter this week including: usernames, email addresses, session tokens and encrypted/salted versions of passwords Twitter becomes the latest US media giant to admit to being hacked,

Continue reading

Filter your visits in Google Analytics (Do not track your IP)

Overview

Setting up Google Analytics to not track your visits is a bit tricky. You can filter by IP, but most of us are using dynamic IP so we need another solution. You’ll learn to setup a cookie on your browser and configure settings in google analytics to achieve this.

Continue reading