Showing posts with label Django. Show all posts
Showing posts with label Django. Show all posts

Sunday, February 2, 2014

hide fields in django forms

Suppose we have the following model:

models.py:

from django.db import models

# Create your models here.
class Article(models.Model):
    name= models.CharField(max_length=200)
    country= models.TextField()
    bdate = models.DateTimeField('date published')
    age= models.IntegerField(default=1, null=True, blank=True)

forms.py:

from django import forms
from models import Article

class ArticleForm(forms.ModelForm):

    class Meta:
        model = Article

The above form will show all the four (04) fields.
To hide may be the age of the student, we'll modify the forms.py as follows:

forms.py:

from django import forms
from models import Article

class ArticleForm(forms.ModelForm):

    class Meta:
        model = Article
        fields = ('name', 'country', 'bdate')

 

Done !!

Django model : defautl, blank and null

from django.db import models

# Create your models here.
class Student(models.Model):
    age= models.IntegerField(default=1, null=True, blank=True)


No comment

Monday, January 13, 2014

Best tutorial for Django

I know this is apparently a hot topic that may raise a lot of discussion, however having wasted a substantial amount of my time trying and searching the best doc to get started with django framework I came to this conclusion. The Django Book is the most comprehensive place to get get started with    Django.       Being online it is  regularly updated compared to other books  you may have out there.
That said you are warned, don't go waste your precious time out there trying to find NOTHING.
You can easily use the scrapbook addon of firefox to download the entire website and read it offline.
You too if you have found something better please don't hesitate to share it  with us...

Django admin page without style

Yep if you have into this mess


and now wondering how to restyle your fancy admin page, cool down it very simple. Just go to the settings.py file and make sure that you have enable this apps:

'django.contrib.staticfiles',

you may then have

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'books',
)

Tuesday, January 7, 2014

django administration: You don't have permission to edit anything.

Yep! it seems you are locked out of your own application. Is this how django treats its followers ?
Trust me Django is right, because after it is for your own good against the www attacks.

This is a minor issue just good to ursl.py and add the following line:
admin.autodiscover()

Finally your urls.py will look ask follow:
from django.conf.urls import patterns, include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
)
Happy coding !!!

Monday, January 6, 2014

unexpected keyword argument 'maxlength' in Django

This problem arise because you are using the latest version of django. Just replace maxlength with max_length.  If you really want to keep using maxlength you must downgrade to django .96.
I hope it removes your road block, have a safe coding journey. See you next time...

python manage.py validate no module named image error

Are you victim of Django ImageField from your model declaration and keep getting "no module named image error" ? Ok, chances are that you have not installed the PIL or  python image library.

Just download and install the latest version and keep rocking. 

Sunday, January 5, 2014

ImproperlyConfigured: Requested setting TEMPLATE_DEBUG, but settings are not configured.

Maybe you want to test your first django template but keep getting this annoying error message. You certainly getting this error because you are misusing the shell. You are using python shell instead of a django managed shell. Saying this, what do i really mean. Suppose you want to test the following template:
>>> from django.template import Context, Template
>>> t = Template("Hello {{ name }}.")
>>> c = Context({"name": "World"})
>>> t.render(c)
Instead of running the above script from the python shell, go the command prompt ( if you are using winodws os)

cmd>django-admin.py startproject dummyProject
cmd> cd dummyProject
dummyProject> python manage.py shell
>>> 
>>> from django.template import Context, Template
>>> t = Template("Hello {{ name }}.")
>>> c = Context({"name": "World !"})
>>> t.render(c)
Hello World !

Congratulation , you just run your first django template.

Saturday, January 4, 2014

Sqlite3 installation on windows

So you want to install sqlite3 and wonder how to do so because there is no single executable file to click on and follow up ?
No problem just go to sqlite3 download page  and under the Precompiled Binaries for Windows part, download the following three file and unzip them.
-sqlite-shell-win32-x86-xxx.zip
-sqlite-dll-win32-x86-xxx.zip
-sqlite-analyzer-win32-x86-xxx.zip


Then after unzipping them put, paste the unzipped files in C:\windows\System32

That is all, now from your command prompt terminal (CMD) type: sqllite3 and your are in.
sqlite>.help

Installing Django on Windows 7 in less than 5 minutes

Out there, there are many tutorials but I think this one may just make the differece.
I assume that you have downloaded a python 2.7.x  and copy of Django 1.6.x .I also suppose that you are familiar with cmd commands like cd, dir , cd.., etc

Step ONE: Install Python
Click on the Python just downloaded to launch the installation. Just follow next and all the defaults.
It should be installed in C:\python27.

Step TWO: Make Python available in the terminal (cmd):
To do this you have to register it as system variables.
Right-Click Properties --> Advanced system settings -->Environment Variables -->System variable -->path -->Edit: and paste the following.
;C:\Python27;C:\Python27\Scripts;C:\Python27\DLLs\;C:\Python27\Lib\;

Step THREE: install Django
Unzip the downloaded compressed folder under the C: drive.
Go to the Command prompt CMD and navigate inside the C:\Django 1.6.x folder.
> cd C:\Django 1.6.x
 C:\Django 1.6.x> python.exe setup.py install 
Please wait till all is done. 

Step FOUR: Create a Django Project Go to a location where you want to create your project. Our project name is ido.

cmd ..> django-admin.py startproject ido
 ..ido> python.exe manage.py runserver

Step FIVE: Launch it.
 Just open your browser and type:
 http://127.0.0.1:8000

By now you should have your project available. Feel free to amend.