Django

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Create django project using

$ django-admin.py startproject tango_with_django_project

To run the development server with optional port

$ python manage.py runserver <your_machines_ip_address>:5555

To add mappings, you must call urlpatterns

Calls a series of django.conf.urls.url() function

python manage.py startapp rango

Creates a new application called rango within project directory

view.py

Each view exists within this file as a series of individual functions example def index()... index is a view Each view takes at least 1 argument, a HttpRequest object Each view must return a HttpResponse object, which has a string parameter representing the content of the page we wish to send to the client requesting the view

manage.py

Script used when developing project, provides a series of commands to help maintain Django project, example runs built-in django development server to test your work and run database commands

Django project is a collection of configurations and applications that make up a web application or website

Such that developing a small series of applications allow you to drop an existing application intoa different project and have it working

Creating a view

Within application views.py files, edit such that: from django.http import HttpResponse def index(request): return HttpResponse("Rango says hey there world!")

Python configuration files

__init__.py indicating to python interpreter that the directory is a python package settings.py stores all django project's settings urls.py stores URL patters for project wsgi.py used to help run your development server and deploy your project to production environment

django-admin.py

allows you start new projects and apps, within project directory

manage.py

allows you to perform administrative tasks within the scope of the project only

startapp command creates a new directory with 5 python scripts

another __init__.py, serving the exact same purpose as discussed previously; models.py, a place to store your application's data models - where you specify the entities and relationships between data; tests.py, where you can store a series of functions to test your application's code; and views.py, where you can store a series of functions that take a clients's requests and return responses. admin.py, where you can register your models so that you can benefit from some Django machinery which creates an admin interface for you (see #TODO(leifos):add link to admin chapter)

^$

is a regular expression which matches to an empty string URL pattern matching only takes a portion of the original url string to be considered to match with a view

What's within the directory?

nested directory with same name as file, this nested directory will be the project's configuration directory. And manage.py script

Before creating application views and models you are required to

notify django project about your new application existence, by modifying settings.py

To configure urls.py with main project. Connect main project to rango

open project's urls.py and update the urlpatterns this mapping looks for url string that match the pattersn ^rango/. After a match occurs the remainder of url string is passed onto and handled by rango.urls

Maaping urls

within application directory add a file called urls.py from django.conf.urls import patterns, url from rango import views urlpatterns = patterns('', url(r'^$', views.index, name='index')) imports views such that we can access our views, allowing us to reference the view in the url mapping


Kaugnay na mga set ng pag-aaral

Chapter 42: Assessment and Management of Patients with Obesity

View Set

Science and Technology Applications For Life

View Set

Past Perfect Simple, the Past Perfect Continuous, Past Simple (Complete the sentences using the appropriate tense-forms) part 2

View Set