Upgrade to Pro — share decks privately, control downloads, hide ads and more …

intro into django for students

intro into django for students

Yura Hulpa

March 23, 2016
Tweet

More Decks by Yura Hulpa

Other Decks in Programming

Transcript

  1. 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?
  2. What Django is? Django is a free and open source

    web application framework, wri en in Python
  3. MVC •Model - Model in Django •View - Template in

    Django •Controller - View in Django
  4. 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
  5. 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
  6. 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)
  7. 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})
  8. 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')), ]
  9. 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>
  10. 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)
  11. 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