Django
BACK: Navigation
django-admin
brings up a list of django-admin commands to use.
django-admin startproject mysite
creates a new django project
python manage.py runserver
runs the web app
Routing
If you go to 127.0.0.1:8000/admin, we are routed to a new place–the logic to handle this is in urls.py
Creating apps for our project:
We can have several apps in our project and copy paste apps into several different projects. We can create our first app with:
python manage.py startapp polls
We will now create a view. In polls/view.py
:
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello, world. You're at the polls index.</h1>")
This creates the view, but in order to use it, we need to map it to a URL first. In polls/urls.py
:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Now we have to map the URL of the app to a URL of the main project. In mysite/urls.py
:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
This adds all the urls within polls/urls.py
. If we add another view (ie url(r'^login$', views.login, name='login')
) in polls/views.py
and map that to polls/urls.py
, we can visit that new view at 127.0.0.1/8000/polls/login
Connecting to Database, Looking at Settings
In mysite/settings.py
, you can configure database settings down at DATABASES = { ... }
While here, take a look at INSTALLED_APPS
. These are all the Django apps that are used in this Django instance. These apps usually make use of a database table, so we can use python manage.py migrate
to create tables in our DB connection.
Now we can create our models–our DB schemas. In polls/models.py
:
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
The variable names will become column names in the DB. The Field type specifies what type of data will be stored in that column.
This little bit of information will create our DB for us and provide an API where we can use that information. We can add this into our mysite/settings.py
and then migrate again.
INSTALLED_APPS = [
'polls.apps.PollsConfig', # named from polls/apps.py
...
]
python manage.py makemigrations polls
This tells Django that we’ve made some changes, seen in polls/migrations/0001_initial.py. We can then call python manage.py sqlmigrate polls 0001
to run those changes and create SQL based on those changes. This doesn’t actually run the SQL–it’s just good to check what Django is about to do when you run python manage.py migrate
again.
API
We can import these classes that we’ve made using:
from polls.models import Question, Choice
From here, we can directly interact with the database.
q = Question()
q.question_text= "What is your name?"
q.save()
There are other API functionalities.