Share
Embed
Transcript
18 slides
-
Intro It is al followup section to our first one
“Introduction into Django” I suppose you are already familiar with such stuff like: • Installing Django • MVC • Django Views, URL’s, Templates right? -
What Django is? Django is a free and open source
web application framework, wri en in Python -
MVC •Model - Model in Django •View - Template in
Django •Controller - View in Django -
Start a project (venv)101:django_intro yura$ django_intro yura$ django-admin startproject django_tutorials
cd django_tutorials/ (venv)101:django_tutorials yura$ ls django_tutorials manage.py (venv)101:django_tutorials yura$ ls django_tutorials/ __init__.py settings.py urls.py wsgi.py -
Start application (venv)101:django_tutorials yura$ ./manage.py startapp django_nulp (venv)101:django_tutorials yura$ ls
django_nulp django_tutorials manage.py (venv)101:django_tutorials yura$ ls -la django_nulp/ __init__.py apps.py models.py views.py admin.py migrations tests.py -
Model from __future__ import unicode_literals from django.db import models #
Create your models here. class Movie(models.Model): title = models.CharField(max_length=100) year = models.DateField() director = models.CharField(max_length=100) -
View(Controller) from django.shortcuts import render from .models import Movie #
Create your views here. def list_movies(request): movies = Movie.objects.all() return render(request, 'movies/list.html', { 'movies': movies}) -
URL Dispatcher(main app) from django.conf.urls import url, include from django.contrib
import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^movies/', include('django_nulp.urls')), ] -
URL Dispatcher(app) from django.conf.urls import url from .views import list_movies
urlpatterns = [ url(r'^', list_movies), ] -
Template(View) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Movies</title> </head>
<body> {% for movie in movies %} <p>{{ movie.title }}</p> {% endfor %} </body> </html> -
Let’s create a test To manipulate data in human readable
form from __future__ import unicode_literals from django.db import models # Create your models here. class Movie(models.Model): title = models.CharField(max_length=100) year = models.DateField() director = models.CharField(max_length=100) -
Almost done But our main django app does not know
nothing about our new app -
Migrations (venv)101:django_tutorials yura$ ./manage.py migrate Operations to perform: Apply all
migrations: admin, contenttypes, auth, sessions Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK #Here goes all basic Django tables, and state of their migrations (venv)101:django_tutorials yura$ ./manage.py makemigrations Migrations for 'django_nulp': 0001_initial.py: - Create model Movie (venv)101:django_tutorials yura$ ./manage.py migrate Operations to perform: Apply all migrations: admin, contenttypes, django_nulp, auth, sessions Running migrations: Rendering model states... DONE Applying django_nulp.0001_initial... OK