Basic Django for Rails devs
Python
15 flashcards · Programming Languages · by lakesare
Name Django MVC parts.
model template (view rendered to a user) view (rails controller!) If you’re familiar with other MVC Web-development frameworks, such as Ruby on Rails, you may consider Django views to be the controllers and Django templates to be the views.
In Rails routes are kept at /config/routes.rb. Where are routes stored in Django?
/portal/urls.py
Rails = /controllers/authors_controller.rb | Django = ? Rails = /models/Author.rb | Django = ?
Rails = authors_controller.rb | Django = authors/views.py Rails = models/Author.rb | Django = authors/models.py
Django project VS app?
project: entire application and all its parts. app: submodule of the application. An app typically has its own models.py (which might actually be empty). App is, hopefully, self-contained and not intertwined with other apps in the project so that, in theory, you could pick it up and plop it down into another project without much, or any, modification. Links: https://docs.djangoproject.com/en/dev/ref/applications/#projects-and-applications
Rails: rails server Django: ?
python manage.py runserver Run a server on your local computer (on localhost:8000). You must run this in the ROOT of your django project where manage.py lives.
Explain what each of these files does: django-admin startproject mysite manage.py ❤️ app_name/settings.py app_name/urls.py app_name/wsgi.py ❤️ python manage.py startapp polls app_name/apps.py ❤️ app_name/admin.py ❤️ app_name/views.py app_name/models.py app_name/tests.py app_name/migrations/*
django-admin startproject mysite manage.py - used as python manage.py some_command app_name/settings.py app_name/urls.py app_name/wsgi.py - Web Server Gateway Interface, used for deployment python manage.py startapp polls app_name/apps.py - this file is created to help the user include any application configuration for the app. Using this, you can configure some of the attributes of the application (name, verbose name, label, path). app_name/admin.py - adds the model to Django Admin interface b…
What is WSGI?
Web Server Gateway Interface, Python standard that describes how a web server communicates with web applications
Explain each argument in /app_name/urls.py: urlpatterns = [ url( r'^exam/', views.exam, name='exam' ) ]
url( r'^exam/', - regex to match urls views.exam, - view action to use name='exam' - used for a reverse lookup for this url! ) After defining name as 'exam', you can, in templates: <a href="{% url 'exam' %}">Go to /exam!</a> in code: from django.core.urlresolvers import reverse return HttpResponseRedirect(reverse('exam'))
settings.py has a section: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } What do 'ENGINE' and 'NAME' mean?
ENGINE - database backend to use. .mysql, .postgresql, etc, are included by default. NAME - database name When connecting to other database backends, such as MySQL, Oracle, or PostgreSQL, additional connection parameters will be required:DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'mydatabase', 'USER': 'mydatabaseuser', 'PASSWORD': 'mypassword', 'HOST': '127.0.0.1', 'PORT': '5432', } } Links: https://docs.djangoproject.com/en/2.0/ref/settings/#databases
What is Django Admin?
Automatically created Django interface for changing Models (adding/deleting rows, etc). Generating admin sites for your staff or clients to add, change, and delete content is tedious work that doesn’t require much creativity. For that reason, Django solves the problem of creating a unified interface for site administrators to edit content.The admin isn’t intended to be used by site visitors. It’s for site managers.
python manage.py makemigrations VS python manage.py migrate?
python manage.py makemigrations - creates new migration files based on the changes detected to your models python manage.py migrate - actually executes these migrations in SQL
What will urlpatterns = [ url(r'^$') ] match?
r'^$' will only match an empty string. r - signifies a raw string (slashes are treated as usual, which is useful for regexes) ^ - beginning of a string $ - end of a string
/posts/views.py: from django.shortcuts import render def index(request): return render(request, 'posts/index.html') Where should index.html be located to be rendered?
/posts/templates/posts/index.html
To populate database with some initial values - Rails: /db/seeds.rb rake db:seed Django: ?
/your-any-app/fixtures/<fixturemame>.json python manage.py loaddata <fixturename>.json [ { "model": "myapp.person", "pk": 1, "fields": { "first_name": "John", "last_name": "Lennon" } }, { "model": "myapp.person", "pk": 2, "fields": { "first_name": "Paul", "last_name": "McCartney" } } ] Link: https://docs.djangoproject.com/en/1.9/howto/initial-data
Django views: render VS render_to_response?
render_to_response is deprecated since Django 2.0. To make render_to_response pass variables from other Middlewares, (user, csrf_token, and messages) you must send a context_instance like this: def article_list(request): posts = Post.objects.all() return render_to_response(template_name, {'posts': posts}, context_instance=RequestContext(request)) Render includes RequestContext automatically: return render(request, template_name, {'posts': posts}) Links: render vs render_to_response