Table of contents
Table of Contents | ||
---|---|---|
|
...
- Install Django: run
pip install Django
from within a venv. - Creating a project
- Create a new Django project:
django-admin startproject mysite
- About the default files:
manage.py
- A command-line utility that lets you interact with this Django project in various ways.mysite/settings.py
- Settings/configuration for the Django project.mysite/urls.py
- The URL declarations for this Django project.- This is a similar concept to Web.py
mysite/wsgi.py
- An entry-point for WSGI-compatible web servers to serve your project.- This should be a familiar concept to anyone who has used PythonAnywhere to serve a Python web app.
- Create a new Django project:
- The development server
- Run the dev server:
python manage.py runserver
- The development server automatically reloads Python code for each request as needed. You don’t need to restart the server for code changes to take effect. However, some actions like adding files don’t trigger a restart, so you’ll have to restart the server in these cases.
- Run the dev server:
- Creating the Polls app
- Projects vs. apps - What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.
- Note that a Django "project" is different from a PyCharm project / git repo. Your Django project will have a "container" folder within your PyCharm project folder / git repo folder, and within that Django project container folder will be a Python package folder that's unique to your project.
- Apps don't live within the Django project's inner package directory. You can keep apps anywhere that a module can be imported from, but if it's tightly tied to your project you'd have it within the Django project's container directory, at the same level as the
manage.py
file and the project's Python package directory:pycharm_project_folder/
django_project_container_folder/
django_project_python_package_folder/
app_1_folder/
manage.py
etc.
- Create an app:
python manage.py startapp app_name
- Apps have the following initial structure:
app_name/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
urls.py
views.py
- Questions I have:
- Why doesn't the
mysite/
folder have aviews.py
,tests.py
, etc. by default?
- Why doesn't the
- Write your first view
- Hello World (
/app_name/views.py
):from django.http import HttpResponse
def index(request): return HttpResponse("Hello, world. You're at the polls index.")
- Create a URL configuration by filling out
/app_name/urls.py
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
- The Django term for this entry is apparently "a URLconf".
- Register the new URLconf with the root URLconf in
mysite/urls.py
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ]
Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
- The idea behind include() is to make it easy to plug-and-play URLs. Since polls are in their own URLconf (polls/urls.py), they can be placed under “/polls/”, or under “/fun_polls/”, or under “/content/polls/”, or any other path root, and the app will still work.
- path() arguments
- route
- A string that contains a URL pattern
- Django starts looking for matches at the first pattern in urlpatterns and makes its way down the list.
- view
- A function defined in views.py.
- Django calls the function with an HttpRequest object as the first argument and any “captured” values from the route as keyword arguments.
- kwargs - Arbitrary keyword arguments can be passed in a dictionary to the target view.
- name - Naming your URL lets you refer to it unambiguously from elsewhere in Django, especially from within templates. This powerful feature allows you to make global changes to the URL patterns of your project while only touching a single file.
- route
- Hello World (
...