Django

Create Project

python -m django startproject <project-name>
django-admin startproject <project-name>

Runserver

python manage.py runserver 
django-admin runserver

Create App

python manage.py startapp <app-name>
django-admin startapp <app-name>

Add the app name to INSTALLED_APPS in settings.py

Create database entries

python manage.py makemigrations
python manage.py migrate

URL

from <'app-name'> import views
urlpatterns = [ 
		path('admin/', admin.site.urls),
		<'add new urls here',app-name.function-name,name='url name'>
		 ]
path('news/', include('news.urls'))

Views

from django.http import HttpResponse
def home(request): 
	return HttpResponse('<h1 Welcome to Home Page</h1')

def home(request):
	return render(request,'example.html')

Creating Templates

Passing data

return render(request, 'home.html', {'name':'Greg Lim'})
<h1> Welcome to Home Page, {{ name }} </h1>

URL render

<form action="{% url 'signup' %}">

Get request from form

def signup(request): 
	email = request.GET.get('email') 
	return render(request, 'signup.html', {'email':email}

Configuring for images

We have to configure where to store our images when we add them. First, go to settings.py and add the following at the bottom of the file:

MEDIA_ROOT = os.path.join(BASE_DIR,'media') 
MEDIA_URL = '/media/'

Serving the stored images

In urls.py add:

from django.conf.urls.static import static 
from django.conf import settings
urlpatterns = [ 
						…
					] 
					
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Models

from django.db import models 
class Movie(models.Model): 
	title = models.CharField(max_length=100) 
	description = models.CharField(max_length=250)
	image = models.ImageField(upload_to='movie/images/')
	url = models.URLField(blank=True)
	
	def __str__(self):
		 return self.headline

Because we are using images, we need pillow. So install pillow

Creating admin access: python manage.py createsuperuser

Add model to admin panel:

from .models import Movie
admin.site.register(Movie)

Listing from models

In views.py:

from .models import Movie
def home(request):
	searchTerm = request.GET.get('searchMovie')
	movies = Movie.objects.all()
	return render(request, 'home.html', {'searchTerm':searchTerm, 'movies': movies})
{% for movie in movies %}
<h1> {{ movie.title }} </h1>
<h2> {{ movie.description }} </h2>
<img src="{{ movie.image.url }}"/>
{% if movie.url %} 
<a href="{{ movie.url }}">Movie Link </a> 
{% endif %} 
{% endfor %}

Database

To switch to another database engine, you can go to settings.py and make changes to the lines:

DATABASES = { 
'default': 
{ 'ENGINE': 
'django.db.backends.sqlite3', 
'NAME': BASE_DIR / 'db.sqlite3', 
}
}

Extending Base Templates

TEMPLATES = [ 
			 { 
			 'BACKEND': 'django.template.backends.django .DjangoTemplates', 
			 'DIRS': [os.path.join(BASE_DIR, 'moviereviews/templates')], 
			 'APP_DIRS': True, 
			 
			 …


{% extends 'base.html' %} # used inside template
{% block content %}
{% endblock content %}

Serving static files