0% found this document useful (0 votes)
54 views5 pages

Django 3 - Webinar

This document provides instructions for setting up Django on a system. It outlines steps to install Python and Django, configure the environment, create a virtual environment, generate a project and application, set up URLs and models, install Django admin generator and Postgres, and build a basic CRUD application with views, URLs and templates. Key aspects covered include installing requirements, configuring the database, defining models, and generating the front-end for basic create, read, update and delete functionality.

Uploaded by

Pocho Ortiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views5 pages

Django 3 - Webinar

This document provides instructions for setting up Django on a system. It outlines steps to install Python and Django, configure the environment, create a virtual environment, generate a project and application, set up URLs and models, install Django admin generator and Postgres, and build a basic CRUD application with views, URLs and templates. Key aspects covered include installing requirements, configuring the database, defining models, and generating the front-end for basic create, read, update and delete functionality.

Uploaded by

Pocho Ortiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Django 

 
Editor de Texto [Opcional] 
https://download.sublimetext.com/Sublime%20Text%20Build%203211%20x64%20Setup.exe 
 
1. Instalar python 
 
Python 3.7.1 
 
64 bits .exe 
https://www.python.org/ftp/python/3.6.5/python-3.6.5-amd64.exe 

 
Otras distribuciones 
https://www.python.org/downloads/release/python-365/ 

 
2. Configurar Variables de Entorno 
3. Instalar generador de entornos Virtuales 
 
pip install virtualenvwrapper-win 

 
4. Crear Entorno virtual 
 
mkvirtualenv prueba 

 
5. Instalar Django 
 
pip install django 
 
django-admin startproject proyecto 

 
6. Crear aplicación en django 
 
python manage.py startapp nombreapp 

7. URLS DEL PROYECTO 


 
"""proyecto URL Configuration 
 
The `urlpatterns` list routes URLs to views. For more information 
please see: 
https://docs.djangoproject.com/en/3.0/topics/http/urls/ 
"""proyecto URL Configuration 
 
The `urlpatterns` list routes URLs to views. For more information please see: 
https://docs.djangoproject.com/en/3.0/topics/http/urls/ 
Examples: 
Function views 
1. Add an import: from my_app import views 
2. Add a URL to urlpatterns: path('', views.home, name='home') 
Class-based views 
1. Add an import: from other_app.views import Home 
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 
Including another URLconf 
1. Import the include() function: from django.urls import include, path 
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 
""" 
from django.contrib import admin 
from django.urls import path, include 
 
urlpatterns = [ 
path('vehiculos/', include('vehiculo.urls', namespace='vehiculos')), 
path('admin/', admin.site.urls), 

 
 
 
8. Models 
 
Importaciones 
from​d
​jango​
.​
db​​
import​​
models 
#from​​
django​
.​
utils​
.​
text​​
import​​
slugify 
#from​​
django​
.​
contrib​
.​
auth​
.​
models​​
import​​
User​
, ​
Group 
 
 
 
Campo Cualquiera 
 
tipo_vehiculo ​
= ​
models​
.​
CharField​
(​
max_length​
=​
256​
, ​
unique​
=​
True​

 
Llave Foránea 
nombre_campo​
= ​
models​
.​
ForeignKey​


Clase_que_voy_a_relacionar​
, ​
on_delete​
=​
models​
.​
CASCADE 

 
ejemplo: 
 
 
 
from django.db import models 
from django.db.models import CharField, FloatField 
# Create your models here. 
 
class TipoVehiculo(models.Model): 
"""Aquí se crean las clases""" 
 
tipo_vehiculo = CharField(max_length=10, unique=True) 
peso_maximo = FloatField() 
peso_minimo = FloatField() 
peso = FloatField() 
 
 
 
Cada vez que existan cambios 
 
python manage.py makemigrations 
python manage.py migrate 
 
 
9. Django Admin Generator 
 
Super Usuario 
python manage.py createsuperuser 
Instalar 
pip install django-admin-generator 
Generar 
python manage.py admin_generator nombreapp > admin1.py 

 
10.Instalar Postgres 
 
POSTGRESQL 10.11 
https://www.enterprisedb.com/thank-you-downloading-postgresql?anid=1257100 
 
PGADMIN 4 EXE 
https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v4.16/windows/pgadmin4-4.16-x86.exe 
 
INSTALAR POSTGRES EN DJANGO 
pip install psycopg2 

 
11. Crear Crud en Django 
1. Conexión a la base de datos 
2. Con Postgresql 
DATABASES​= { 
"
​default"​
: { 

"ENGINE"​
: "
​django.db.backends.postgresql"​


"NAME"​
: ​
'nombre_base_de_datos'​


"USER"​
: ​
'usuario'​


"PASSWORD"​
: ​
'contraseña'​


"HOST"​
: ​
'localhost'​


"PORT"​
: ​
'5432'​


3. STATIC FILES 
 
STATIC_URL​= ​
'/static/' 
STATICFILES_DIRS​= (​
os​
.​
path​
.​
join​
(​
BASE_DIR​
, ​
"static"​
),) 
STATIC_ROOT​= o
​s​
.​
path​
.​
join​
(​
BASE_DIR​
, ​
"staticfiles"​

MEDIA_URL​= ​
"/media/" 
MEDIA_ROOT​= ​
os​
.​
path​
.​
join​
(​
BASE_DIR​
, ​
"media"​

FILE_UPLOAD_MAX_MEMORY_SIZE​= 1
​00242880​ ​
# 95,59MB 
 
 
 
4. CRUD 
Views 

from django.views.generic import ListView,


CreateView, DetailView, UpdateView
from .models import TipoVehiculo

class TipoVehiculoList(ListView):
model = TipoVehiculo 
 
URL’s 
 
 

urlpatterns = [

url(r'^$', TipoVehiculo.as_view(),
name='list'),
url(r'^(?P<pk>\d+)$', TipoVehiculo.as_view(),
name='detail'),
url(r'^nuevo$', TipoVehiculo.as_view(),
name='new'),
url(r'^editar/(?P<pk>\d+)$',
TipoVehiculo.as_view(), name='edit'),
url(r'^borrar/(?P<pk>\d+)$',
TipoVehiculo.as_view(), name='delete'),


TEMPLATE 
 

<h1>Vehículos</h1>

<p>
<a href="{% url "vehiculo:new" %}">Agregar
curso</a>
</p>

<ul>
{% for course in object_list %}
<li>
<p>{{ course.name }}</p>
<p>
<a href="{% url "courses:detail"
course.id %}">Ver</a> |
<a href="{% url "courses:edit"
course.id %}">Editar</a> |
<a href="{% url "courses:delete"
course.id %}">Borrar</a>
</p>
</li>
{% endfor %}
</ul> 
 
12. Requirements.txt 
 
Exportar Requirements 
pip freeze > requirements.txt 
 
Importar Requirements 
pip install -r requirements.txt 
 
 
def cuenta_usuario(request): 
 
ctx = {} 
return render(request, plantilla.html', ctx) 
 

You might also like