mirror of
https://github.com/skoobasteeve/django-movies.git
synced 2026-03-20 03:28:59 +00:00
Compare commits
4 Commits
384b2e7303
...
django-tut
| Author | SHA1 | Date | |
|---|---|---|---|
|
fc715c3ced
|
|||
|
b1c9b88de1
|
|||
|
58ab1e7151
|
|||
|
72d10ab9d2
|
9
apps/movies/admin.py
Normal file
9
apps/movies/admin.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import MediaFormat, Movie
|
||||
|
||||
# Register your models here.
|
||||
|
||||
# Register your models here.
|
||||
admin.site.register(Movie)
|
||||
admin.site.register(MediaFormat)
|
||||
5
apps/movies/apps.py
Normal file
5
apps/movies/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class MoviesConfig(AppConfig):
|
||||
name = "apps.movies"
|
||||
32
apps/movies/migrations/0001_initial.py
Normal file
32
apps/movies/migrations/0001_initial.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# Generated by Django 6.0 on 2026-02-28 17:08
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='MediaFormat',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=50)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Movie',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=200)),
|
||||
('release_date', models.DateField()),
|
||||
('pub_date', models.DateTimeField(verbose_name='date published')),
|
||||
('media_formats', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='movies.mediaformat')),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,26 @@
|
||||
# Generated by Django 6.0 on 2026-02-28 17:14
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('movies', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='movie',
|
||||
name='pub_date',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='movie',
|
||||
name='media_formats',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='movie',
|
||||
name='media_formats',
|
||||
field=models.ManyToManyField(to='movies.mediaformat'),
|
||||
),
|
||||
]
|
||||
20
apps/movies/migrations/0003_movie_added_date.py
Normal file
20
apps/movies/migrations/0003_movie_added_date.py
Normal file
@@ -0,0 +1,20 @@
|
||||
# Generated by Django 6.0 on 2026-02-28 17:38
|
||||
|
||||
import django.utils.timezone
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('movies', '0002_remove_movie_pub_date_remove_movie_media_formats_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='movie',
|
||||
name='added_date',
|
||||
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
0
apps/movies/migrations/__init__.py
Normal file
0
apps/movies/migrations/__init__.py
Normal file
21
apps/movies/models.py
Normal file
21
apps/movies/models.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
# Create your models here.
|
||||
class MediaFormat(models.Model):
|
||||
name = models.CharField(max_length=50)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Movie(models.Model):
|
||||
title = models.CharField(max_length=200)
|
||||
release_date = models.DateField()
|
||||
media_formats = models.ManyToManyField(MediaFormat)
|
||||
|
||||
added_date = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
52
apps/movies/templates/movies/index.html
Normal file
52
apps/movies/templates/movies/index.html
Normal file
@@ -0,0 +1,52 @@
|
||||
<!doctype html>
|
||||
<title>Movies Homepage</title>
|
||||
|
||||
<style>
|
||||
.table-hover {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-family: sans-serif;
|
||||
font-size: 0.9rem;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.table-hover thead th {
|
||||
/* Style for the header */
|
||||
padding: 12px 15px;
|
||||
background-color: #f4f4f4;
|
||||
border-bottom: 2px solid #ccc;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.table-hover td {
|
||||
padding: 12px 15px;
|
||||
border-bottom: 1px solid #e0e0e0;
|
||||
}
|
||||
|
||||
/* This is the key part of the template */
|
||||
.table-hover tbody tr:hover {
|
||||
background-color: #f5f5f5; /* A light grey for the highlight */
|
||||
cursor: pointer; /* Changes the cursor to a pointer to indicate interactivity */
|
||||
}
|
||||
</style>
|
||||
|
||||
<table class="table-hover">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Title</th>
|
||||
<th scope="col">Release Date</th>
|
||||
<th scope="col">Date Added</th>
|
||||
<th scope="col">Formats</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for m in latest_movies %}
|
||||
<tr>
|
||||
<th>{{ m.title }}</th>
|
||||
<th>{{ m.release_date }}</th>
|
||||
<th>{{ m.added_date }}</th>
|
||||
<th>{{ m.media_formats }}</th>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -3,5 +3,5 @@ from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path("", views.index, name="index"),
|
||||
path("", views.IndexView.as_view(), name="index"),
|
||||
]
|
||||
18
apps/movies/views.py
Normal file
18
apps/movies/views.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
from django.views import generic
|
||||
|
||||
from .models import Movie
|
||||
|
||||
|
||||
def index(request):
|
||||
return HttpResponse("Hello, world. You're at the movies index.")
|
||||
|
||||
|
||||
class IndexView(generic.ListView):
|
||||
template_name = "movies/index.html"
|
||||
context_object_name = "latest_movies"
|
||||
|
||||
def get_queryset(self):
|
||||
"""Return the last five published questions."""
|
||||
return Movie.objects.order_by("-added_date")[:5]
|
||||
0
apps/polls/__init__.py
Normal file
0
apps/polls/__init__.py
Normal file
7
apps/polls/admin.py
Normal file
7
apps/polls/admin.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Choice, Question
|
||||
|
||||
# Register your models here.
|
||||
admin.site.register(Question)
|
||||
admin.site.register(Choice)
|
||||
@@ -2,4 +2,4 @@ from django.apps import AppConfig
|
||||
|
||||
|
||||
class PollsConfig(AppConfig):
|
||||
name = 'polls'
|
||||
name = "apps.polls"
|
||||
32
apps/polls/migrations/0001_initial.py
Normal file
32
apps/polls/migrations/0001_initial.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# Generated by Django 6.0 on 2025-12-21 22:37
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Question',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('question_text', models.CharField(max_length=200)),
|
||||
('pub_date', models.DateTimeField(verbose_name='date published')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Choice',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('choice_text', models.CharField(max_length=200)),
|
||||
('votes', models.IntegerField(default=0)),
|
||||
('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.question')),
|
||||
],
|
||||
),
|
||||
]
|
||||
0
apps/polls/migrations/__init__.py
Normal file
0
apps/polls/migrations/__init__.py
Normal file
26
apps/polls/models.py
Normal file
26
apps/polls/models.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import datetime
|
||||
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
|
||||
# Create your models here.
|
||||
|
||||
|
||||
class Question(models.Model):
|
||||
question_text = models.CharField(max_length=200)
|
||||
pub_date = models.DateTimeField("date published")
|
||||
|
||||
def __str__(self):
|
||||
return self.question_text
|
||||
|
||||
def was_published_recently(self):
|
||||
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
|
||||
|
||||
|
||||
class Choice(models.Model):
|
||||
question = models.ForeignKey(Question, on_delete=models.CASCADE)
|
||||
choice_text = models.CharField(max_length=200)
|
||||
votes = models.IntegerField(default=0)
|
||||
|
||||
def __str__(self):
|
||||
return self.choice_text
|
||||
14
apps/polls/templates/polls/detail.html
Normal file
14
apps/polls/templates/polls/detail.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<form action="{% url 'polls:vote' question.id %}" method="post">
|
||||
{% csrf_token %}
|
||||
<fieldset>
|
||||
<legend><h1>{{ question.question_text }}</h1></legend>
|
||||
{% if error_message %}
|
||||
<p><strong>{{ error_message }}</strong></p>
|
||||
{% endif %}
|
||||
{% for choice in question.choice_set.all %}
|
||||
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
|
||||
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
<input type="submit" value="Vote">
|
||||
</form>
|
||||
13
apps/polls/templates/polls/index.html
Normal file
13
apps/polls/templates/polls/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
{% if latest_question_list %}
|
||||
<ul>
|
||||
{% for question in latest_question_list %}
|
||||
<li>
|
||||
<a href="{% url 'polls:detail' question.id %}"
|
||||
>{{ question.question_text }}</a
|
||||
>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% else %}
|
||||
<p>No polls are available.</p>
|
||||
{% endif %}
|
||||
9
apps/polls/templates/polls/results.html
Normal file
9
apps/polls/templates/polls/results.html
Normal file
@@ -0,0 +1,9 @@
|
||||
<h1>{{ question.question_text }}</h1>
|
||||
|
||||
<ul>
|
||||
{% for choice in question.choice_set.all %}
|
||||
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
|
||||
3
apps/polls/tests.py
Normal file
3
apps/polls/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
11
apps/polls/urls.py
Normal file
11
apps/polls/urls.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
app_name = "polls"
|
||||
urlpatterns = [
|
||||
path("", views.IndexView.as_view(), name="index"),
|
||||
path("<int:pk>/", views.DetailView.as_view(), name="detail"),
|
||||
path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
|
||||
path("<int:question_id>/vote/", views.vote, name="vote"),
|
||||
]
|
||||
49
apps/polls/views.py
Normal file
49
apps/polls/views.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from django.db.models import F
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.urls import reverse
|
||||
from django.views import generic
|
||||
|
||||
from .models import Choice, Question
|
||||
|
||||
|
||||
class IndexView(generic.ListView):
|
||||
template_name = "polls/index.html"
|
||||
context_object_name = "latest_question_list"
|
||||
|
||||
def get_queryset(self):
|
||||
"""Return the last five published questions."""
|
||||
return Question.objects.order_by("-pub_date")[:5]
|
||||
|
||||
|
||||
class DetailView(generic.DetailView):
|
||||
model = Question
|
||||
template_name = "polls/detail.html"
|
||||
|
||||
|
||||
class ResultsView(generic.DetailView):
|
||||
model = Question
|
||||
template_name = "polls/results.html"
|
||||
|
||||
|
||||
def vote(request, question_id):
|
||||
question = get_object_or_404(Question, pk=question_id)
|
||||
try:
|
||||
selected_choice = question.choice_set.get(pk=request.POST["choice"])
|
||||
except (KeyError, Choice.DoesNotExist):
|
||||
# Redisplay the question voting form.
|
||||
return render(
|
||||
request,
|
||||
"polls/detail.html",
|
||||
{
|
||||
"question": question,
|
||||
"error_message": "You didn't select a choice.",
|
||||
},
|
||||
)
|
||||
else:
|
||||
selected_choice.votes = F("votes") + 1
|
||||
selected_choice.save()
|
||||
# Always return an HttpResponseRedirect after successfully dealing
|
||||
# with POST data. This prevents data from being posted twice if a
|
||||
# user hits the Back button.
|
||||
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
|
||||
@@ -20,7 +20,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'django-insecure-yt9sy+oy77u7i3adf2&jno_&%448n5ic&ih9tk#s9ad3gxt$#5'
|
||||
SECRET_KEY = "django-insecure-yt9sy+oy77u7i3adf2&jno_&%448n5ic&ih9tk#s9ad3gxt$#5"
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
@@ -31,51 +31,53 @@ ALLOWED_HOSTS = []
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
"apps.polls.apps.PollsConfig",
|
||||
"apps.movies.apps.MoviesConfig",
|
||||
"django.contrib.admin",
|
||||
"django.contrib.auth",
|
||||
"django.contrib.contenttypes",
|
||||
"django.contrib.sessions",
|
||||
"django.contrib.messages",
|
||||
"django.contrib.staticfiles",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'django_movies.urls'
|
||||
ROOT_URLCONF = "django_movies.urls"
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||
"DIRS": [],
|
||||
"APP_DIRS": True,
|
||||
"OPTIONS": {
|
||||
"context_processors": [
|
||||
"django.template.context_processors.request",
|
||||
"django.contrib.auth.context_processors.auth",
|
||||
"django.contrib.messages.context_processors.messages",
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'django_movies.wsgi.application'
|
||||
WSGI_APPLICATION = "django_movies.wsgi.application"
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': BASE_DIR / 'db.sqlite3',
|
||||
"default": {
|
||||
"ENGINE": "django.db.backends.sqlite3",
|
||||
"NAME": BASE_DIR / "db.sqlite3",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,16 +87,16 @@ DATABASES = {
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
|
||||
},
|
||||
]
|
||||
|
||||
@@ -102,9 +104,9 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/6.0/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
LANGUAGE_CODE = "en-us"
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
TIME_ZONE = "America/New_York"
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
@@ -114,4 +116,4 @@ USE_TZ = True
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/6.0/howto/static-files/
|
||||
|
||||
STATIC_URL = 'static/'
|
||||
STATIC_URL = "static/"
|
||||
|
||||
@@ -20,5 +20,6 @@ from django.urls import include, path
|
||||
|
||||
urlpatterns = [
|
||||
path("admin/", admin.site.urls),
|
||||
path("polls/", include("polls.urls")),
|
||||
path("polls/", include("apps.polls.urls")),
|
||||
path("", include("apps.movies.urls")),
|
||||
]
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
@@ -1,3 +0,0 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
@@ -1,9 +0,0 @@
|
||||
from django.http import HttpResponse
|
||||
|
||||
|
||||
def index(request):
|
||||
return HttpResponse("Hello, world. You're at the polls index.")
|
||||
|
||||
|
||||
def index2(request):
|
||||
return HttpResponse("Hello, world. You're at the polls index #2.")
|
||||
6
pyrightconfig.json
Normal file
6
pyrightconfig.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"include": ["apps", "django_movies"],
|
||||
"extraPaths": ["."],
|
||||
"pythonVersion": "3.14",
|
||||
"typeCheckingMode": "basic",
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
django==6.0
|
||||
django-stubs==5.2.8
|
||||
|
||||
Reference in New Issue
Block a user