Django Step by Step in Windows

Following steps in the tutorial of Djangoproject.com from quick installation to complete 7 modules:

I have installed python2 and 3 via Anaconda already, but to work with Django, it’s easier to leverage pip command in cmd directly, so I decided to install to C:\Users\ncarucci\AppData\Local\Programs\python again. The latest version python 3.7.3 comes with pip and idle. Next, there is a small hurdle of setting up environmental variable PATH to make pip running directly from cmd command line. For some reason I don’t know, typing set PATH=%PATH%; C:\\users\\ncarucci\\appdata\\local\\programs\\python\\python37 didn’t work, so I turn to windows environmental variable setting to manually enter “%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;%USERPROFILE%\AppData\Local\Programs\Python\Python37\Scripts;%USERPROFILE%\AppData\Local\Programs\Python\Python37;”. I notice that I have to follow/use %USERPROFILE% instead of c:\users\ncarucci here for it to work.

Next, create virutual environment by typing “virtualenv env_site1″, I will then apply all apps/webs within this virtual environment going forward. to make it work, first activate: C:\Users\ncarucci\env_site1\Scripts>activate, then it shows ” (env_site1) C:\Users\ncarucci\env_site1\” before prompt.

Now I will install Django by ” (env_site1) C:\Users\ncarucci\env_site1\Scripts>pip install django “, then enter python to enter to shell, Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32 Type “help”, “copyright”, “credits” or “license” for more information. type >>> import django, >>> django.get_version() 2.2.1′. Now to get a feel of Django, type ” django-admin startproject sampsite” to create a sample site project, an empty framework is set to run, then type “python manage.py runserver”, ” Starting development server at http://127.0.0.1:8000/ ” is shown in cmd window, toggle back to the Chrome to open up this url, the page will show heartwarming “successful” banner on it.

Django sampsite comes with these pre-built files:

then when we create a page/url beneath this project of mysite, type “python manage.py startapp polls”, note the directly you sit on when typing commands. Polls folder contains pre-built django files too.

It’s worth noting there are two urls.py files, one in the mysite folder, one in polls folder.

The upper level urls.py matches the url path of child app polls directly:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path(‘polls/’, include(‘polls.urls’)),
path(‘admin/’, admin.site.urls),
]

While the lower level urls.py set up path urlpatterns more specifically:

from django.urls import path

from . import views

urlpatterns = [
# ex: /polls/
path(”, views.index, name=’index’),
# ex: /polls/5/
path(‘/’, views.detail, name=’detail’),
# ex: /polls/5/results/
path(‘/results/’, views.results, name=’results’),
# ex: /polls/5/vote/
path(‘/vote/’, views.vote, name=’vote’),
]

Views.py file is important that it define the functions or views to run in the back end. A very first attempt is after setting a simple HttpResponse upon request, is to make it more dynamically displaying such as

from django.http import HttpResponse
from django.template import loader
from .models import Question

def index(request):
latest_question_list = Question.objects.order_by(‘-pub_date’)[:5]
template = loader.get_template(‘polls/index.html’)
context = {
‘latest_question_list’: latest_question_list,
}
return HttpResponse(template.render(context, request))

As to how the output will be displayed in html format, the index.html is written as

{% if latest_question_list %}

{% else %}

No polls are available.

{% endif %}

Now we find the hard-coded /polls/{{question.id}}/ part too rigid, so it’s better to replace it with a interactive plug-in, {% url ‘detail’ question.id %}, which is corresponding to the url setting:

#the ‘name’ value as called by the {% url %} template tag

path(‘ int:question.id/ ‘, views.detail, name=’detail’),

Lastly, it’s necessary to know the namespace so the url tag is clear to know when and where to be referenced.

in the url file, add app_name = ‘polls’

in the index.html file, modify href to be “{% url ‘polls:detail’ question.id %}”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.