informatica:linux:django
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| informatica:linux:django [2014/03/11 09:10] – [Archivos estaticos] javi | informatica:linux:django [2019/05/19 16:55] (current) – [Activar la interfaz administrativa en app1 y tabla 'Tabla1'] 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 117: | Line 135: | ||
| - | ===== Activar la interfaz administrativa en app1 y tabla ' | + | ==== (Deprecated) |
| 2.1 Crear: | 2.1 Crear: | ||
| Line 265: | Line 283: | ||
| https:// | https:// | ||
| - | TODO | + | 1. Creamos el directorio desde donse se van a servir los archivos estaticos. Puede estar fuera del DocumentRoot de Apache |
| - | | + | |
| + | |||
| + | 2. Definir la variable " | ||
| - | ===== Consultas ===== | + | < |
| + | vim / | ||
| - | QuerySet | + | ... |
| + | STATIC_ROOT = '/ | ||
| + | ... | ||
| + | </ | ||
| - | Tabla1.objects.filter(pub_date__year=2006) | + | 3. Entramos en el site de django y ejecutamos un comando para copiar todos los archivos estaticos a ese directorio: |
| - | + | ||
| - | ===== Instalacion manual ===== | + | |
| - | 1. Download the latest release from our download page. | + | < |
| + | cd / | ||
| + | python manage.py collectstatic | ||
| - | 2. Untar the downloaded file (e.g. tar xzvf Django-X.Y.tar.gz, | + | You have requested to collect static files at the destination |
| + | location | ||
| - | 3. Change into the directory created in step 2 (e.g. cd Django-X.Y). | + | This will overwrite existing files! |
| + | Are you sure you want to do this? | ||
| - | 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' | + | Type 'yes' to continue, or ' |
| + | Copying '/ | ||
| + | ... | ||
| + | </ | ||
| - | python setup.py install | + | Al final tenemos en "/ |
| + | 4. Anyadir la entrada al VirtualHost de Apache (version < 2.4): | ||
| + | |||
| + | < | ||
| + | ... | ||
| + | Alias /static/ / | ||
| + | < | ||
| + | # Apache < 2.4 | ||
| + | Order deny,allow | ||
| + | Allow from all | ||
| + | </ | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | Para Apache >= 2.4: | ||
| + | |||
| + | < | ||
| + | ... | ||
| + | Alias /static/ / | ||
| + | < | ||
| + | # Apache >= 2.4 | ||
| + | Require all granted | ||
| + | </ | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | 5. Recargar la configuracion de Apache para que los cambios tomen efecto: | ||
| + | |||
| + | sudo service apache2 reload | ||
| + | ===== Consultas ===== | ||
| + | |||
| + | QuerySet | ||
| + | |||
| + | Tabla1.objects.filter(pub_date__year=2006) | ||
| + | | ||
| ===== Ejemplo aplicacion sin BBDD ===== | ===== Ejemplo aplicacion sin BBDD ===== | ||
| Line 452: | 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 701: | 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.1394529053.txt.gz · Last modified: (external edit)
