Ярлыки

Показаны сообщения с ярлыком Django. Показать все сообщения
Показаны сообщения с ярлыком Django. Показать все сообщения

суббота, 3 ноября 2012 г.

Django: django-admin - user was not authenticated


Problem: //localhost:8000/admin => error "user was not authenticated".
Solve: in postgreSQL - add this user as superuser; restart DB.

Django: default values in views


in urls:
...
    (r'^/test/$',test_func),
    (r'^/test/(\d+)/$',test_func),
...

in views:

def test_func(request, ddd='0'):
...


Django: two buttons in the one form


There are two buttons in the one form:
<form ...>
...

<input type="submit" name=b1 value="Request">
<input type="submit" name=b2 value="Refresh">
</form>

code in views.py:
if request.GET.get('b2',None)!=
None:
   (code for refresh form)
else:
   (code for request data)

Django: using form in views


Some way to use forms in views:
blank form or form with some content:
if request.method=='GET':
   form1=SelectForm(request.GET)
else:
   form1=SelectForm()

And than we send to form necessary corteges for "select" elements (each cortege include  few two-elements cortege and like as ((,),(,),(,),...,(,)) ):
form1.fields['select1'].
choices=((1,'First'),(2,'Second'),(3,'Third'))

We can get data from form:
datas=[]
if form1.is_valid():
   datas=form1.cleaned_data
   dd=datas['select2']
( format of the value "cleaned_data" is dict() )

django.forms documentation


Necessary fields:
https://docs.djangoproject.com/en/1.3/ref/forms/fields/

Tehere are few properties for each field:
1) Label - set normal text prefix for field
2) Initial  - set initial values, which must be correcting (initial values handle as unfilled)
3) widget define interface of the field:
https://docs.djangoproject.com/en/1.3/ref/forms/widgets/
                 widget.attrs - is way to do different fields
4) helptext - pop-up text(?)

About forms in docs:
http://docs.djangoproject.com/en/1.3/topics/forms/

Django: utf-8


Python 2.7, Django 1.3, Apache2

For all to work via utf-8:

1) In the template base.html on the begin of the file we must write:
<meta http-equiv="Cintent-Type" content="text/html" charset=utf-8" Content-Transfer-Encoding: base64>

2) each python file must starting with follow string:
# -*- coding: utf-8 -*-

3) in functions view.py we change request encoding to utf-8:
request.encoding='utf-8'

4) in settings.py we must have:
LANGUAGE_CODE = 'ru'
In django.forms we can use IDs for work with combobox (there is not utf-8 in address string).

четверг, 25 октября 2012 г.

Install Django on UbuntuServer

How I installed Django on UbuntuServer 11.04 - the Natty Narwhal
python2
django1.3

Apache already installed. 

Installing apxs2: 
 sudo apt-get install apache2-threaded-dev

Installing mod_python-3.3.1:
sudo apt-get install libapache2-mod-python

stop Apache:
sudo /etc/init.d/apache2 stop

Than we write settings in Apache config file (for me - /etc/apache2/apache2.conf). This settings bind Django with URL:
#Connect to mod_python:
LoadModule python_module /usr/lib/apache2/modules/mod_python.so

#Connect Django with URL:
<Location "/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE settings
    PythonDebug Off
    PythonPath "['/home/vinger4/vinger4site','/usr/local/lib/python3.2/dist-packages/django'] + sys.path"
    PythonAutoReload Off
</Location>

Correcting urls.py and settings.py
You must delete prefix of you site (in my case - vinger4site), because it wrote in PythonPath for Apache (please, see previous paragraph).

start Apache
sudo /etc/init.d/apache2 start

Now each request to the server must go through Apache.

Virtual Machine Image with Django


Virtual Machine Image with Django: 
http://bitnami.org/stack/djangostack