Day 18 – Intro to Django!

So I started Django today. My first impression is that it’s definitely better suited for larger projects than Flask is. Pretty much everything gets done in one file in Flask, which can get real messy real fast. Django, on the other hand, is designed with the MVC paradigm in mind, so everything is separated out into a nice, structured hierarchy of files. In fact, there are so many separate files and folders to keep track of that it was a tad overwhelming at first. Right off the bat, instructor Fiaz had me memorize the startup process for the beginning setup of a new Django process and drill it in over and over again until I was able to do the whole process in under two minutes. I guess I can even list them here because why not.

  1. Enter your Django virtual environment
  2. Run the command django-admin startproject projectNameHere wherever you want your project folder to be located
  3. cd into the projectNameHere directory and create the apps folder with mkdir apps
  4. cd into the apps directory and touch __init__.py
  5. Run the command python ../manage.py startapp appNameHere
  6. Open the project folder in atom with atom .. (or use some other text editor)
  7. Navigate to the settings.py file in the projectNameHere subfolder
  8. Add the new app 'apps.appNameHere', to the top of the INSTALLED_APPS list. And don’t forget the comma! I got errors for that quite a few times, lol.
  9. Navigate to urls.py in the same folder, add include to the import line right after url so that it looks like from django.conf.urls import url, include.
  10. Add another pattern for django to listen for in the urlpatterns list , url(r'^', include('apps.appNameHere.urls')),. This file we’re including doesn’t exist yet but we’ll add it soon.
  11. Next, navigate to the views.py in the appNameHere folder inside of the apps folder you created earlier.
  12. In views.py, all of your controller methods need to be defined here. Just to get started I like to import HttpResponse and define my index method like so:
  13. Now we need to create that urls file. Inside the appNameHere folder, create a new file called urls.py and paste this in there:
  14. And now we should be good to go! Go back to the terminal and run python ../manage.py runserver and go to localhost:8000 in your web browser!

This little explanation took longer than I thought it would! I really need to figure out some better way of putting code into wordpress posts… Anyways, after this initial setup, the project is ready to be filled up with more functionality. There are a couple more steps to add when rendering actual HTML templates rather than just simple HttpResponses, but this is the basic setup that needs to happen for every project. I’m liking Django so far and I’m excited to translate more of my flask projects over. I’m gonna really dig in these next few days and do my best to prepare for the belt exam. I’m really looking forward to project week, so I better not fail it, lol!

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s