informatica:linux:django
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
informatica:linux:django [2014/03/11 12:28] – javi | informatica:linux:django [2019/05/19 16:55] – [Activar la interfaz administrativa] javi | ||
---|---|---|---|
Line 2: | Line 2: | ||
django python framework | django python framework | ||
+ | |||
+ | ===== Instalacion ===== | ||
+ | |||
+ | 1. Instalar pip | ||
+ | |||
+ | sudo aptitude update; sudo aptitude install python-pip | ||
+ | | ||
+ | 2. Instalar ultima version de django: | ||
+ | |||
+ | sudo pip install Django==1.8 | ||
===== Primeros pasos ===== | ===== Primeros pasos ===== | ||
Line 91: | Line 101: | ||
python manage.py runserver 0.0.0.0: | python manage.py runserver 0.0.0.0: | ||
- | ===== Activar la interfaz administrativa ===== | + | ===== Interfaz administrativa ===== |
+ | |||
+ | Ahora la interfaz administrativa se activa por defecto | ||
+ | |||
+ | ==== Crear superusuario ==== | ||
+ | |||
+ | https:// | ||
+ | |||
+ | ==== (Deprecated) | ||
1 | 1 | ||
Line 269: | Line 287: | ||
mkdir /tmp/static | mkdir /tmp/static | ||
- | 2. Entramos en el site de django y ejecutamos un comando para copiar todos los archivos estaticos a ese directorio: | + | 2. Definir la variable " |
+ | |||
+ | < | ||
+ | vim / | ||
+ | |||
+ | ... | ||
+ | STATIC_ROOT = '/ | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | 3. Entramos en el site de django y ejecutamos un comando para copiar todos los archivos estaticos a ese directorio: | ||
< | < | ||
Line 288: | Line 316: | ||
Al final tenemos en "/ | Al final tenemos en "/ | ||
- | 3. Anyadir la entrada al VirtualHost de Apache (version < 2.4): | + | 4. Anyadir la entrada al VirtualHost de Apache (version < 2.4): |
< | < | ||
Line 311: | Line 339: | ||
</ | </ | ||
... | ... | ||
- | </code | + | </code> |
- | 4. Recargar la configuracion de Apache para que los cambios tomen efecto: | + | 5. Recargar la configuracion de Apache para que los cambios tomen efecto: |
sudo service apache2 reload | sudo service apache2 reload | ||
- | |||
===== Consultas ===== | ===== Consultas ===== | ||
Line 323: | Line 350: | ||
Tabla1.objects.filter(pub_date__year=2006) | Tabla1.objects.filter(pub_date__year=2006) | ||
| | ||
- | ===== Instalacion manual ===== | ||
- | |||
- | 1. Download the latest release from our download page. | ||
- | |||
- | 2. Untar the downloaded file (e.g. tar xzvf Django-X.Y.tar.gz, | ||
- | |||
- | 3. Change into the directory created in step 2 (e.g. cd Django-X.Y). | ||
- | |||
- | 4. If you're using Linux, Mac OS X or some other flavor of Unix, enter the command sudo python setup.py install at the shell prompt. If you're using Windows, start a command shell with administrator privileges and run the command python setup.py install. This will install Django in your Python installation' | ||
- | |||
- | python setup.py install | ||
- | |||
===== Ejemplo aplicacion sin BBDD ===== | ===== Ejemplo aplicacion sin BBDD ===== | ||
Line 500: | Line 515: | ||
} | } | ||
... | ... | ||
+ | </ | ||
+ | |||
+ | Flujo habitual para mantener el modelo: | ||
+ | |||
+ | < | ||
+ | 1. Change your models (in **models.py**). | ||
+ | 2. Run **python manage.py makemigrations** to create migrations for those changes | ||
+ | 3. Run **python manage.py migrate** to apply those changes to the database. | ||
</ | </ | ||
Line 749: | Line 772: | ||
python -c " | python -c " | ||
+ | | ||
+ | ===== LDAP ===== | ||
+ | |||
+ | pythonhosted.org/ | ||
+ | |||
+ | 1. Instalar modulo: | ||
+ | |||
+ | sudo aptitude install python-ldap | ||
+ | sudo pip install django-auth-ldap | ||
+ | |||
+ | 2. Editar settings: | ||
+ | |||
+ | < | ||
+ | docroot/ | ||
+ | |||
+ | # LDAP | ||
+ | import ldap | ||
+ | from django_auth_ldap.config import LDAPSearch | ||
+ | AUTHENTICATION_BACKENDS = ( | ||
+ | ' | ||
+ | ) | ||
+ | AUTH_LDAP_SERVER_URI = " | ||
+ | AUTH_LDAP_BIND_DN = " | ||
+ | AUTH_LDAP_BIND_PASSWORD = " | ||
+ | # | ||
+ | AUTH_LDAP_USER_SEARCH = LDAPSearch(" | ||
+ | " | ||
+ | # LDAP groups | ||
+ | from django_auth_ldap.config import LDAPSearch, GroupOfNamesType | ||
+ | AUTH_LDAP_GROUP_SEARCH = LDAPSearch(" | ||
+ | ldap.SCOPE_SUBTREE, | ||
+ | ) | ||
+ | AUTH_LDAP_GROUP_TYPE = GroupOfNamesType() | ||
+ | AUTH_LDAP_REQUIRE_GROUP = " | ||
+ | </ | ||
+ | |||
+ | En este ejemplo nos conectamos via TLS al servidor LDAP " | ||
+ | |||
+ | 3. Crear el modelo de base de datos, si es que no lo estaba ya: | ||
+ | |||
+ | < | ||
+ | cd docroot/ | ||
+ | </ | ||
+ | |||
+ | Contestar a las preguntas. | ||
+ | |||
+ | **TODO**: ver si hay alguna forma de evitar este paso, y que se almacenen todos los valores en sesiones. | ||
+ | **SOLUCION 1**: sobreescribir _LDAPUser._get_or_create_user() de "/ | ||
+ | **SOLUCION 2**: escribir nuestro propio backend tomando django_auth_ldap como ejemplo | ||
+ | |||
+ | 4. Ejemplo de formulario con validacion de usuario: | ||
+ | |||
+ | < | ||
+ | from django.contrib.auth import authenticate | ||
+ | |||
+ | def login(request): | ||
+ | ''' | ||
+ | d = {} | ||
+ | if request.method == ' | ||
+ | d[' | ||
+ | if d[' | ||
+ | # Process the data in form.cleaned_data | ||
+ | username = d[' | ||
+ | password = d[' | ||
+ | user = authenticate(username=username, | ||
+ | if user is not None: | ||
+ | logger.info(user) | ||
+ | return HttpResponse(' | ||
+ | if user.is_active: | ||
+ | login(request, | ||
+ | return HttpResponse(' | ||
+ | else: | ||
+ | return HttpResponse(' | ||
+ | else: | ||
+ | return HttpResponse(' | ||
+ | else: | ||
+ | d[' | ||
+ | else: | ||
+ | d[' | ||
+ | return render_to_response(' | ||
+ | | ||
+ | </ | ||
+ | |||
+ | Falta el template y el resto de la vista. Es solo un ejemplo | ||
+ | |||
+ | ===== Errores ===== | ||
+ | |||
+ | ==== The password is too similar to the username. ==== | ||
+ | |||
+ | En realidad no es un error, es solo para documentar un atajo para evitar esta restricción a la hora de especificar una contraseña para un nuevo usuario desde la interfaz gráfica del módulo admin. | ||
+ | |||
+ | https:// | ||
+ | |||
+ | 1. Crear el usuario desde el admin: | ||
+ | |||
+ | http:// | ||
+ | |||
+ | 2. Iniciar shell | ||
+ | |||
+ | cd / | ||
+ | | ||
+ | 3. Cambiar la contraseña de ese usuario, en este ejemplo " | ||
+ | |||
+ | < | ||
+ | from django.contrib.auth.models import User | ||
+ | user = User.objects.get(username=' | ||
+ | user.set_password(' | ||
+ | user.save() | ||
+ | </ | ||
+ | |||
+ | ==== Error al crear app ==== | ||
+ | |||
+ | Comando: | ||
+ | |||
+ | < | ||
+ | python manage.py startapp app1 | ||
+ | </ | ||
+ | |||
+ | Error: | ||
+ | |||
+ | < | ||
+ | File " | ||
+ | ) from exc | ||
+ | ^ | ||
+ | SyntaxError: | ||
+ | </ | ||
+ | |||
+ | Solución: | ||
+ | |||
+ | < | ||
+ | python3 manage.py startapp app1 | ||
+ | </ |
informatica/linux/django.txt · Last modified: 2019/05/19 16:55 by javi