Skip to content

Latest commit

 

History

History
113 lines (77 loc) · 2.73 KB

django-peer-learning.md

File metadata and controls

113 lines (77 loc) · 2.73 KB

My Django learning notes

Day 1: Sat, 5-6PM, 29th June, 2020

Tips

  • Use official documentation

  • Try to type every command mentioned in tutorial

  • Question why the tutorial is saying this or that way

  • Don't afraid from errors. Try to make love to them because this will help you improve.

  • There are many types of Errors:

    • Typo Error (Human Error)
    • System Error
    • Package level Error
    • ...
  • Full Stack Definition

    • Back-end and Front-end ( Old )
    • Back-end, Front-end and DevOps (you should be able to deploy your code to server) ( Now )

Building first Django

Reference link here.

  • Verify Django

    $ python -m django --version
    
  • Create Project

    $ django-admin startproject mysite
    
  • Run Server

    $ python manage.py runserver
    
  • Open http://127.0.0.1:8000/ in browser

Creating the Polls app

Click here

Database Setup

Click here

NOTE: Skip Introducing the Django Admin as it might be advanced for complete beginner

Creation Question View, Route

Use following lines in polls/views.py

from django.shortcuts import get_object_or_404, render

from .models import Question

def index(request):
  return HttpResponse("Namaste, World. You are at the polls index")

def detail(request, question_id):
  question = get_object_or_404(Question, pk=question_id)
  return render(request, 'polls/detail.html', {'question': question})

And, set path in polls/urls.py:

from django.urls import path

from . import views

urlpatterns = [
  path('', views.index, name='index'),
  path('<int:question_id>/', views.detail, name='detail'),
]

And, template file polls/templates/polls/detail.html:

<h1>{{ question.question_text }}</h1>
<ul>
  {% for choice in question.choice_set.all %}
      <li>{{ choice.choice_text }}</li>
  {% endfor %}
</ul>

Day 1 Output

Question View

NOTE: If you type /polls/2 then you will get error since you haven't created question with id 2.