1
LEARNING DJANGO: DJANGO IS A PYTHON WEB DEVELOPMENT FRAMEWORK

LEARNING DJANGO

DJANGO IS A PYTHON WEB DEVELOPMENT FRAMEWORK

DJANGO, PYTHON 3

MEDIUM

last hacked on Sep 23, 2017

From the [original docs](https://www.djangoproject.com/): _Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It's free and open source. Ridiculously fast._
# Requirements: ### Update Python Update your system and make sure Python 3 is installed: $ sudo apt-get update $ sudo apt-get -y upgrade $ sudo apt-get dist-upgrade ### Install Django: $ pip3 install django ### Create a Virtualenv Create and activate a virtual enviroment named `venv`: $ pip3 install --upgrade pip $ pip3 install virtualenv $ virtualenv -p /usr/bin/python3 venv $ source venv/bin/activate Virtual environments are useful in storing project-specific dependencies and packages versions separate from other globally installed packages. ### Install Django within Virtualenv: Next, check your installed versions of your `python` and `pip`; then install `django` in an active virtual environment: (venv) $ which python (venv) $ which pip (venv) $ python --version (venv) $ pip install django #### Deactivate Virtualenv When you are done with your virtual environment, you can exit by typing: (venv) $ deactivate If you deactivated your virtual environment, activate it once more by entering into your terminal `source venv/bin/activate`. # Some Background: Django follows a Model View Template (MVT) framework, meaning the controller (which would normally take in input and turn it into commands for the model & view) is handeled by the framework itself. Here is an overview of what each part does: ## Models (M) * Data representation (defines data structure) * Database queries ## Views (V) * Data manipulation/ templating * Describes *which* data gets presented to the user * Serves as the pipeline between Models and Template * Getting data in and out of database * processes HTTP requests ## Templates (T) * Describes *how* data is presented * Renders data in HTML * Sends response to web browser to display "pretty" data You can learn about Django's framework more from their site: [Django Documentaion](https://docs.djangoproject.com/en/1.11/faq/general/) # First Django Project ### Create Project: Move to the directory where you want your project. To create a django project, type: $ django-admin startproject <PROJECT_NAME> Make sure to replace `<PROJECT_NAME>` with a project name of your choosing. Avoid names that start with numbers or include the name "django" This will create a project directory with an array of files and sub-directories. Notably, you will find the file `manage.py` which will enable you to interact with your project by leveraging powerful Django features, and related packages. You will also find a sub-directory (also) named <PROJECT_NAME>, which will contain important configuration files. ### Runserver: To run your app locally, move back to your project's directory, where the file `manage.py` is located, and enter: $ python manage.py runserver In your web browser, to open page: copy and paste: http://127.0.0.1:8000/ You should see a web page that says "It Worked! Congratulations on your first Django-powered page." To quit the server, hit CONTROL-C and you will be taken back to your prompt line. You can also change the server's port by simply typing a different number at the end of the `runserver` command. This is useful for running multiple applications in the same local environment. For example, to use port `9000` type: $ python manage.py runserver 9000 ## Creating First Django App: Blog To create an application called "blog" type: $ python manage.py startapp blog This should create the directory "blog" in the myproject folder with the following files: admin, apps, migrations, models, test, and views (as well as __init__ which you don't need to worry about for now). Any time you make changes to the models.py file, you need to create migrations, which transforms the database to mimic these changes. To create migrations and apply changes to the database, type: $ python manage.py makemigration $ python manage.py migrate --- ## Additional Resources: * [Django Website](https://djangoproject.com) * [Djangogirls](https://djangogirls.org) * [Sentdex Django Series](https://www.youtube.com/watch?v=FNQxxpM1yOs&list=PLQVvvaa0QuDeA05ZouE4OzDYLHY-XH-Nd) --- ## Interesting Django Packages ### crispy-forms **Install crispy-forms** Run on your terminal: pip install --upgrade django-crispy-forms Add `crispy_forms` to your `INSTALLED_APPS` in `settings.py`: INSTALLED_APPS = ( 'crispy_forms', ) **Use crispy-forms** Load `crispy_forms_tags` to the template (e.g. `login_form.html`) you want to use `crispy_forms` for. {% load crispy_forms_tags %} Within the same template file, use `crispy_forms` by adding `crispy` as a filter within the form tag: {{ form|crispy }} Keep in mind that your views should be returning such `form` to your template.

COMMENTS


back to all projects