The official Django tutorial, extended for PyCharm demo uses.
- Clone this repo, open in PyCharm Professional, which should:
- Make a virtual environment
- Prompt to install dependencies from
requirements.txt - Configure the settings that recognize this as a Django project
- Make a Django run configuration
npm installto get JS dependencies.- Open PyCharm's
manage.pyconsole viaTools | Run manage..py task makemigrationsand press entermigrateand press entercreatesuperuser(and answer the questions)- For preloading questions into DB, make sure to run
python manage.py load_questions - Run the created run config (probably named
django_tutorial) - Visit
http://127.0.0.1:8000/admin/and add a question - Visit
http://127.0.0.1:8000/polls/
Other steps:
Mark as Templates Folderforpolls/templatesSettings | Languages & Frameworks | JavaScript | Prettierand set it up to run on save/reformat- Ensure Django support and pytest are configured in settings
- In the main template, find the
<script>for Tailwind, Alt-Enter on the URL, and "Download" as a library - Add the SQLite instance in the database tool
For the extended demo:
- Have the
BlackConnectplugin installed- Have
blackdinstalled somewhere, e.g. via pipx or add to the venv - But NOT set up to run on Reformat/Save
- Have
- Install
.envandrequirementsplugins
We're going to talk about the "I" in "IDE".
- PyCharm eliminates some cumbersome setup steps
- Show that you could use PyCharm's bundled
Djangotemplate inNew Project- Makes and sets a virtual env
- Installs Django into it
- Makes a sample Django project
- Creates run config
- Configures Django support
- Or, clone https://github.com/pauleveritt/django-tutorial-extended from VCS
- Shift-Shift
tem/basto opentemplates/base.html - Run the
Django Serverrun configuration - Click on link in the run window
- Navigate to
View Polls- Delete the
imgwithPyCharm.svg - Reload browser
- Cmd-Alt-O IndV to
polls/views.pynumber of returned questions by changing[0:10]to[0:1] - See that Django restarts the process
- Delete the
- Use
Run manage.pytool window tomakemigrations- Cmd-Shift-A mana to bring up manage.py tool
- Type in
makemigrations
- Run server under the debugger, not a big speed hit
- Stop the running server
- Restart it with the debugger
- Set breakpoints in:
polls.views.IndexView.get_queryset- Also in
polls.templates/polls/index.html- Inside
{% for question in latest_question_list %}
- Inside
- Reload the page at
/polls/ - Show stepping through Python and Django
- Clear the breakpoints and resume
- Autocomplete
Shift-Shift INSto navigate toINSTALLED_APPSinmysite/settings.py- Autocomplete any package names in strings
- Make a typo, show the warning
- Open
Shift-Shift pol/indto openpolls/index.html - Autocomplete
latest_question_liston line 9
- Navigate
- Same spot in
polls/index.html, navigate tolatest_question_list - Same for
INSTALLED_APPS - In
polls/index.htmlnavigate back and forth, view/template, via icons
- Same spot in
- Autoimport
Cmd-Alt-O urlpato openpolls/urlpatterns.py- Change
views.ResultsView.as_view()toReViandCmd-Space-Spaceto autocomplete - Show that it generated the import
- Refactor
Shift-Shift IndVto navigate topolls.views.IndexView- Refactor Rename
index.htmltodjindex.html - Point out the filename has changed
- Undo
Shift-Shift pol/indto openpolls/index.html- On Line 12, insert new line and recreate the
<img srcwith Emmetimg tabto start the<img>LiveTemplate- template for
{% sto start thestaticDjango tag - Autocomplete the part of the
polls/images/jb_beam.pngpath - Make a typo to show squiggly
- Fix typo
- Cursor in the
jb_beam.pngsegment - Cmd-B to navigate to the PNG
Cmd-Shift-A Spl Rito split right- On left:
Cmd-Alt-O IndVto navigate to IndexView - On right:
Cmd-Alt-O t_pto navigate totests/test_base_functions.test_polls_index - Click the gutter icon to run just that test
- On left: put a breakpoint in
IndexView.get_queryset - In test, click gutter icon and run in debug mode
TODO We need to file a PR.
- Make sure Django Server is running in debug mode
- Open
run-apis.http - Run the first URL
- Put a breakpoint in
IndexView.get_queryseton thereturn - Click that URL again
- Continue, clear breakpoint
base.htmland<script src="https://cdn.tailwindcss.com">- We downloaded the "library" with Alt-Enter
- Go to
<div class="navbarand autocompletenavbar - Then, navigate to
navbarto see definition
- In
package.json, run thedevscript - Click the URL
- See the React app
- Navigate to Symbol
App - Remove the import of
Index - Autocomplete the usage and show the import, then navigate to it
- Mention
package.jsonand npm integration- You get a popup with link for installing
- Autocomplete in package.json
- In preferences, show Prettier integration
- Make sure prettier on save and reformat are checked
- In
Index.jsxmess up spacing, indentation - Reformat Code
- Show preferences for eslint
- In
Index.jsxput a breakpoint on{question.question_text} - Split Right
- Shift-Shift
Index.testo navigate to the test file - Use gutter icon to run test under the debugger
- Open the database tool
- Double-click on the
polls_questiontable
This goes before the full-stack part and covers material from Adam's "Boost Your Django DX" book.
- In
IndexView.get_querysetmouse over:importfor Python codefilterfor Django code
- Mouseover an
h1to show MDN-integrated docs
- Open
requirements.txt - Add another package
- Mention the community plugin
- Show it in action
- Cmd-Shift-A Py Con
- Opens Python Console, with Django imports, using ipython
- Note the different repl
print(2+2)with syntax highlighting, autocomplete
- Make sure
BlackConnectis off! - Open
.editorconfig - Change
4to14 - Open
conftest.py - Reformat Code
- Change
14back to4 - Reformat Code
- Enable BlackConnect
- Note the
.pre-commit-config.yaml - Commit window, gear icon at bottom, show the checkbox for git hooks
This will be used in the sponsor talk at PyCon US 2023. It emphasizes points made in Adam Johnson's Django DX book.